composer package updates
[openemr.git] / vendor / adldap2 / adldap2 / src / Models / BatchModification.php
blob10597f7cea03c998f3282f3e3c3623f1cd74b7fb
1 <?php
3 namespace Adldap\Models;
5 /**
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
15 /**
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';
22 /**
23 * The original value of the attribute before modification.
25 * @var null
27 protected $original = null;
29 /**
30 * The attribute of the modification.
32 * @var int|string
34 protected $attribute;
36 /**
37 * The values of the modification.
39 * @var array
41 protected $values = [];
43 /**
44 * The modtype integer of the batch modification.
46 * @var int
48 protected $type;
50 /**
51 * Constructor.
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)
60 ->setType($type)
61 ->setValues($values);
64 /**
65 * Sets the original value of the attribute before modification.
67 * @param mixed $original
69 * @return $this
71 public function setOriginal($original = null)
73 $this->original = $original;
75 return $this;
78 /**
79 * Returns the original value of the attribute before modification.
81 * @return mixed
83 public function getOriginal()
85 return $this->original;
88 /**
89 * Sets the attribute of the modification.
91 * @param string $attribute
93 * @return $this
95 public function setAttribute($attribute)
97 $this->attribute = $attribute;
99 return $this;
103 * Returns the attribute of the modification.
105 * @return string
107 public function getAttribute()
109 return $this->attribute;
113 * Sets the values of the modification.
115 * @param array $values
117 * @return $this
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;
126 }, $values);
128 return $this;
132 * Returns the values of the modification.
134 * @return array
136 public function getValues()
138 return $this->values;
142 * Sets the type of the modification.
144 * @param int $type
146 * @return $this
148 public function setType($type)
150 $this->type = $type;
152 return $this;
156 * Returns the type of the modification.
158 * @return int
160 public function getType()
162 return $this->type;
166 * Determines if the batch modification
167 * is valid in its current state.
169 * @return bool
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.
180 * @return $this
182 public function build()
184 $filtered = array_diff(
185 array_map('trim', $this->values),
186 ['']
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.
202 } else {
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);
209 } else {
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);
216 return $this;
220 * Returns the built batch modification array.
222 * @return array|null
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:
232 // Fallthrough.
233 case LDAP_MODIFY_BATCH_ADD:
234 case LDAP_MODIFY_BATCH_REPLACE:
235 return [
236 static::KEY_ATTRIB => $this->attribute,
237 static::KEY_MODTYPE => $this->type,
238 static::KEY_VALUES => $this->values,
240 default:
241 // If the modtype isn't recognized, we'll return null.
242 return;