Index: library/Zend/Navigation/Container.php =================================================================== --- library/Zend/Navigation/Container.php (revision 24695) +++ library/Zend/Navigation/Container.php (working copy) @@ -274,53 +274,147 @@ } /** - * Returns a child page matching $property == $value, or null if not found + * Returns a child page matching $property == $value or + * preg_match($value, $property), or null if not found * - * @param string $property name of property to match against - * @param mixed $value value to match property against + * @param string $property name of property to match against + * @param mixed $value value to match property against + * @param bool $useRegex [optional] if true PHP's preg_match + * is used. Default is false. * @return Zend_Navigation_Page|null matching page or null */ - public function findOneBy($property, $value) - { - $iterator = new RecursiveIteratorIterator($this, - RecursiveIteratorIterator::SELF_FIRST); + public function findOneBy($property, $value, $useRegex = false) + { + $iterator = new RecursiveIteratorIterator( + $this, + RecursiveIteratorIterator::SELF_FIRST + ); foreach ($iterator as $page) { - if ($page->get($property) == $value) { - return $page; + $pageProperty = $page->get($property); + + // Rel and rev + if (is_array($pageProperty)) { + foreach ($pageProperty as $item) { + if (is_array($item)) { + // Use regex? + if (true === $useRegex) { + foreach ($item as $item2) { + if (0 !== preg_match($value, $item2)) { + return $page; + } + } + } else { + if (in_array($value, $item)) { + return $page; + } + } + } else { + // Use regex? + if (true === $useRegex) { + if (0 !== preg_match($value, $item)) { + return $page; + } + } else { + if ($item == $value) { + return $page; + } + } + } + } + + continue; } + + // Use regex? + if (true === $useRegex) { + if (preg_match($value, $pageProperty)) { + return $page; + } + } else { + if ($pageProperty == $value) { + return $page; + } + } } - + return null; } /** - * Returns all child pages matching $property == $value, or an empty array - * if no pages are found + * Returns all child pages matching $property == $value or + * preg_match($value, $property), or an empty array if no pages are found * * @param string $property name of property to match against * @param mixed $value value to match property against + * @param bool $useRegex [optional] if true PHP's preg_match is used. + * Default is false. * @return array array containing only Zend_Navigation_Page * instances */ - public function findAllBy($property, $value) - { + public function findAllBy($property, $value, $useRegex = false) + { $found = array(); - $iterator = new RecursiveIteratorIterator($this, - RecursiveIteratorIterator::SELF_FIRST); - + $iterator = new RecursiveIteratorIterator( + $this, + RecursiveIteratorIterator::SELF_FIRST + ); + foreach ($iterator as $page) { - if ($page->get($property) == $value) { - $found[] = $page; + $pageProperty = $page->get($property); + + // Rel and rev + if (is_array($pageProperty)) { + foreach ($pageProperty as $item) { + if (is_array($item)) { + // Use regex? + if (true === $useRegex) { + foreach ($item as $item2) { + if (0 !== preg_match($value, $item2)) { + $found[] = $page; + } + } + } else { + if (in_array($value, $item)) { + $found[] = $page; + } + } + } else { + // Use regex? + if (true === $useRegex) { + if (0 !== preg_match($value, $item)) { + $found[] = $page; + } + } else { + if ($item == $value) { + $found[] = $page; + } + } + } + } + + continue; } + + // Use regex? + if (true === $useRegex) { + if (0 !== preg_match($value, $pageProperty)) { + $found[] = $page; + } + } else { + if ($pageProperty == $value) { + $found[] = $page; + } + } } return $found; } /** - * Returns page(s) matching $property == $value + * Returns page(s) matching $property == $value or + * preg_match($value, $property) * * @param string $property name of property to match against * @param mixed $value value to match property against @@ -330,14 +424,16 @@ * matching pages are found. If false, null will * be returned if no matching page is found. * Default is false. + * @param bool $useRegex [optional] if true PHP's preg_match is used. + * Default is false. * @return Zend_Navigation_Page|null matching page or null */ - public function findBy($property, $value, $all = false) + public function findBy($property, $value, $all = false, $useRegex = false) { if ($all) { - return $this->findAllBy($property, $value); + return $this->findAllBy($property, $value, $useRegex); } else { - return $this->findOneBy($property, $value); + return $this->findOneBy($property, $value, $useRegex); } } @@ -346,27 +442,33 @@ * * Examples of finder calls: * - * // METHOD // SAME AS - * $nav->findByLabel('foo'); // $nav->findOneBy('label', 'foo'); - * $nav->findOneByLabel('foo'); // $nav->findOneBy('label', 'foo'); - * $nav->findAllByClass('foo'); // $nav->findAllBy('class', 'foo'); + * // METHOD // SAME AS + * $nav->findByLabel('foo'); // $nav->findOneBy('label', 'foo'); + * $nav->findByLabel('/foo/', true); // $nav->findBy('label', '/foo/', true); + * $nav->findOneByLabel('foo'); // $nav->findOneBy('label', 'foo'); + * $nav->findAllByClass('foo'); // $nav->findAllBy('class', 'foo'); * * - * @param string $method method name - * @param array $arguments method arguments - * @throws Zend_Navigation_Exception if method does not exist + * @param string $method method name + * @param array $arguments method arguments + * @return mixed Zend_Navigation|array|null matching page, array of pages + * or null + * @throws Zend_Navigation_Exception if method does not exist */ public function __call($method, $arguments) { if (@preg_match('/(find(?:One|All)?By)(.+)/', $method, $match)) { - return $this->{$match[1]}($match[2], $arguments[0]); + return $this->{$match[1]}($match[2], $arguments[0], !empty($arguments[1])); } require_once 'Zend/Navigation/Exception.php'; - throw new Zend_Navigation_Exception(sprintf( + throw new Zend_Navigation_Exception( + sprintf( 'Bad method call: Unknown method %s::%s', get_class($this), - $method)); + $method + ) + ); } /**