3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Ldap\Node
;
15 use Zend\Ldap\Exception
;
18 * This class provides a base implementation for LDAP nodes
20 abstract class AbstractNode
implements ArrayAccess
, Countable
22 protected static $systemAttributes = array('createtimestamp', 'creatorsname',
23 'entrycsn', 'entrydn', 'entryuuid', 'hassubordinates', 'modifiersname',
24 'modifytimestamp', 'structuralobjectclass', 'subschemasubentry',
25 'distinguishedname', 'instancetype', 'name', 'objectcategory',
27 'usnchanged', 'usncreated', 'whenchanged', 'whencreated');
30 * Holds the node's DN.
37 * Holds the node's current data.
41 protected $currentData;
46 * Constructor is protected to enforce the use of factory methods.
48 * @param \Zend\Ldap\Dn $dn
50 * @param bool $fromDataSource
52 protected function __construct(Ldap\Dn
$dn, array $data, $fromDataSource)
55 $this->loadData($data, $fromDataSource);
60 * @param bool $fromDataSource
62 protected function loadData(array $data, $fromDataSource)
64 if (array_key_exists('dn', $data)) {
67 ksort($data, SORT_STRING
);
68 $this->currentData
= $data;
72 * Reload node attributes from LDAP.
74 * This is an online method.
76 * @param \Zend\Ldap\Ldap $ldap
77 * @return AbstractNode Provides a fluid interface
79 public function reload(Ldap\Ldap
$ldap = null)
82 $data = $ldap->getEntry($this->_getDn(), array('*', '+'), true);
83 $this->loadData($data, true);
90 * Gets the DN of the current node as a Zend\Ldap\Dn.
92 * This is an offline method.
94 * @return \Zend\Ldap\Dn
96 protected function _getDn()
102 * Gets the DN of the current node as a Zend\Ldap\Dn.
103 * The method returns a clone of the node's DN to prohibit modification.
105 * This is an offline method.
107 * @return \Zend\Ldap\Dn
109 public function getDn()
111 $dn = clone $this->_getDn();
116 * Gets the DN of the current node as a string.
118 * This is an offline method.
120 * @param string $caseFold
123 public function getDnString($caseFold = null)
125 return $this->_getDn()->toString($caseFold);
129 * Gets the DN of the current node as an array.
131 * This is an offline method.
133 * @param string $caseFold
136 public function getDnArray($caseFold = null)
138 return $this->_getDn()->toArray($caseFold);
142 * Gets the RDN of the current node as a string.
144 * This is an offline method.
146 * @param string $caseFold
149 public function getRdnString($caseFold = null)
151 return $this->_getDn()->getRdnString($caseFold);
155 * Gets the RDN of the current node as an array.
157 * This is an offline method.
159 * @param string $caseFold
162 public function getRdnArray($caseFold = null)
164 return $this->_getDn()->getRdn($caseFold);
168 * Gets the objectClass of the node
172 public function getObjectClass()
174 return $this->getAttribute('objectClass', null);
178 * Gets all attributes of node.
180 * The collection contains all attributes.
182 * This is an offline method.
184 * @param bool $includeSystemAttributes
187 public function getAttributes($includeSystemAttributes = true)
190 foreach ($this->getData($includeSystemAttributes) as $name => $value) {
191 $data[$name] = $this->getAttribute($name, null);
197 * Returns the DN of the current node. {@see getDnString()}
201 public function toString()
203 return $this->getDnString();
207 * Cast to string representation {@see toString()}
211 public function __toString()
213 return $this->toString();
217 * Returns an array representation of the current node
219 * @param bool $includeSystemAttributes
222 public function toArray($includeSystemAttributes = true)
224 $attributes = $this->getAttributes($includeSystemAttributes);
225 return array_merge(array('dn' => $this->getDnString()), $attributes);
229 * Returns a JSON representation of the current node
231 * @param bool $includeSystemAttributes
234 public function toJson($includeSystemAttributes = true)
236 return json_encode($this->toArray($includeSystemAttributes));
240 * Gets node attributes.
242 * The array contains all attributes in its internal format (no conversion).
244 * This is an offline method.
246 * @param bool $includeSystemAttributes
249 public function getData($includeSystemAttributes = true)
251 if ($includeSystemAttributes === false) {
253 foreach ($this->currentData
as $key => $value) {
254 if (!in_array($key, static::$systemAttributes)) {
255 $data[$key] = $value;
261 return $this->currentData
;
265 * Checks whether a given attribute exists.
267 * If $emptyExists is false empty attributes (containing only array()) are
268 * treated as non-existent returning false.
269 * If $emptyExists is true empty attributes are treated as existent returning
270 * true. In this case method returns false only if the attribute name is
271 * missing in the key-collection.
273 * @param string $name
274 * @param bool $emptyExists
277 public function existsAttribute($name, $emptyExists = false)
279 $name = strtolower($name);
280 if (isset($this->currentData
[$name])) {
285 return count($this->currentData
[$name]) > 0;
292 * Checks if the given value(s) exist in the attribute
294 * @param string $attribName
295 * @param mixed|array $value
298 public function attributeHasValue($attribName, $value)
300 return Ldap\Attribute
::attributeHasValue($this->currentData
, $attribName, $value);
304 * Gets a LDAP attribute.
306 * This is an offline method.
308 * @param string $name
311 * @throws \Zend\Ldap\Exception\LdapException
313 public function getAttribute($name, $index = null)
316 return $this->getDnString();
319 return Ldap\Attribute
::getAttribute($this->currentData
, $name, $index);
323 * Gets a LDAP date/time attribute.
325 * This is an offline method.
327 * @param string $name
330 * @throws \Zend\Ldap\Exception\LdapException
332 public function getDateTimeAttribute($name, $index = null)
334 return Ldap\Attribute
::getDateTimeAttribute($this->currentData
, $name, $index);
338 * Sets a LDAP attribute.
340 * This is an offline method.
342 * @param string $name
343 * @param mixed $value
344 * @throws \Zend\Ldap\Exception\BadMethodCallException
346 public function __set($name, $value)
348 throw new Exception\
BadMethodCallException();
352 * Gets a LDAP attribute.
354 * This is an offline method.
356 * @param string $name
358 * @throws \Zend\Ldap\Exception\LdapException
360 public function __get($name)
362 return $this->getAttribute($name, null);
366 * Deletes a LDAP attribute.
368 * This method deletes the attribute.
370 * This is an offline method.
373 * @throws \Zend\Ldap\Exception\BadMethodCallException
375 public function __unset($name)
377 throw new Exception\
BadMethodCallException();
381 * Checks whether a given attribute exists.
383 * Empty attributes will be treated as non-existent.
385 * @param string $name
388 public function __isset($name)
390 return $this->existsAttribute($name, false);
394 * Sets a LDAP attribute.
395 * Implements ArrayAccess.
397 * This is an offline method.
399 * @param string $name
401 * @throws \Zend\Ldap\Exception\BadMethodCallException
402 * @param mixed $value
403 * @throws \Zend\Ldap\Exception\BadMethodCallException
405 public function offsetSet($name, $value)
407 throw new Exception\
BadMethodCallException();
411 * Gets a LDAP attribute.
412 * Implements ArrayAccess.
414 * This is an offline method.
416 * @param string $name
418 * @throws \Zend\Ldap\Exception\LdapException
420 public function offsetGet($name)
422 return $this->getAttribute($name, null);
426 * Deletes a LDAP attribute.
427 * Implements ArrayAccess.
429 * This method deletes the attribute.
431 * This is an offline method.
434 * @throws \Zend\Ldap\Exception\BadMethodCallException
436 public function offsetUnset($name)
438 throw new Exception\
BadMethodCallException();
442 * Checks whether a given attribute exists.
443 * Implements ArrayAccess.
445 * Empty attributes will be treated as non-existent.
447 * @param string $name
450 public function offsetExists($name)
452 return $this->existsAttribute($name, false);
456 * Returns the number of attributes in node.
457 * Implements Countable
461 public function count()
463 return count($this->currentData
);