xref: /plugin/pureldap/vendor/freedsx/ldap/src/FreeDSx/Ldap/LdapUrlExtension.php (revision dad993c57a70866aa1db59c43f043769c2eb7ed0)
10b3fd2d3SAndreas Gohr<?php
2*dad993c5SAndreas Gohr
30b3fd2d3SAndreas Gohr/**
40b3fd2d3SAndreas Gohr * This file is part of the FreeDSx LDAP package.
50b3fd2d3SAndreas Gohr *
60b3fd2d3SAndreas Gohr * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
70b3fd2d3SAndreas Gohr *
80b3fd2d3SAndreas Gohr * For the full copyright and license information, please view the LICENSE
90b3fd2d3SAndreas Gohr * file that was distributed with this source code.
100b3fd2d3SAndreas Gohr */
110b3fd2d3SAndreas Gohr
120b3fd2d3SAndreas Gohrnamespace FreeDSx\Ldap;
130b3fd2d3SAndreas Gohr
140b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Exception\UrlParseException;
15*dad993c5SAndreas Gohruse function explode;
16*dad993c5SAndreas Gohruse function str_ireplace;
17*dad993c5SAndreas Gohruse function str_replace;
18*dad993c5SAndreas Gohruse function substr;
190b3fd2d3SAndreas Gohr
200b3fd2d3SAndreas Gohr/**
210b3fd2d3SAndreas Gohr * Represents a LDAP URL extension component. RFC 4516, Section 2.
220b3fd2d3SAndreas Gohr *
230b3fd2d3SAndreas Gohr * @author Chad Sikorra <Chad.Sikorra@gmail.com>
240b3fd2d3SAndreas Gohr */
250b3fd2d3SAndreas Gohrclass LdapUrlExtension
260b3fd2d3SAndreas Gohr{
270b3fd2d3SAndreas Gohr    use LdapUrlTrait;
280b3fd2d3SAndreas Gohr
290b3fd2d3SAndreas Gohr    /**
300b3fd2d3SAndreas Gohr     * @var string
310b3fd2d3SAndreas Gohr     */
320b3fd2d3SAndreas Gohr    protected $name;
330b3fd2d3SAndreas Gohr
340b3fd2d3SAndreas Gohr    /**
350b3fd2d3SAndreas Gohr     * @var null|string
360b3fd2d3SAndreas Gohr     */
370b3fd2d3SAndreas Gohr    protected $value;
380b3fd2d3SAndreas Gohr
390b3fd2d3SAndreas Gohr    /**
400b3fd2d3SAndreas Gohr     * @var bool
410b3fd2d3SAndreas Gohr     */
420b3fd2d3SAndreas Gohr    protected $isCritical = false;
430b3fd2d3SAndreas Gohr
440b3fd2d3SAndreas Gohr    /**
450b3fd2d3SAndreas Gohr     * @param string $name
460b3fd2d3SAndreas Gohr     * @param null|string $value
470b3fd2d3SAndreas Gohr     * @param bool $isCritical
480b3fd2d3SAndreas Gohr     */
490b3fd2d3SAndreas Gohr    public function __construct(string $name, ?string $value = null, bool $isCritical = false)
500b3fd2d3SAndreas Gohr    {
510b3fd2d3SAndreas Gohr        $this->name = $name;
520b3fd2d3SAndreas Gohr        $this->value = $value;
530b3fd2d3SAndreas Gohr        $this->isCritical = $isCritical;
540b3fd2d3SAndreas Gohr    }
550b3fd2d3SAndreas Gohr
560b3fd2d3SAndreas Gohr    /**
570b3fd2d3SAndreas Gohr     * @return string
580b3fd2d3SAndreas Gohr     */
590b3fd2d3SAndreas Gohr    public function getName(): string
600b3fd2d3SAndreas Gohr    {
610b3fd2d3SAndreas Gohr        return $this->name;
620b3fd2d3SAndreas Gohr    }
630b3fd2d3SAndreas Gohr
640b3fd2d3SAndreas Gohr    /**
650b3fd2d3SAndreas Gohr     * @param string $name
660b3fd2d3SAndreas Gohr     * @return $this
670b3fd2d3SAndreas Gohr     */
680b3fd2d3SAndreas Gohr    public function setName(string $name)
690b3fd2d3SAndreas Gohr    {
700b3fd2d3SAndreas Gohr        $this->name = $name;
710b3fd2d3SAndreas Gohr
720b3fd2d3SAndreas Gohr        return $this;
730b3fd2d3SAndreas Gohr    }
740b3fd2d3SAndreas Gohr
750b3fd2d3SAndreas Gohr    /**
760b3fd2d3SAndreas Gohr     * @return null|string
770b3fd2d3SAndreas Gohr     */
780b3fd2d3SAndreas Gohr    public function getValue(): ?string
790b3fd2d3SAndreas Gohr    {
800b3fd2d3SAndreas Gohr        return $this->value;
810b3fd2d3SAndreas Gohr    }
820b3fd2d3SAndreas Gohr
830b3fd2d3SAndreas Gohr    /**
840b3fd2d3SAndreas Gohr     * @param null|string $value
850b3fd2d3SAndreas Gohr     * @return $this
860b3fd2d3SAndreas Gohr     */
870b3fd2d3SAndreas Gohr    public function setValue(?string $value)
880b3fd2d3SAndreas Gohr    {
890b3fd2d3SAndreas Gohr        $this->value = $value;
900b3fd2d3SAndreas Gohr
910b3fd2d3SAndreas Gohr        return $this;
920b3fd2d3SAndreas Gohr    }
930b3fd2d3SAndreas Gohr
940b3fd2d3SAndreas Gohr    public function getIsCritical(): bool
950b3fd2d3SAndreas Gohr    {
960b3fd2d3SAndreas Gohr        return $this->isCritical;
970b3fd2d3SAndreas Gohr    }
980b3fd2d3SAndreas Gohr
990b3fd2d3SAndreas Gohr    /**
1000b3fd2d3SAndreas Gohr     * @param bool $isCritical
1010b3fd2d3SAndreas Gohr     * @return $this
1020b3fd2d3SAndreas Gohr     */
1030b3fd2d3SAndreas Gohr    public function setIsCritical(bool $isCritical)
1040b3fd2d3SAndreas Gohr    {
1050b3fd2d3SAndreas Gohr        $this->isCritical = $isCritical;
1060b3fd2d3SAndreas Gohr
1070b3fd2d3SAndreas Gohr        return $this;
1080b3fd2d3SAndreas Gohr    }
1090b3fd2d3SAndreas Gohr
1100b3fd2d3SAndreas Gohr    /**
1110b3fd2d3SAndreas Gohr     * @return string
1120b3fd2d3SAndreas Gohr     */
1130b3fd2d3SAndreas Gohr    public function toString(): string
1140b3fd2d3SAndreas Gohr    {
115*dad993c5SAndreas Gohr        $ext = ($this->isCritical ? '!' : '') . str_replace(',', '%2c', self::encode($this->name));
1160b3fd2d3SAndreas Gohr
1170b3fd2d3SAndreas Gohr        if ($this->value !== null) {
118*dad993c5SAndreas Gohr            $ext .= '=' . str_replace(',', '%2c', self::encode($this->value));
1190b3fd2d3SAndreas Gohr        }
1200b3fd2d3SAndreas Gohr
1210b3fd2d3SAndreas Gohr        return $ext;
1220b3fd2d3SAndreas Gohr    }
1230b3fd2d3SAndreas Gohr
1240b3fd2d3SAndreas Gohr    /**
1250b3fd2d3SAndreas Gohr     * @return string
1260b3fd2d3SAndreas Gohr     */
1270b3fd2d3SAndreas Gohr    public function __toString()
1280b3fd2d3SAndreas Gohr    {
1290b3fd2d3SAndreas Gohr        return $this->toString();
1300b3fd2d3SAndreas Gohr    }
1310b3fd2d3SAndreas Gohr
1320b3fd2d3SAndreas Gohr    /**
1330b3fd2d3SAndreas Gohr     * @param string $extension
1340b3fd2d3SAndreas Gohr     * @return LdapUrlExtension
1350b3fd2d3SAndreas Gohr     * @throws UrlParseException
1360b3fd2d3SAndreas Gohr     */
1370b3fd2d3SAndreas Gohr    public static function parse(string $extension): LdapUrlExtension
1380b3fd2d3SAndreas Gohr    {
1390b3fd2d3SAndreas Gohr        if (preg_match('/!?\w+(=.*)?/', $extension) !== 1) {
1400b3fd2d3SAndreas Gohr            throw new UrlParseException(sprintf('The LDAP URL extension is malformed: %s', $extension));
1410b3fd2d3SAndreas Gohr        }
142*dad993c5SAndreas Gohr        $pieces = explode('=', $extension, 2);
1430b3fd2d3SAndreas Gohr
1440b3fd2d3SAndreas Gohr        $isCritical = isset($pieces[0][0]) && $pieces[0][0] === '!';
1450b3fd2d3SAndreas Gohr        if ($isCritical) {
146*dad993c5SAndreas Gohr            $pieces[0] = substr($pieces[0], 1);
1470b3fd2d3SAndreas Gohr        }
1480b3fd2d3SAndreas Gohr
149*dad993c5SAndreas Gohr        $name = str_ireplace('%2c', ',', self::decode($pieces[0]));
150*dad993c5SAndreas Gohr        $value = isset($pieces[1]) ? str_ireplace('%2c', ',', self::decode($pieces[1])) : null;
1510b3fd2d3SAndreas Gohr
1520b3fd2d3SAndreas Gohr        return new self($name, $value, $isCritical);
1530b3fd2d3SAndreas Gohr    }
1540b3fd2d3SAndreas Gohr}
155