xref: /plugin/pureldap/vendor/freedsx/ldap/src/FreeDSx/Ldap/Entry/Change.php (revision 3b5700e13795d5d841394b44a81ce0caff6654da)
1<?php
2/**
3 * This file is part of the FreeDSx LDAP package.
4 *
5 * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace FreeDSx\Ldap\Entry;
12
13/**
14 * Represents an entry change.
15 *
16 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
17 */
18class Change
19{
20    /**
21     * Add a value to an attribute.
22     */
23    public const TYPE_ADD = 0;
24
25    /**
26     * Delete a value, or values, from an attribute.
27     */
28    public const TYPE_DELETE = 1;
29
30    /**
31     * Replaces the current value of an attribute with a different one.
32     */
33    public const TYPE_REPLACE = 2;
34
35    /**
36     * @var int
37     */
38    protected $modType;
39
40    /**
41     * @var Attribute
42     */
43    protected $attribute;
44
45    /**
46     * @param int $modType
47     * @param string|Attribute $attribute
48     * @param string[] ...$values
49     */
50    public function __construct(int $modType, $attribute, ...$values)
51    {
52        $this->modType = $modType;
53        $this->attribute = $attribute instanceof Attribute ? $attribute : new Attribute($attribute, ...$values);
54    }
55
56    /**
57     * @return Attribute
58     */
59    public function getAttribute(): Attribute
60    {
61        return $this->attribute;
62    }
63
64    /**
65     * @param Attribute $attribute
66     * @return $this
67     */
68    public function setAttribute(Attribute $attribute)
69    {
70        $this->attribute = $attribute;
71
72        return $this;
73    }
74
75    /**
76     * @return int
77     */
78    public function getType(): int
79    {
80        return $this->modType;
81    }
82
83    /**
84     * @param int $modType
85     * @return $this
86     */
87    public function setType(int $modType)
88    {
89        $this->modType = $modType;
90
91        return $this;
92    }
93
94    /**
95     * @return bool
96     */
97    public function isAdd(): bool
98    {
99        return $this->modType === self::TYPE_ADD;
100    }
101
102    /**
103     * @return bool
104     */
105    public function isDelete(): bool
106    {
107        return $this->modType === self::TYPE_DELETE && \count($this->attribute->getValues()) !== 0;
108    }
109
110    /**
111     * @return bool
112     */
113    public function isReplace(): bool
114    {
115        return $this->modType === self::TYPE_REPLACE;
116    }
117
118    /**
119     * @return bool
120     */
121    public function isReset(): bool
122    {
123        return $this->modType === self::TYPE_DELETE && \count($this->attribute->getValues()) === 0;
124    }
125
126    /**
127     * Add the values contained in the attribute, creating the attribute if necessary.
128     *
129     * @param string|Attribute $attribute
130     * @param string[] ...$values
131     * @return Change
132     */
133    public static function add($attribute, ...$values): Change
134    {
135        $attribute = $attribute instanceof Attribute ? $attribute : new Attribute($attribute, ...$values);
136
137        return new self(self::TYPE_ADD, $attribute);
138    }
139
140    /**
141     * Delete values from the attribute. If no values are listed, or if all current values of the attribute are listed,
142     * the entire attribute is removed.
143     *
144     * @param Attribute|string $attribute
145     * @param string[] ...$values
146     * @return Change
147     */
148    public static function delete($attribute, ...$values): Change
149    {
150        $attribute = $attribute instanceof Attribute ? $attribute : new Attribute($attribute, ...$values);
151
152        return new self(self::TYPE_DELETE, $attribute);
153    }
154
155    /**
156     * Replace all existing values with the new values, creating the attribute if it did not already exist.  A replace
157     * with no value will delete the entire attribute if it exists, and it is ignored if the attribute does not exist.
158     *
159     * @param Attribute|string $attribute
160     * @param string[] ...$values
161     * @return Change
162     */
163    public static function replace($attribute, ...$values): Change
164    {
165        $attribute = $attribute instanceof Attribute ? $attribute : new Attribute($attribute, ...$values);
166
167        return new self(self::TYPE_REPLACE, $attribute);
168    }
169
170    /**
171     * Remove all values from an attribute, essentially un-setting/resetting it. This is the same type as delete when
172     * going to LDAP. The real difference being that no values are attached to the change.
173     *
174     * @param string|Attribute $attribute
175     * @return Change
176     */
177    public static function reset($attribute): Change
178    {
179        $attribute = $attribute instanceof Attribute ? new Attribute($attribute->getDescription()) : new Attribute($attribute);
180
181        return new self(self::TYPE_DELETE, $attribute);
182    }
183}
184