3 namespace Adldap\Models\Concerns
;
5 use Illuminate\Support\Arr
;
10 * The default output date format for all time related methods.
12 * Default format is suited for MySQL timestamps.
16 public $dateFormat = 'Y-m-d H:i:s';
19 * The format that is used to convert AD timestamps to unix timestamps.
23 protected $timestampFormat = 'YmdHis.0Z';
26 * The models attributes.
30 protected $attributes = [];
33 * The models original attributes.
37 protected $original = [];
40 * Dynamically retrieve attributes on the object.
46 public function __get($key)
48 return $this->getAttribute($key);
52 * Dynamically set attributes on the object.
59 public function __set($key, $value)
61 return $this->setAttribute($key, $value);
65 * Synchronizes the models original attributes
66 * with the model's current attributes.
70 public function syncOriginal()
72 $this->original
= $this->attributes
;
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
88 public function getAttribute($key, $subKey = null)
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.
108 public function getFirstAttribute($key)
110 return $this->getAttribute($key, 0);
114 * Returns all of the models attributes.
118 public function getAttributes()
120 return $this->attributes
;
124 * Fills the entry with the supplied attributes.
126 * @param array $attributes
130 public function fill(array $attributes = [])
132 foreach ($attributes as $key => $value) {
133 $this->setAttribute($key, $value);
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
148 public function setAttribute($key, $value, $subKey = null)
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];
162 $this->attributes
[$key][$subKey] = $value;
169 * Sets the first attributes value by the specified key.
171 * @param int|string $key
172 * @param mixed $value
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
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.
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
213 $this->exists
= true;
219 * Filters the count key recursively from raw LDAP attributes.
221 * @param array $attributes
222 * @param array|string $keys
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) :
240 * Returns true / false if the specified attribute
241 * exists in the attributes array.
243 * @param int|string $key
244 * @param int|string $subKey
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.
263 public function countAttributes()
265 return count($this->getAttributes());
269 * Returns the models original attributes.
273 public function getOriginal()
275 return $this->original
;
279 * Get the attributes that have been changed since last sync.
283 public function getDirty()
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);
299 * Returns a normalized attribute key.
305 protected function normalizeAttributeKey($key)
307 return strtolower($key);
311 * Determine if the new and old values for a given key are equivalent.
317 protected function originalIsEquivalent($key)
319 if (! array_key_exists($key, $this->original
)) {
323 $current = $this->attributes
[$key];
325 $original = $this->original
[$key];
327 if ($current === $original) {
331 return is_numeric($current) &&
332 is_numeric($original) &&
333 strcmp((string) $current, (string) $original) === 0;