xref: /plugin/pureldap/vendor/freedsx/ldap/src/FreeDSx/Ldap/Entry/Change.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\Entry;
130b3fd2d3SAndreas Gohr
14*dad993c5SAndreas Gohruse function count;
15*dad993c5SAndreas Gohr
160b3fd2d3SAndreas Gohr/**
170b3fd2d3SAndreas Gohr * Represents an entry change.
180b3fd2d3SAndreas Gohr *
190b3fd2d3SAndreas Gohr * @author Chad Sikorra <Chad.Sikorra@gmail.com>
200b3fd2d3SAndreas Gohr */
210b3fd2d3SAndreas Gohrclass Change
220b3fd2d3SAndreas Gohr{
230b3fd2d3SAndreas Gohr    /**
240b3fd2d3SAndreas Gohr     * Add a value to an attribute.
250b3fd2d3SAndreas Gohr     */
260b3fd2d3SAndreas Gohr    public const TYPE_ADD = 0;
270b3fd2d3SAndreas Gohr
280b3fd2d3SAndreas Gohr    /**
290b3fd2d3SAndreas Gohr     * Delete a value, or values, from an attribute.
300b3fd2d3SAndreas Gohr     */
310b3fd2d3SAndreas Gohr    public const TYPE_DELETE = 1;
320b3fd2d3SAndreas Gohr
330b3fd2d3SAndreas Gohr    /**
340b3fd2d3SAndreas Gohr     * Replaces the current value of an attribute with a different one.
350b3fd2d3SAndreas Gohr     */
360b3fd2d3SAndreas Gohr    public const TYPE_REPLACE = 2;
370b3fd2d3SAndreas Gohr
380b3fd2d3SAndreas Gohr    /**
390b3fd2d3SAndreas Gohr     * @var int
400b3fd2d3SAndreas Gohr     */
410b3fd2d3SAndreas Gohr    protected $modType;
420b3fd2d3SAndreas Gohr
430b3fd2d3SAndreas Gohr    /**
440b3fd2d3SAndreas Gohr     * @var Attribute
450b3fd2d3SAndreas Gohr     */
460b3fd2d3SAndreas Gohr    protected $attribute;
470b3fd2d3SAndreas Gohr
480b3fd2d3SAndreas Gohr    /**
490b3fd2d3SAndreas Gohr     * @param int $modType
500b3fd2d3SAndreas Gohr     * @param string|Attribute $attribute
51*dad993c5SAndreas Gohr     * @param string ...$values
520b3fd2d3SAndreas Gohr     */
530b3fd2d3SAndreas Gohr    public function __construct(int $modType, $attribute, ...$values)
540b3fd2d3SAndreas Gohr    {
550b3fd2d3SAndreas Gohr        $this->modType = $modType;
560b3fd2d3SAndreas Gohr        $this->attribute = $attribute instanceof Attribute ? $attribute : new Attribute($attribute, ...$values);
570b3fd2d3SAndreas Gohr    }
580b3fd2d3SAndreas Gohr
590b3fd2d3SAndreas Gohr    /**
600b3fd2d3SAndreas Gohr     * @return Attribute
610b3fd2d3SAndreas Gohr     */
620b3fd2d3SAndreas Gohr    public function getAttribute(): Attribute
630b3fd2d3SAndreas Gohr    {
640b3fd2d3SAndreas Gohr        return $this->attribute;
650b3fd2d3SAndreas Gohr    }
660b3fd2d3SAndreas Gohr
670b3fd2d3SAndreas Gohr    /**
680b3fd2d3SAndreas Gohr     * @param Attribute $attribute
690b3fd2d3SAndreas Gohr     * @return $this
700b3fd2d3SAndreas Gohr     */
710b3fd2d3SAndreas Gohr    public function setAttribute(Attribute $attribute)
720b3fd2d3SAndreas Gohr    {
730b3fd2d3SAndreas Gohr        $this->attribute = $attribute;
740b3fd2d3SAndreas Gohr
750b3fd2d3SAndreas Gohr        return $this;
760b3fd2d3SAndreas Gohr    }
770b3fd2d3SAndreas Gohr
780b3fd2d3SAndreas Gohr    /**
790b3fd2d3SAndreas Gohr     * @return int
800b3fd2d3SAndreas Gohr     */
810b3fd2d3SAndreas Gohr    public function getType(): int
820b3fd2d3SAndreas Gohr    {
830b3fd2d3SAndreas Gohr        return $this->modType;
840b3fd2d3SAndreas Gohr    }
850b3fd2d3SAndreas Gohr
860b3fd2d3SAndreas Gohr    /**
870b3fd2d3SAndreas Gohr     * @param int $modType
880b3fd2d3SAndreas Gohr     * @return $this
890b3fd2d3SAndreas Gohr     */
900b3fd2d3SAndreas Gohr    public function setType(int $modType)
910b3fd2d3SAndreas Gohr    {
920b3fd2d3SAndreas Gohr        $this->modType = $modType;
930b3fd2d3SAndreas Gohr
940b3fd2d3SAndreas Gohr        return $this;
950b3fd2d3SAndreas Gohr    }
960b3fd2d3SAndreas Gohr
970b3fd2d3SAndreas Gohr    /**
980b3fd2d3SAndreas Gohr     * @return bool
990b3fd2d3SAndreas Gohr     */
1000b3fd2d3SAndreas Gohr    public function isAdd(): bool
1010b3fd2d3SAndreas Gohr    {
1020b3fd2d3SAndreas Gohr        return $this->modType === self::TYPE_ADD;
1030b3fd2d3SAndreas Gohr    }
1040b3fd2d3SAndreas Gohr
1050b3fd2d3SAndreas Gohr    /**
1060b3fd2d3SAndreas Gohr     * @return bool
1070b3fd2d3SAndreas Gohr     */
1080b3fd2d3SAndreas Gohr    public function isDelete(): bool
1090b3fd2d3SAndreas Gohr    {
110*dad993c5SAndreas Gohr        return $this->modType === self::TYPE_DELETE && count($this->attribute->getValues()) !== 0;
1110b3fd2d3SAndreas Gohr    }
1120b3fd2d3SAndreas Gohr
1130b3fd2d3SAndreas Gohr    /**
1140b3fd2d3SAndreas Gohr     * @return bool
1150b3fd2d3SAndreas Gohr     */
1160b3fd2d3SAndreas Gohr    public function isReplace(): bool
1170b3fd2d3SAndreas Gohr    {
1180b3fd2d3SAndreas Gohr        return $this->modType === self::TYPE_REPLACE;
1190b3fd2d3SAndreas Gohr    }
1200b3fd2d3SAndreas Gohr
1210b3fd2d3SAndreas Gohr    /**
1220b3fd2d3SAndreas Gohr     * @return bool
1230b3fd2d3SAndreas Gohr     */
1240b3fd2d3SAndreas Gohr    public function isReset(): bool
1250b3fd2d3SAndreas Gohr    {
126*dad993c5SAndreas Gohr        return $this->modType === self::TYPE_DELETE && count($this->attribute->getValues()) === 0;
1270b3fd2d3SAndreas Gohr    }
1280b3fd2d3SAndreas Gohr
1290b3fd2d3SAndreas Gohr    /**
1300b3fd2d3SAndreas Gohr     * Add the values contained in the attribute, creating the attribute if necessary.
1310b3fd2d3SAndreas Gohr     *
1320b3fd2d3SAndreas Gohr     * @param string|Attribute $attribute
133*dad993c5SAndreas Gohr     * @param string ...$values
1340b3fd2d3SAndreas Gohr     * @return Change
1350b3fd2d3SAndreas Gohr     */
1360b3fd2d3SAndreas Gohr    public static function add($attribute, ...$values): Change
1370b3fd2d3SAndreas Gohr    {
1380b3fd2d3SAndreas Gohr        $attribute = $attribute instanceof Attribute ? $attribute : new Attribute($attribute, ...$values);
1390b3fd2d3SAndreas Gohr
1400b3fd2d3SAndreas Gohr        return new self(self::TYPE_ADD, $attribute);
1410b3fd2d3SAndreas Gohr    }
1420b3fd2d3SAndreas Gohr
1430b3fd2d3SAndreas Gohr    /**
1440b3fd2d3SAndreas Gohr     * Delete values from the attribute. If no values are listed, or if all current values of the attribute are listed,
1450b3fd2d3SAndreas Gohr     * the entire attribute is removed.
1460b3fd2d3SAndreas Gohr     *
1470b3fd2d3SAndreas Gohr     * @param Attribute|string $attribute
148*dad993c5SAndreas Gohr     * @param string ...$values
1490b3fd2d3SAndreas Gohr     * @return Change
1500b3fd2d3SAndreas Gohr     */
1510b3fd2d3SAndreas Gohr    public static function delete($attribute, ...$values): Change
1520b3fd2d3SAndreas Gohr    {
1530b3fd2d3SAndreas Gohr        $attribute = $attribute instanceof Attribute ? $attribute : new Attribute($attribute, ...$values);
1540b3fd2d3SAndreas Gohr
1550b3fd2d3SAndreas Gohr        return new self(self::TYPE_DELETE, $attribute);
1560b3fd2d3SAndreas Gohr    }
1570b3fd2d3SAndreas Gohr
1580b3fd2d3SAndreas Gohr    /**
1590b3fd2d3SAndreas Gohr     * Replace all existing values with the new values, creating the attribute if it did not already exist.  A replace
1600b3fd2d3SAndreas Gohr     * with no value will delete the entire attribute if it exists, and it is ignored if the attribute does not exist.
1610b3fd2d3SAndreas Gohr     *
1620b3fd2d3SAndreas Gohr     * @param Attribute|string $attribute
163*dad993c5SAndreas Gohr     * @param string ...$values
1640b3fd2d3SAndreas Gohr     * @return Change
1650b3fd2d3SAndreas Gohr     */
1660b3fd2d3SAndreas Gohr    public static function replace($attribute, ...$values): Change
1670b3fd2d3SAndreas Gohr    {
1680b3fd2d3SAndreas Gohr        $attribute = $attribute instanceof Attribute ? $attribute : new Attribute($attribute, ...$values);
1690b3fd2d3SAndreas Gohr
1700b3fd2d3SAndreas Gohr        return new self(self::TYPE_REPLACE, $attribute);
1710b3fd2d3SAndreas Gohr    }
1720b3fd2d3SAndreas Gohr
1730b3fd2d3SAndreas Gohr    /**
1740b3fd2d3SAndreas Gohr     * Remove all values from an attribute, essentially un-setting/resetting it. This is the same type as delete when
1750b3fd2d3SAndreas Gohr     * going to LDAP. The real difference being that no values are attached to the change.
1760b3fd2d3SAndreas Gohr     *
1770b3fd2d3SAndreas Gohr     * @param string|Attribute $attribute
1780b3fd2d3SAndreas Gohr     * @return Change
1790b3fd2d3SAndreas Gohr     */
1800b3fd2d3SAndreas Gohr    public static function reset($attribute): Change
1810b3fd2d3SAndreas Gohr    {
1820b3fd2d3SAndreas Gohr        $attribute = $attribute instanceof Attribute ? new Attribute($attribute->getDescription()) : new Attribute($attribute);
1830b3fd2d3SAndreas Gohr
1840b3fd2d3SAndreas Gohr        return new self(self::TYPE_DELETE, $attribute);
1850b3fd2d3SAndreas Gohr    }
1860b3fd2d3SAndreas Gohr}
187