composer package updates
[openemr.git] / vendor / adldap2 / adldap2 / src / Models / Concerns / HasAttributes.php
blob06bbdf84a9fe87277f2163bb17a4638e9619b926
1 <?php
3 namespace Adldap\Models\Concerns;
5 use Illuminate\Support\Arr;
7 trait HasAttributes
9 /**
10 * The default output date format for all time related methods.
12 * Default format is suited for MySQL timestamps.
14 * @var string
16 public $dateFormat = 'Y-m-d H:i:s';
18 /**
19 * The format that is used to convert AD timestamps to unix timestamps.
21 * @var string
23 protected $timestampFormat = 'YmdHis.0Z';
25 /**
26 * The models attributes.
28 * @var array
30 protected $attributes = [];
32 /**
33 * The models original attributes.
35 * @var array
37 protected $original = [];
39 /**
40 * Dynamically retrieve attributes on the object.
42 * @param mixed $key
44 * @return bool
46 public function __get($key)
48 return $this->getAttribute($key);
51 /**
52 * Dynamically set attributes on the object.
54 * @param mixed $key
55 * @param mixed $value
57 * @return $this
59 public function __set($key, $value)
61 return $this->setAttribute($key, $value);
64 /**
65 * Synchronizes the models original attributes
66 * with the model's current attributes.
68 * @return $this
70 public function syncOriginal()
72 $this->original = $this->attributes;
74 return $this;
77 /**
78 * Returns the models attribute with the specified key.
80 * If a sub-key is specified, it will try and
81 * retrieve it from the parent keys array.
83 * @param int|string $key
84 * @param int|string $subKey
86 * @return mixed
88 public function getAttribute($key, $subKey = null)
90 if (! $key) {
91 return;
94 if (is_null($subKey) && $this->hasAttribute($key)) {
95 return $this->attributes[$key];
96 } elseif ($this->hasAttribute($key, $subKey)) {
97 return $this->attributes[$key][$subKey];
102 * Returns the first attribute by the specified key.
104 * @param string $key
106 * @return mixed
108 public function getFirstAttribute($key)
110 return $this->getAttribute($key, 0);
114 * Returns all of the models attributes.
116 * @return array
118 public function getAttributes()
120 return $this->attributes;
124 * Fills the entry with the supplied attributes.
126 * @param array $attributes
128 * @return $this
130 public function fill(array $attributes = [])
132 foreach ($attributes as $key => $value) {
133 $this->setAttribute($key, $value);
136 return $this;
140 * Sets an attributes value by the specified key and sub-key.
142 * @param int|string $key
143 * @param mixed $value
144 * @param int|string $subKey
146 * @return $this
148 public function setAttribute($key, $value, $subKey = null)
150 // Normalize key.
151 $key = $this->normalizeAttributeKey($key);
153 // If the key is equal to 'dn', we'll automatically
154 // change it to the full attribute name.
155 $key = ($key == 'dn' ? $this->schema->distinguishedName() : $key);
157 if (is_null($subKey)) {
158 // We need to ensure all attributes are set as arrays so all
159 // of our model methods retrieve attributes correctly.
160 $this->attributes[$key] = is_array($value) ? $value : [$value];
161 } else {
162 $this->attributes[$key][$subKey] = $value;
165 return $this;
169 * Sets the first attributes value by the specified key.
171 * @param int|string $key
172 * @param mixed $value
174 * @return $this
176 public function setFirstAttribute($key, $value)
178 return $this->setAttribute($key, $value, 0);
182 * Sets the attributes property.
184 * Used when constructing an existing LDAP record.
186 * @param array $attributes
188 * @return $this
190 public function setRawAttributes(array $attributes = [])
192 $this->attributes = $this->filterRawAttributes($attributes);
194 // We'll pull out the distinguished name from our raw attributes
195 // and set it into our attributes array with the full attribute
196 // definition. This allows us to normalize distinguished
197 // names across different LDAP variants.
198 if ($dn = Arr::get($attributes, 'dn')) {
199 // In some LDAP variants, the distinguished
200 // name is returned as an array.
201 if (is_array($dn)) {
202 $dn = Arr::first($dn);
205 $this->setDistinguishedName($dn);
208 $this->syncOriginal();
210 // Set exists to true since raw attributes are only
211 // set in the case of attributes being loaded by
212 // query results.
213 $this->exists = true;
215 return $this;
219 * Filters the count key recursively from raw LDAP attributes.
221 * @param array $attributes
222 * @param array|string $keys
224 * @return array
226 public function filterRawAttributes(array $attributes = [], $keys = ['count', 'dn'])
228 $attributes = Arr::except($attributes, $keys);
230 array_walk($attributes, function (&$value) use ($keys) {
231 $value = is_array($value) ?
232 $this->filterRawAttributes($value, $keys) :
233 $value;
236 return $attributes;
240 * Returns true / false if the specified attribute
241 * exists in the attributes array.
243 * @param int|string $key
244 * @param int|string $subKey
246 * @return bool
248 public function hasAttribute($key, $subKey = null)
250 if (is_null($subKey)) {
251 return Arr::has($this->attributes, $key);
254 return Arr::has($this->attributes, "$key.$subKey");
258 * Returns the number of attributes inside
259 * the attributes property.
261 * @return int
263 public function countAttributes()
265 return count($this->getAttributes());
269 * Returns the models original attributes.
271 * @return array
273 public function getOriginal()
275 return $this->original;
279 * Get the attributes that have been changed since last sync.
281 * @return array
283 public function getDirty()
285 $dirty = [];
287 foreach ($this->attributes as $key => $value) {
288 if (! $this->originalIsEquivalent($key)) {
289 // We need to set reset the array's indices using array_values due to
290 // LDAP requiring consecutive indices (0, 1, 2 etc.)
291 $dirty[$key] = array_values($value);
295 return $dirty;
299 * Returns a normalized attribute key.
301 * @param string $key
303 * @return string
305 protected function normalizeAttributeKey($key)
307 return strtolower($key);
311 * Determine if the new and old values for a given key are equivalent.
313 * @param string $key
315 * @return bool
317 protected function originalIsEquivalent($key)
319 if (! array_key_exists($key, $this->original)) {
320 return false;
323 $current = $this->attributes[$key];
325 $original = $this->original[$key];
327 if ($current === $original) {
328 return true;
331 return is_numeric($current) &&
332 is_numeric($original) &&
333 strcmp((string) $current, (string) $original) === 0;