PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Navigation/Page.php

http://github.com/centurion-project/Centurion
PHP | 1119 lines | 450 code | 118 blank | 551 comment | 73 complexity | 1d3f399d1a0e04dd2bf9b5f7344d33e1 MD5 | raw file
Possible License(s): BSD-3-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_Navigation
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Navigation_Container
  23. */
  24. //$1 'Zend/Navigation/Container.php';
  25. /**
  26. * Base class for Zend_Navigation_Page pages
  27. *
  28. * @category Zend
  29. * @package Zend_Navigation
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. abstract class Zend_Navigation_Page extends Zend_Navigation_Container
  34. {
  35. /**
  36. * Page label
  37. *
  38. * @var string|null
  39. */
  40. protected $_label;
  41. /**
  42. * Page id
  43. *
  44. * @var string|null
  45. */
  46. protected $_id;
  47. /**
  48. * Style class for this page (CSS)
  49. *
  50. * @var string|null
  51. */
  52. protected $_class;
  53. /**
  54. * A more descriptive title for this page
  55. *
  56. * @var string|null
  57. */
  58. protected $_title;
  59. /**
  60. * This page's target
  61. *
  62. * @var string|null
  63. */
  64. protected $_target;
  65. /**
  66. * Forward links to other pages
  67. *
  68. * @link http://www.w3.org/TR/html4/struct/links.html#h-12.3.1
  69. *
  70. * @var array
  71. */
  72. protected $_rel = array();
  73. /**
  74. * Reverse links to other pages
  75. *
  76. * @link http://www.w3.org/TR/html4/struct/links.html#h-12.3.1
  77. *
  78. * @var array
  79. */
  80. protected $_rev = array();
  81. /**
  82. * Page order used by parent container
  83. *
  84. * @var int|null
  85. */
  86. protected $_order;
  87. /**
  88. * ACL resource associated with this page
  89. *
  90. * @var string|Zend_Acl_Resource_Interface|null
  91. */
  92. protected $_resource;
  93. /**
  94. * ACL privilege associated with this page
  95. *
  96. * @var string|null
  97. */
  98. protected $_privilege;
  99. /**
  100. * Whether this page should be considered active
  101. *
  102. * @var bool
  103. */
  104. protected $_active = false;
  105. /**
  106. * Whether this page should be considered visible
  107. *
  108. * @var bool
  109. */
  110. protected $_visible = true;
  111. /**
  112. * Parent container
  113. *
  114. * @var Zend_Navigation_Container|null
  115. */
  116. protected $_parent;
  117. /**
  118. * Custom page properties, used by __set(), __get() and __isset()
  119. *
  120. * @var array
  121. */
  122. protected $_properties = array();
  123. // Initialization:
  124. /**
  125. * Factory for Zend_Navigation_Page classes
  126. *
  127. * A specific type to construct can be specified by specifying the key
  128. * 'type' in $options. If type is 'uri' or 'mvc', the type will be resolved
  129. * to Zend_Navigation_Page_Uri or Zend_Navigation_Page_Mvc. Any other value
  130. * for 'type' will be considered the full name of the class to construct.
  131. * A valid custom page class must extend Zend_Navigation_Page.
  132. *
  133. * If 'type' is not given, the type of page to construct will be determined
  134. * by the following rules:
  135. * - If $options contains either of the keys 'action', 'controller',
  136. * 'module', or 'route', a Zend_Navigation_Page_Mvc page will be created.
  137. * - If $options contains the key 'uri', a Zend_Navigation_Page_Uri page
  138. * will be created.
  139. *
  140. * @param array|Zend_Config $options options used for creating page
  141. * @return Zend_Navigation_Page a page instance
  142. * @throws Zend_Navigation_Exception if $options is not array/Zend_Config
  143. * @throws Zend_Exception if 'type' is specified and
  144. * Zend_Loader is unable to load the
  145. * class
  146. * @throws Zend_Navigation_Exception if something goes wrong during
  147. * instantiation of the page
  148. * @throws Zend_Navigation_Exception if 'type' is given, and the specified
  149. * type does not extend this class
  150. * @throws Zend_Navigation_Exception if unable to determine which class
  151. * to instantiate
  152. */
  153. public static function factory($options)
  154. {
  155. if ($options instanceof Zend_Config) {
  156. $options = $options->toArray();
  157. }
  158. if (!is_array($options)) {
  159. //$1 'Zend/Navigation/Exception.php';
  160. throw new Zend_Navigation_Exception(
  161. 'Invalid argument: $options must be an array or Zend_Config');
  162. }
  163. if (isset($options['type'])) {
  164. $type = $options['type'];
  165. if (is_string($type) && !empty($type)) {
  166. switch (strtolower($type)) {
  167. case 'mvc':
  168. $type = 'Zend_Navigation_Page_Mvc';
  169. break;
  170. case 'uri':
  171. $type = 'Zend_Navigation_Page_Uri';
  172. break;
  173. }
  174. if (!class_exists($type)) {
  175. //$1 'Zend/Loader.php';
  176. @Zend_Loader::loadClass($type);
  177. }
  178. $page = new $type($options);
  179. if (!$page instanceof Zend_Navigation_Page) {
  180. //$1 'Zend/Navigation/Exception.php';
  181. throw new Zend_Navigation_Exception(sprintf(
  182. 'Invalid argument: Detected type "%s", which ' .
  183. 'is not an instance of Zend_Navigation_Page',
  184. $type));
  185. }
  186. return $page;
  187. }
  188. }
  189. $hasUri = isset($options['uri']);
  190. $hasMvc = isset($options['action']) || isset($options['controller']) ||
  191. isset($options['module']) || isset($options['route']);
  192. if ($hasMvc) {
  193. //$1 'Zend/Navigation/Page/Mvc.php';
  194. return new Zend_Navigation_Page_Mvc($options);
  195. } elseif ($hasUri) {
  196. //$1 'Zend/Navigation/Page/Uri.php';
  197. return new Zend_Navigation_Page_Uri($options);
  198. } else {
  199. //$1 'Zend/Navigation/Exception.php';
  200. throw new Zend_Navigation_Exception(
  201. 'Invalid argument: Unable to determine class to instantiate');
  202. }
  203. }
  204. /**
  205. * Page constructor
  206. *
  207. * @param array|Zend_Config $options [optional] page options. Default is
  208. * null, which should set defaults.
  209. * @throws Zend_Navigation_Exception if invalid options are given
  210. */
  211. public function __construct($options = null)
  212. {
  213. if (is_array($options)) {
  214. $this->setOptions($options);
  215. } elseif ($options instanceof Zend_Config) {
  216. $this->setConfig($options);
  217. }
  218. // do custom initialization
  219. $this->_init();
  220. }
  221. /**
  222. * Initializes page (used by subclasses)
  223. *
  224. * @return void
  225. */
  226. protected function _init()
  227. {
  228. }
  229. /**
  230. * Sets page properties using a Zend_Config object
  231. *
  232. * @param Zend_Config $config config object to get properties from
  233. * @return Zend_Navigation_Page fluent interface, returns self
  234. * @throws Zend_Navigation_Exception if invalid options are given
  235. */
  236. public function setConfig(Zend_Config $config)
  237. {
  238. return $this->setOptions($config->toArray());
  239. }
  240. /**
  241. * Sets page properties using options from an associative array
  242. *
  243. * Each key in the array corresponds to the according set*() method, and
  244. * each word is separated by underscores, e.g. the option 'target'
  245. * corresponds to setTarget(), and the option 'reset_params' corresponds to
  246. * the method setResetParams().
  247. *
  248. * @param array $options associative array of options to set
  249. * @return Zend_Navigation_Page fluent interface, returns self
  250. * @throws Zend_Navigation_Exception if invalid options are given
  251. */
  252. public function setOptions(array $options)
  253. {
  254. foreach ($options as $key => $value) {
  255. $this->set($key, $value);
  256. }
  257. return $this;
  258. }
  259. // Accessors:
  260. /**
  261. * Sets page label
  262. *
  263. * @param string $label new page label
  264. * @return Zend_Navigation_Page fluent interface, returns self
  265. * @throws Zend_Navigation_Exception if empty/no string is given
  266. */
  267. public function setLabel($label)
  268. {
  269. if (null !== $label && !is_string($label)) {
  270. //$1 'Zend/Navigation/Exception.php';
  271. throw new Zend_Navigation_Exception(
  272. 'Invalid argument: $label must be a string or null');
  273. }
  274. $this->_label = $label;
  275. return $this;
  276. }
  277. /**
  278. * Returns page label
  279. *
  280. * @return string page label or null
  281. */
  282. public function getLabel()
  283. {
  284. return $this->_label;
  285. }
  286. /**
  287. * Sets page id
  288. *
  289. * @param string|null $id [optional] id to set. Default is null,
  290. * which sets no id.
  291. * @return Zend_Navigation_Page fluent interface, returns self
  292. * @throws Zend_Navigation_Exception if not given string or null
  293. */
  294. public function setId($id = null)
  295. {
  296. if (null !== $id && !is_string($id) && !is_numeric($id)) {
  297. //$1 'Zend/Navigation/Exception.php';
  298. throw new Zend_Navigation_Exception(
  299. 'Invalid argument: $id must be a string, number or null');
  300. }
  301. $this->_id = null === $id ? $id : (string) $id;
  302. return $this;
  303. }
  304. /**
  305. * Returns page id
  306. *
  307. * @return string|null page id or null
  308. */
  309. public function getId()
  310. {
  311. return $this->_id;
  312. }
  313. /**
  314. * Sets page CSS class
  315. *
  316. * @param string|null $class [optional] CSS class to set. Default
  317. * is null, which sets no CSS class.
  318. * @return Zend_Navigation_Page fluent interface, returns self
  319. * @throws Zend_Navigation_Exception if not given string or null
  320. */
  321. public function setClass($class = null)
  322. {
  323. if (null !== $class && !is_string($class)) {
  324. //$1 'Zend/Navigation/Exception.php';
  325. throw new Zend_Navigation_Exception(
  326. 'Invalid argument: $class must be a string or null');
  327. }
  328. $this->_class = $class;
  329. return $this;
  330. }
  331. /**
  332. * Returns page class (CSS)
  333. *
  334. * @return string|null page's CSS class or null
  335. */
  336. public function getClass()
  337. {
  338. return $this->_class;
  339. }
  340. /**
  341. * Sets page title
  342. *
  343. * @param string $title [optional] page title. Default is
  344. * null, which sets no title.
  345. * @return Zend_Navigation_Page fluent interface, returns self
  346. * @throws Zend_Navigation_Exception if not given string or null
  347. */
  348. public function setTitle($title = null)
  349. {
  350. if (null !== $title && !is_string($title)) {
  351. //$1 'Zend/Navigation/Exception.php';
  352. throw new Zend_Navigation_Exception(
  353. 'Invalid argument: $title must be a non-empty string');
  354. }
  355. $this->_title = $title;
  356. return $this;
  357. }
  358. /**
  359. * Returns page title
  360. *
  361. * @return string|null page title or null
  362. */
  363. public function getTitle()
  364. {
  365. return $this->_title;
  366. }
  367. /**
  368. * Sets page target
  369. *
  370. * @param string|null $target [optional] target to set. Default is
  371. * null, which sets no target.
  372. * @return Zend_Navigation_Page fluent interface, returns self
  373. * @throws Zend_Navigation_Exception if target is not string or null
  374. */
  375. public function setTarget($target = null)
  376. {
  377. if (null !== $target && !is_string($target)) {
  378. //$1 'Zend/Navigation/Exception.php';
  379. throw new Zend_Navigation_Exception(
  380. 'Invalid argument: $target must be a string or null');
  381. }
  382. $this->_target = $target;
  383. return $this;
  384. }
  385. /**
  386. * Returns page target
  387. *
  388. * @return string|null page target or null
  389. */
  390. public function getTarget()
  391. {
  392. return $this->_target;
  393. }
  394. /**
  395. * Sets the page's forward links to other pages
  396. *
  397. * This method expects an associative array of forward links to other pages,
  398. * where each element's key is the name of the relation (e.g. alternate,
  399. * prev, next, help, etc), and the value is a mixed value that could somehow
  400. * be considered a page.
  401. *
  402. * @param array|Zend_Config $relations [optional] an associative array of
  403. * forward links to other pages
  404. * @return Zend_Navigation_Page fluent interface, returns self
  405. */
  406. public function setRel($relations = null)
  407. {
  408. $this->_rel = array();
  409. if (null !== $relations) {
  410. if ($relations instanceof Zend_Config) {
  411. $relations = $relations->toArray();
  412. }
  413. if (!is_array($relations)) {
  414. //$1 'Zend/Navigation/Exception.php';
  415. throw new Zend_Navigation_Exception(
  416. 'Invalid argument: $relations must be an ' .
  417. 'array or an instance of Zend_Config');
  418. }
  419. foreach ($relations as $name => $relation) {
  420. if (is_string($name)) {
  421. $this->_rel[$name] = $relation;
  422. }
  423. }
  424. }
  425. return $this;
  426. }
  427. /**
  428. * Returns the page's forward links to other pages
  429. *
  430. * This method returns an associative array of forward links to other pages,
  431. * where each element's key is the name of the relation (e.g. alternate,
  432. * prev, next, help, etc), and the value is a mixed value that could somehow
  433. * be considered a page.
  434. *
  435. * @param string $relation [optional] name of relation to return. If not
  436. * given, all relations will be returned.
  437. * @return array an array of relations. If $relation is not
  438. * specified, all relations will be returned in
  439. * an associative array.
  440. */
  441. public function getRel($relation = null)
  442. {
  443. if (null !== $relation) {
  444. return isset($this->_rel[$relation]) ?
  445. $this->_rel[$relation] :
  446. null;
  447. }
  448. return $this->_rel;
  449. }
  450. /**
  451. * Sets the page's reverse links to other pages
  452. *
  453. * This method expects an associative array of reverse links to other pages,
  454. * where each element's key is the name of the relation (e.g. alternate,
  455. * prev, next, help, etc), and the value is a mixed value that could somehow
  456. * be considered a page.
  457. *
  458. * @param array|Zend_Config $relations [optional] an associative array of
  459. * reverse links to other pages
  460. * @return Zend_Navigation_Page fluent interface, returns self
  461. */
  462. public function setRev($relations = null)
  463. {
  464. $this->_rev = array();
  465. if (null !== $relations) {
  466. if ($relations instanceof Zend_Config) {
  467. $relations = $relations->toArray();
  468. }
  469. if (!is_array($relations)) {
  470. //$1 'Zend/Navigation/Exception.php';
  471. throw new Zend_Navigation_Exception(
  472. 'Invalid argument: $relations must be an ' .
  473. 'array or an instance of Zend_Config');
  474. }
  475. foreach ($relations as $name => $relation) {
  476. if (is_string($name)) {
  477. $this->_rev[$name] = $relation;
  478. }
  479. }
  480. }
  481. return $this;
  482. }
  483. /**
  484. * Returns the page's reverse links to other pages
  485. *
  486. * This method returns an associative array of forward links to other pages,
  487. * where each element's key is the name of the relation (e.g. alternate,
  488. * prev, next, help, etc), and the value is a mixed value that could somehow
  489. * be considered a page.
  490. *
  491. * @param string $relation [optional] name of relation to return. If not
  492. * given, all relations will be returned.
  493. * @return array an array of relations. If $relation is not
  494. * specified, all relations will be returned in
  495. * an associative array.
  496. */
  497. public function getRev($relation = null)
  498. {
  499. if (null !== $relation) {
  500. return isset($this->_rev[$relation]) ?
  501. $this->_rev[$relation] :
  502. null;
  503. }
  504. return $this->_rev;
  505. }
  506. /**
  507. * Sets page order to use in parent container
  508. *
  509. * @param int $order [optional] page order in container.
  510. * Default is null, which sets no
  511. * specific order.
  512. * @return Zend_Navigation_Page fluent interface, returns self
  513. * @throws Zend_Navigation_Exception if order is not integer or null
  514. */
  515. public function setOrder($order = null)
  516. {
  517. if (is_string($order)) {
  518. $temp = (int) $order;
  519. if ($temp < 0 || $temp > 0 || $order == '0') {
  520. $order = $temp;
  521. }
  522. }
  523. if (null !== $order && !is_int($order)) {
  524. //$1 'Zend/Navigation/Exception.php';
  525. throw new Zend_Navigation_Exception(
  526. 'Invalid argument: $order must be an integer or null, ' .
  527. 'or a string that casts to an integer');
  528. }
  529. $this->_order = $order;
  530. // notify parent, if any
  531. if (isset($this->_parent)) {
  532. $this->_parent->notifyOrderUpdated();
  533. }
  534. return $this;
  535. }
  536. /**
  537. * Returns page order used in parent container
  538. *
  539. * @return int|null page order or null
  540. */
  541. public function getOrder()
  542. {
  543. return $this->_order;
  544. }
  545. /**
  546. * Sets ACL resource assoicated with this page
  547. *
  548. * @param string|Zend_Acl_Resource_Interface $resource [optional] resource
  549. * to associate with
  550. * page. Default is
  551. * null, which sets no
  552. * resource.
  553. * @throws Zend_Navigation_Exception if $resource if
  554. * invalid
  555. * @return Zend_Navigation_Page fluent interface,
  556. * returns self
  557. */
  558. public function setResource($resource = null)
  559. {
  560. if (null === $resource || is_string($resource) ||
  561. $resource instanceof Zend_Acl_Resource_Interface) {
  562. $this->_resource = $resource;
  563. } else {
  564. //$1 'Zend/Navigation/Exception.php';
  565. throw new Zend_Navigation_Exception(
  566. 'Invalid argument: $resource must be null, a string, ' .
  567. ' or an instance of Zend_Acl_Resource_Interface');
  568. }
  569. return $this;
  570. }
  571. /**
  572. * Returns ACL resource assoicated with this page
  573. *
  574. * @return string|Zend_Acl_Resource_Interface|null ACL resource or null
  575. */
  576. public function getResource()
  577. {
  578. return $this->_resource;
  579. }
  580. /**
  581. * Sets ACL privilege associated with this page
  582. *
  583. * @param string|null $privilege [optional] ACL privilege to associate
  584. * with this page. Default is null, which
  585. * sets no privilege.
  586. * @return Zend_Navigation_Page fluent interface, returns self
  587. */
  588. public function setPrivilege($privilege = null)
  589. {
  590. $this->_privilege = is_string($privilege) ? $privilege : null;
  591. return $this;
  592. }
  593. /**
  594. * Returns ACL privilege associated with this page
  595. *
  596. * @return string|null ACL privilege or null
  597. */
  598. public function getPrivilege()
  599. {
  600. return $this->_privilege;
  601. }
  602. /**
  603. * Sets whether page should be considered active or not
  604. *
  605. * @param bool $active [optional] whether page should be
  606. * considered active or not. Default is true.
  607. * @return Zend_Navigation_Page fluent interface, returns self
  608. */
  609. public function setActive($active = true)
  610. {
  611. $this->_active = (bool) $active;
  612. return $this;
  613. }
  614. /**
  615. * Returns whether page should be considered active or not
  616. *
  617. * @param bool $recursive [optional] whether page should be considered
  618. * active if any child pages are active. Default is
  619. * false.
  620. * @return bool whether page should be considered active
  621. */
  622. public function isActive($recursive = false)
  623. {
  624. if (!$this->_active && $recursive) {
  625. foreach ($this->_pages as $page) {
  626. if ($page->isActive(true)) {
  627. return true;
  628. }
  629. }
  630. return false;
  631. }
  632. return $this->_active;
  633. }
  634. /**
  635. * Proxy to isActive()
  636. *
  637. * @param bool $recursive [optional] whether page should be considered
  638. * active if any child pages are active. Default
  639. * is false.
  640. * @return bool whether page should be considered active
  641. */
  642. public function getActive($recursive = false)
  643. {
  644. return $this->isActive($recursive);
  645. }
  646. /**
  647. * Sets whether the page should be visible or not
  648. *
  649. * @param bool $visible [optional] whether page should be
  650. * considered visible or not. Default is true.
  651. * @return Zend_Navigation_Page fluent interface, returns self
  652. */
  653. public function setVisible($visible = true)
  654. {
  655. $this->_visible = (bool) $visible;
  656. return $this;
  657. }
  658. /**
  659. * Returns a boolean value indicating whether the page is visible
  660. *
  661. * @param bool $recursive [optional] whether page should be considered
  662. * invisible if parent is invisible. Default is
  663. * false.
  664. * @return bool whether page should be considered visible
  665. */
  666. public function isVisible($recursive = false)
  667. {
  668. if ($recursive && isset($this->_parent) &&
  669. $this->_parent instanceof Zend_Navigation_Page) {
  670. if (!$this->_parent->isVisible(true)) {
  671. return false;
  672. }
  673. }
  674. return $this->_visible;
  675. }
  676. /**
  677. * Proxy to isVisible()
  678. *
  679. * Returns a boolean value indicating whether the page is visible
  680. *
  681. * @param bool $recursive [optional] whether page should be considered
  682. * invisible if parent is invisible. Default is
  683. * false.
  684. * @return bool whether page should be considered visible
  685. */
  686. public function getVisible($recursive = false)
  687. {
  688. return $this->isVisible($recursive);
  689. }
  690. /**
  691. * Sets parent container
  692. *
  693. * @param Zend_Navigation_Container $parent [optional] new parent to set.
  694. * Default is null which will set
  695. * no parent.
  696. * @return Zend_Navigation_Page fluent interface, returns self
  697. */
  698. public function setParent(Zend_Navigation_Container $parent = null)
  699. {
  700. if ($parent === $this) {
  701. //$1 'Zend/Navigation/Exception.php';
  702. throw new Zend_Navigation_Exception(
  703. 'A page cannot have itself as a parent');
  704. }
  705. // return if the given parent already is parent
  706. if ($parent === $this->_parent) {
  707. return $this;
  708. }
  709. // remove from old parent
  710. if (null !== $this->_parent) {
  711. $this->_parent->removePage($this);
  712. }
  713. // set new parent
  714. $this->_parent = $parent;
  715. // add to parent if page and not already a child
  716. if (null !== $this->_parent && !$this->_parent->hasPage($this, false)) {
  717. $this->_parent->addPage($this);
  718. }
  719. return $this;
  720. }
  721. /**
  722. * Returns parent container
  723. *
  724. * @return Zend_Navigation_Container|null parent container or null
  725. */
  726. public function getParent()
  727. {
  728. return $this->_parent;
  729. }
  730. /**
  731. * Sets the given property
  732. *
  733. * If the given property is native (id, class, title, etc), the matching
  734. * set method will be used. Otherwise, it will be set as a custom property.
  735. *
  736. * @param string $property property name
  737. * @param mixed $value value to set
  738. * @return Zend_Navigation_Page fluent interface, returns self
  739. * @throws Zend_Navigation_Exception if property name is invalid
  740. */
  741. public function set($property, $value)
  742. {
  743. if (!is_string($property) || empty($property)) {
  744. //$1 'Zend/Navigation/Exception.php';
  745. throw new Zend_Navigation_Exception(
  746. 'Invalid argument: $property must be a non-empty string');
  747. }
  748. $method = 'set' . self::_normalizePropertyName($property);
  749. if ($method != 'setOptions' && $method != 'setConfig' &&
  750. method_exists($this, $method)) {
  751. $this->$method($value);
  752. } else {
  753. $this->_properties[$property] = $value;
  754. }
  755. return $this;
  756. }
  757. /**
  758. * Returns the value of the given property
  759. *
  760. * If the given property is native (id, class, title, etc), the matching
  761. * get method will be used. Otherwise, it will return the matching custom
  762. * property, or null if not found.
  763. *
  764. * @param string $property property name
  765. * @return mixed the property's value or null
  766. * @throws Zend_Navigation_Exception if property name is invalid
  767. */
  768. public function get($property)
  769. {
  770. if (!is_string($property) || empty($property)) {
  771. //$1 'Zend/Navigation/Exception.php';
  772. throw new Zend_Navigation_Exception(
  773. 'Invalid argument: $property must be a non-empty string');
  774. }
  775. $method = 'get' . self::_normalizePropertyName($property);
  776. if (method_exists($this, $method)) {
  777. return $this->$method();
  778. } elseif (isset($this->_properties[$property])) {
  779. return $this->_properties[$property];
  780. }
  781. return null;
  782. }
  783. // Magic overloads:
  784. /**
  785. * Sets a custom property
  786. *
  787. * Magic overload for enabling <code>$page->propname = $value</code>.
  788. *
  789. * @param string $name property name
  790. * @param mixed $value value to set
  791. * @return void
  792. * @throws Zend_Navigation_Exception if property name is invalid
  793. */
  794. public function __set($name, $value)
  795. {
  796. $this->set($name, $value);
  797. }
  798. /**
  799. * Returns a property, or null if it doesn't exist
  800. *
  801. * Magic overload for enabling <code>$page->propname</code>.
  802. *
  803. * @param string $name property name
  804. * @return mixed property value or null
  805. * @throws Zend_Navigation_Exception if property name is invalid
  806. */
  807. public function __get($name)
  808. {
  809. return $this->get($name);
  810. }
  811. /**
  812. * Checks if a property is set
  813. *
  814. * Magic overload for enabling <code>isset($page->propname)</code>.
  815. *
  816. * Returns true if the property is native (id, class, title, etc), and
  817. * true or false if it's a custom property (depending on whether the
  818. * property actually is set).
  819. *
  820. * @param string $name property name
  821. * @return bool whether the given property exists
  822. */
  823. public function __isset($name)
  824. {
  825. $method = 'get' . self::_normalizePropertyName($name);
  826. if (method_exists($this, $method)) {
  827. return true;
  828. }
  829. return isset($this->_properties[$name]);
  830. }
  831. /**
  832. * Unsets the given custom property
  833. *
  834. * Magic overload for enabling <code>unset($page->propname)</code>.
  835. *
  836. * @param string $name property name
  837. * @return void
  838. * @throws Zend_Navigation_Exception if the property is native
  839. */
  840. public function __unset($name)
  841. {
  842. $method = 'set' . self::_normalizePropertyName($name);
  843. if (method_exists($this, $method)) {
  844. //$1 'Zend/Navigation/Exception.php';
  845. throw new Zend_Navigation_Exception(sprintf(
  846. 'Unsetting native property "%s" is not allowed',
  847. $name));
  848. }
  849. if (isset($this->_properties[$name])) {
  850. unset($this->_properties[$name]);
  851. }
  852. }
  853. /**
  854. * Returns page label
  855. *
  856. * Magic overload for enabling <code>echo $page</code>.
  857. *
  858. * @return string page label
  859. */
  860. public function __toString()
  861. {
  862. return $this->_label;
  863. }
  864. // Public methods:
  865. /**
  866. * Adds a forward relation to the page
  867. *
  868. * @param string $relation relation name (e.g. alternate, glossary,
  869. * canonical, etc)
  870. * @param mixed $value value to set for relation
  871. * @return Zend_Navigation_Page fluent interface, returns self
  872. */
  873. public function addRel($relation, $value)
  874. {
  875. if (is_string($relation)) {
  876. $this->_rel[$relation] = $value;
  877. }
  878. return $this;
  879. }
  880. /**
  881. * Adds a reverse relation to the page
  882. *
  883. * @param string $relation relation name (e.g. alternate, glossary,
  884. * canonical, etc)
  885. * @param mixed $value value to set for relation
  886. * @return Zend_Navigation_Page fluent interface, returns self
  887. */
  888. public function addRev($relation, $value)
  889. {
  890. if (is_string($relation)) {
  891. $this->_rev[$relation] = $value;
  892. }
  893. return $this;
  894. }
  895. /**
  896. * Removes a forward relation from the page
  897. *
  898. * @param string $relation name of relation to remove
  899. * @return Zend_Navigation_Page fluent interface, returns self
  900. */
  901. public function removeRel($relation)
  902. {
  903. if (isset($this->_rel[$relation])) {
  904. unset($this->_rel[$relation]);
  905. }
  906. return $this;
  907. }
  908. /**
  909. * Removes a reverse relation from the page
  910. *
  911. * @param string $relation name of relation to remove
  912. * @return Zend_Navigation_Page fluent interface, returns self
  913. */
  914. public function removeRev($relation)
  915. {
  916. if (isset($this->_rev[$relation])) {
  917. unset($this->_rev[$relation]);
  918. }
  919. return $this;
  920. }
  921. /**
  922. * Returns an array containing the defined forward relations
  923. *
  924. * @return array defined forward relations
  925. */
  926. public function getDefinedRel()
  927. {
  928. return array_keys($this->_rel);
  929. }
  930. /**
  931. * Returns an array containing the defined reverse relations
  932. *
  933. * @return array defined reverse relations
  934. */
  935. public function getDefinedRev()
  936. {
  937. return array_keys($this->_rev);
  938. }
  939. /**
  940. * Returns custom properties as an array
  941. *
  942. * @return array an array containing custom properties
  943. */
  944. public function getCustomProperties()
  945. {
  946. return $this->_properties;
  947. }
  948. /**
  949. * Returns a hash code value for the page
  950. *
  951. * @return string a hash code value for this page
  952. */
  953. public final function hashCode()
  954. {
  955. return spl_object_hash($this);
  956. }
  957. /**
  958. * Returns an array representation of the page
  959. *
  960. * @return array associative array containing all page properties
  961. */
  962. public function toArray()
  963. {
  964. return array_merge(
  965. $this->getCustomProperties(),
  966. array(
  967. 'label' => $this->getlabel(),
  968. 'id' => $this->getId(),
  969. 'class' => $this->getClass(),
  970. 'title' => $this->getTitle(),
  971. 'target' => $this->getTarget(),
  972. 'rel' => $this->getRel(),
  973. 'rev' => $this->getRev(),
  974. 'order' => $this->getOrder(),
  975. 'resource' => $this->getResource(),
  976. 'privilege' => $this->getPrivilege(),
  977. 'active' => $this->isActive(),
  978. 'visible' => $this->isVisible(),
  979. 'type' => get_class($this),
  980. 'pages' => parent::toArray()
  981. ));
  982. }
  983. // Internal methods:
  984. /**
  985. * Normalizes a property name
  986. *
  987. * @param string $property property name to normalize
  988. * @return string normalized property name
  989. */
  990. protected static function _normalizePropertyName($property)
  991. {
  992. return str_replace(' ', '', ucwords(str_replace('_', ' ', $property)));
  993. }
  994. // Abstract methods:
  995. /**
  996. * Returns href for this page
  997. *
  998. * @return string the page's href
  999. */
  1000. abstract public function getHref();
  1001. }