Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Ldap / Node / AbstractNode.php
bloba5162bc9c7e7fe8fc1eb0b2bbd5d6443d1421099
1 <?php
2 /**
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
8 */
10 namespace Zend\Ldap\Node;
12 use ArrayAccess;
13 use Countable;
14 use Zend\Ldap;
15 use Zend\Ldap\Exception;
17 /**
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',
26 'objectguid',
27 'usnchanged', 'usncreated', 'whenchanged', 'whencreated');
29 /**
30 * Holds the node's DN.
32 * @var \Zend\Ldap\Dn
34 protected $dn;
36 /**
37 * Holds the node's current data.
39 * @var array
41 protected $currentData;
43 /**
44 * Constructor.
46 * Constructor is protected to enforce the use of factory methods.
48 * @param \Zend\Ldap\Dn $dn
49 * @param array $data
50 * @param bool $fromDataSource
52 protected function __construct(Ldap\Dn $dn, array $data, $fromDataSource)
54 $this->dn = $dn;
55 $this->loadData($data, $fromDataSource);
58 /**
59 * @param array $data
60 * @param bool $fromDataSource
62 protected function loadData(array $data, $fromDataSource)
64 if (array_key_exists('dn', $data)) {
65 unset($data['dn']);
67 ksort($data, SORT_STRING);
68 $this->currentData = $data;
71 /**
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)
81 if ($ldap !== null) {
82 $data = $ldap->getEntry($this->_getDn(), array('*', '+'), true);
83 $this->loadData($data, true);
86 return $this;
89 /**
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()
98 return $this->dn;
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();
112 return $dn;
116 * Gets the DN of the current node as a string.
118 * This is an offline method.
120 * @param string $caseFold
121 * @return string
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
134 * @return array
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
147 * @return string
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
160 * @return array
162 public function getRdnArray($caseFold = null)
164 return $this->_getDn()->getRdn($caseFold);
168 * Gets the objectClass of the node
170 * @return array
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
185 * @return array
187 public function getAttributes($includeSystemAttributes = true)
189 $data = array();
190 foreach ($this->getData($includeSystemAttributes) as $name => $value) {
191 $data[$name] = $this->getAttribute($name, null);
193 return $data;
197 * Returns the DN of the current node. {@see getDnString()}
199 * @return string
201 public function toString()
203 return $this->getDnString();
207 * Cast to string representation {@see toString()}
209 * @return string
211 public function __toString()
213 return $this->toString();
217 * Returns an array representation of the current node
219 * @param bool $includeSystemAttributes
220 * @return array
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
232 * @return string
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
247 * @return array
249 public function getData($includeSystemAttributes = true)
251 if ($includeSystemAttributes === false) {
252 $data = array();
253 foreach ($this->currentData as $key => $value) {
254 if (!in_array($key, static::$systemAttributes)) {
255 $data[$key] = $value;
258 return $data;
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
275 * @return bool
277 public function existsAttribute($name, $emptyExists = false)
279 $name = strtolower($name);
280 if (isset($this->currentData[$name])) {
281 if ($emptyExists) {
282 return true;
285 return count($this->currentData[$name]) > 0;
288 return false;
292 * Checks if the given value(s) exist in the attribute
294 * @param string $attribName
295 * @param mixed|array $value
296 * @return bool
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
309 * @param int $index
310 * @return mixed
311 * @throws \Zend\Ldap\Exception\LdapException
313 public function getAttribute($name, $index = null)
315 if ($name == 'dn') {
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
328 * @param int $index
329 * @return array|int
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
357 * @return mixed
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.
372 * @param $name
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
386 * @return bool
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
400 * @param $value
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
417 * @return mixed
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.
433 * @param $name
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
448 * @return bool
450 public function offsetExists($name)
452 return $this->existsAttribute($name, false);
456 * Returns the number of attributes in node.
457 * Implements Countable
459 * @return int
461 public function count()
463 return count($this->currentData);