PageRenderTime 29ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/zf2/vendor/Zend/Ldap/Node/AbstractNode.php

http://github.com/eryx/php-framework-benchmark
PHP | 486 lines | 164 code | 33 blank | 289 comment | 11 complexity | be86eafa64732d1193407103dcf4c0fb MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Ldap
  17. * @subpackage Node
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Ldap\Node;
  25. use Zend\Ldap;
  26. /**
  27. * Zend_Ldap_Node_Abstract provides a bas eimplementation for LDAP nodes
  28. *
  29. * @uses ArrayAccess
  30. * @uses BadMethodCallException
  31. * @uses Countable
  32. * @uses \Zend\Ldap\Attribute
  33. * @uses \Zend\Ldap\Dn
  34. * @category Zend
  35. * @package Zend_Ldap
  36. * @subpackage Node
  37. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. abstract class AbstractNode implements \ArrayAccess, \Countable
  41. {
  42. protected static $_systemAttributes=array('createtimestamp', 'creatorsname',
  43. 'entrycsn', 'entrydn', 'entryuuid', 'hassubordinates', 'modifiersname',
  44. 'modifytimestamp', 'structuralobjectclass', 'subschemasubentry',
  45. 'distinguishedname', 'instancetype', 'name', 'objectcategory', 'objectguid',
  46. 'usnchanged', 'usncreated', 'whenchanged', 'whencreated');
  47. /**
  48. * Holds the node's DN.
  49. *
  50. * @var \Zend\Ldap\Dn
  51. */
  52. protected $_dn;
  53. /**
  54. * Holds the node's current data.
  55. *
  56. * @var array
  57. */
  58. protected $_currentData;
  59. /**
  60. * Constructor.
  61. *
  62. * Constructor is protected to enforce the use of factory methods.
  63. *
  64. * @param \Zend\Ldap\Dn $dn
  65. * @param array $data
  66. * @param boolean $fromDataSource
  67. */
  68. protected function __construct(Ldap\Dn $dn, array $data, $fromDataSource)
  69. {
  70. $this->_dn = $dn;
  71. $this->_loadData($data, $fromDataSource);
  72. }
  73. /**
  74. * @param array $data
  75. * @param boolean $fromDataSource
  76. * @throws \Zend\Ldap\Exception
  77. */
  78. protected function _loadData(array $data, $fromDataSource)
  79. {
  80. if (array_key_exists('dn', $data)) {
  81. unset($data['dn']);
  82. }
  83. ksort($data, SORT_STRING);
  84. $this->_currentData = $data;
  85. }
  86. /**
  87. * Reload node attributes from LDAP.
  88. *
  89. * This is an online method.
  90. *
  91. * @param \Zend\Ldap\Ldap $ldap
  92. * @return \Zend\Ldap\Node\AbstractNode Provides a fluid interface
  93. * @throws \Zend\Ldap\Exception
  94. */
  95. public function reload(Ldap\Ldap $ldap = null)
  96. {
  97. if ($ldap !== null) {
  98. $data = $ldap->getEntry($this->_getDn(), array('*', '+'), true);
  99. $this->_loadData($data, true);
  100. }
  101. return $this;
  102. }
  103. /**
  104. * Gets the DN of the current node as a Zend_Ldap_Dn.
  105. *
  106. * This is an offline method.
  107. *
  108. * @return \Zend\Ldap\Dn
  109. */
  110. protected function _getDn()
  111. {
  112. return $this->_dn;
  113. }
  114. /**
  115. * Gets the DN of the current node as a Zend_Ldap_Dn.
  116. * The method returns a clone of the node's DN to prohibit modification.
  117. *
  118. * This is an offline method.
  119. *
  120. * @return \Zend\Ldap\Dn
  121. */
  122. public function getDn()
  123. {
  124. $dn = clone $this->_getDn();
  125. return $dn;
  126. }
  127. /**
  128. * Gets the DN of the current node as a string.
  129. *
  130. * This is an offline method.
  131. *
  132. * @param string $caseFold
  133. * @return string
  134. */
  135. public function getDnString($caseFold = null)
  136. {
  137. return $this->_getDn()->toString($caseFold);
  138. }
  139. /**
  140. * Gets the DN of the current node as an array.
  141. *
  142. * This is an offline method.
  143. *
  144. * @param string $caseFold
  145. * @return array
  146. */
  147. public function getDnArray($caseFold = null)
  148. {
  149. return $this->_getDn()->toArray($caseFold);
  150. }
  151. /**
  152. * Gets the RDN of the current node as a string.
  153. *
  154. * This is an offline method.
  155. *
  156. * @param string $caseFold
  157. * @return string
  158. */
  159. public function getRdnString($caseFold = null)
  160. {
  161. return $this->_getDn()->getRdnString($caseFold);
  162. }
  163. /**
  164. * Gets the RDN of the current node as an array.
  165. *
  166. * This is an offline method.
  167. *
  168. * @param string $caseFold
  169. * @return array
  170. */
  171. public function getRdnArray($caseFold = null)
  172. {
  173. return $this->_getDn()->getRdn($caseFold);
  174. }
  175. /**
  176. * Gets the objectClass of the node
  177. *
  178. * @return array
  179. */
  180. public function getObjectClass()
  181. {
  182. return $this->getAttribute('objectClass', null);
  183. }
  184. /**
  185. * Gets all attributes of node.
  186. *
  187. * The collection contains all attributes.
  188. *
  189. * This is an offline method.
  190. *
  191. * @param boolean $includeSystemAttributes
  192. * @return array
  193. */
  194. public function getAttributes($includeSystemAttributes = true)
  195. {
  196. $data = array();
  197. foreach ($this->getData($includeSystemAttributes) as $name => $value) {
  198. $data[$name] = $this->getAttribute($name, null);
  199. }
  200. return $data;
  201. }
  202. /**
  203. * Returns the DN of the current node. {@see getDnString()}
  204. *
  205. * @return string
  206. */
  207. public function toString()
  208. {
  209. return $this->getDnString();
  210. }
  211. /**
  212. * Cast to string representation {@see toString()}
  213. *
  214. * @return string
  215. */
  216. public function __toString()
  217. {
  218. return $this->toString();
  219. }
  220. /**
  221. * Returns an array representation of the current node
  222. *
  223. * @param boolean $includeSystemAttributes
  224. * @return array
  225. */
  226. public function toArray($includeSystemAttributes = true)
  227. {
  228. $attributes = $this->getAttributes($includeSystemAttributes);
  229. return array_merge(array('dn' => $this->getDnString()), $attributes);
  230. }
  231. /**
  232. * Returns a JSON representation of the current node
  233. *
  234. * @param boolean $includeSystemAttributes
  235. * @return string
  236. */
  237. public function toJson($includeSystemAttributes = true)
  238. {
  239. return json_encode($this->toArray($includeSystemAttributes));
  240. }
  241. /**
  242. * Gets node attributes.
  243. *
  244. * The array contains all attributes in its internal format (no conversion).
  245. *
  246. * This is an offline method.
  247. *
  248. * @param boolean $includeSystemAttributes
  249. * @return array
  250. */
  251. public function getData($includeSystemAttributes = true)
  252. {
  253. if ($includeSystemAttributes === false) {
  254. $data = array();
  255. foreach ($this->_currentData as $key => $value) {
  256. if (!in_array($key, self::$_systemAttributes)) {
  257. $data[$key] = $value;
  258. }
  259. }
  260. return $data;
  261. } else {
  262. return $this->_currentData;
  263. }
  264. }
  265. /**
  266. * Checks whether a given attribute exists.
  267. *
  268. * If $emptyExists is false empty attributes (containing only array()) are
  269. * treated as non-existent returning false.
  270. * If $emptyExists is true empty attributes are treated as existent returning
  271. * true. In this case method returns false only if the attribute name is
  272. * missing in the key-collection.
  273. *
  274. * @param string $name
  275. * @param boolean $emptyExists
  276. * @return boolean
  277. */
  278. public function existsAttribute($name, $emptyExists = false)
  279. {
  280. $name = strtolower($name);
  281. if (isset($this->_currentData[$name])) {
  282. if ($emptyExists) return true;
  283. return count($this->_currentData[$name])>0;
  284. }
  285. else return false;
  286. }
  287. /**
  288. * Checks if the given value(s) exist in the attribute
  289. *
  290. * @param string $attribName
  291. * @param mixed|array $value
  292. * @return boolean
  293. */
  294. public function attributeHasValue($attribName, $value)
  295. {
  296. return Ldap\Attribute::attributeHasValue($this->_currentData, $attribName, $value);
  297. }
  298. /**
  299. * Gets a LDAP attribute.
  300. *
  301. * This is an offline method.
  302. *
  303. * @param string $name
  304. * @param integer $index
  305. * @return mixed
  306. * @throws \Zend\Ldap\Exception
  307. */
  308. public function getAttribute($name, $index = null)
  309. {
  310. if ($name == 'dn') {
  311. return $this->getDnString();
  312. }
  313. else {
  314. return Ldap\Attribute::getAttribute($this->_currentData, $name, $index);
  315. }
  316. }
  317. /**
  318. * Gets a LDAP date/time attribute.
  319. *
  320. * This is an offline method.
  321. *
  322. * @param string $name
  323. * @param integer $index
  324. * @return array|integer
  325. * @throws \Zend\Ldap\Exception
  326. */
  327. public function getDateTimeAttribute($name, $index = null)
  328. {
  329. return Ldap\Attribute::getDateTimeAttribute($this->_currentData, $name, $index);
  330. }
  331. /**
  332. * Sets a LDAP attribute.
  333. *
  334. * This is an offline method.
  335. *
  336. * @param string $name
  337. * @param mixed $value
  338. * @return null
  339. * @throws BadMethodCallException
  340. */
  341. public function __set($name, $value)
  342. {
  343. throw new \BadMethodCallException();
  344. }
  345. /**
  346. * Gets a LDAP attribute.
  347. *
  348. * This is an offline method.
  349. *
  350. * @param string $name
  351. * @return array
  352. * @throws \Zend\Ldap\Exception
  353. */
  354. public function __get($name)
  355. {
  356. return $this->getAttribute($name, null);
  357. }
  358. /**
  359. * Deletes a LDAP attribute.
  360. *
  361. * This method deletes the attribute.
  362. *
  363. * This is an offline method.
  364. *
  365. * @param string $name
  366. * @return null
  367. * @throws BadMethodCallException
  368. */
  369. public function __unset($name)
  370. {
  371. throw new \BadMethodCallException();
  372. }
  373. /**
  374. * Checks whether a given attribute exists.
  375. *
  376. * Empty attributes will be treated as non-existent.
  377. *
  378. * @param string $name
  379. * @return boolean
  380. */
  381. public function __isset($name)
  382. {
  383. return $this->existsAttribute($name, false);
  384. }
  385. /**
  386. * Sets a LDAP attribute.
  387. * Implements ArrayAccess.
  388. *
  389. * This is an offline method.
  390. *
  391. * @param string $name
  392. * @param mixed $value
  393. * @return null
  394. * @throws BadMethodCallException
  395. */
  396. public function offsetSet($name, $value)
  397. {
  398. throw new \BadMethodCallException();
  399. }
  400. /**
  401. * Gets a LDAP attribute.
  402. * Implements ArrayAccess.
  403. *
  404. * This is an offline method.
  405. *
  406. * @param string $name
  407. * @return array
  408. * @throws \Zend\Ldap\Exception
  409. */
  410. public function offsetGet($name)
  411. {
  412. return $this->getAttribute($name, null);
  413. }
  414. /**
  415. * Deletes a LDAP attribute.
  416. * Implements ArrayAccess.
  417. *
  418. * This method deletes the attribute.
  419. *
  420. * This is an offline method.
  421. *
  422. * @param string $name
  423. * @return null
  424. * @throws BadMethodCallException
  425. */
  426. public function offsetUnset($name)
  427. {
  428. throw new \BadMethodCallException();
  429. }
  430. /**
  431. * Checks whether a given attribute exists.
  432. * Implements ArrayAccess.
  433. *
  434. * Empty attributes will be treated as non-existent.
  435. *
  436. * @param string $name
  437. * @return boolean
  438. */
  439. public function offsetExists($name)
  440. {
  441. return $this->existsAttribute($name, false);
  442. }
  443. /**
  444. * Returns the number of attributes in node.
  445. * Implements Countable
  446. *
  447. * @return int
  448. */
  449. public function count()
  450. {
  451. return count($this->_currentData);
  452. }
  453. }