3 namespace Adldap\Models
;
6 * Class BatchModification
8 * A utility class to assist in the creation of LDAP
9 * batch modifications and ensure their validity.
11 * @package Adldap\Models
13 class BatchModification
16 * The array keys to be used in batch modifications.
18 const KEY_ATTRIB
= 'attrib';
19 const KEY_MODTYPE
= 'modtype';
20 const KEY_VALUES
= 'values';
23 * The original value of the attribute before modification.
27 protected $original = null;
30 * The attribute of the modification.
37 * The values of the modification.
41 protected $values = [];
44 * The modtype integer of the batch modification.
53 * @param string|null $attribute
54 * @param string|int|null $type
55 * @param array $values
57 public function __construct($attribute = null, $type = null, $values = [])
59 $this->setAttribute($attribute)
65 * Sets the original value of the attribute before modification.
67 * @param mixed $original
71 public function setOriginal($original = null)
73 $this->original
= $original;
79 * Returns the original value of the attribute before modification.
83 public function getOriginal()
85 return $this->original
;
89 * Sets the attribute of the modification.
91 * @param string $attribute
95 public function setAttribute($attribute)
97 $this->attribute
= $attribute;
103 * Returns the attribute of the modification.
107 public function getAttribute()
109 return $this->attribute
;
113 * Sets the values of the modification.
115 * @param array $values
119 public function setValues(array $values = [])
121 $this->values
= array_map(function($value) {
122 // We need to make sure all values given to a batch modification are
123 // strings, otherwise we'll receive an LDAP exception when
124 // we try to process the modification.
125 return (string) $value;
132 * Returns the values of the modification.
136 public function getValues()
138 return $this->values
;
142 * Sets the type of the modification.
148 public function setType($type)
156 * Returns the type of the modification.
160 public function getType()
166 * Determines if the batch modification
167 * is valid in its current state.
171 public function isValid()
173 return ! is_null($this->get());
177 * Builds the type of modification automatically
178 * based on the current and original values.
182 public function build()
184 $filtered = array_diff(
185 array_map('trim', $this->values
),
189 if (is_null($this->original
)) {
190 // If the original value is null, we'll assume
191 // that the attribute doesn't exist yet.
192 if (!empty($filtered)) {
193 // If the filtered array is not empty, we'll
194 // assume the developer is looking to
195 // add attributes to the model.
196 $this->setType(LDAP_MODIFY_BATCH_ADD
);
199 // If the filtered array is empty and there is no original
200 // value, then we can ignore this attribute since
201 // we can't push null values to AD.
203 if (empty($filtered)) {
204 // If there's an original value and the array is
205 // empty then we can assume the developer is
206 // looking to completely remove all values
207 // of the specified attribute.
208 $this->setType(LDAP_MODIFY_BATCH_REMOVE_ALL
);
210 // If the array isn't empty then we can assume the
211 // developer is trying to replace all attributes.
212 $this->setType(LDAP_MODIFY_BATCH_REPLACE
);
220 * Returns the built batch modification array.
224 public function get()
226 switch ($this->type
) {
227 case LDAP_MODIFY_BATCH_REMOVE_ALL
:
228 // A values key cannot be provided when
229 // a remove all type is selected.
230 return [static::KEY_ATTRIB
=> $this->attribute
, static::KEY_MODTYPE
=> $this->type
];
231 case LDAP_MODIFY_BATCH_REMOVE
:
233 case LDAP_MODIFY_BATCH_ADD
:
234 case LDAP_MODIFY_BATCH_REPLACE
:
236 static::KEY_ATTRIB
=> $this->attribute
,
237 static::KEY_MODTYPE
=> $this->type
,
238 static::KEY_VALUES
=> $this->values
,
241 // If the modtype isn't recognized, we'll return null.