public function findTermByName(TaxonomyInterface $taxonomy, array $ancestors)
 {
     if (!count($ancestors)) {
         throw new Exception\RuntimeException('Ancestors are empty');
     }
     $terms = $taxonomy->getChildren();
     $ancestorsFound = 0;
     $found = false;
     foreach ($ancestors as &$element) {
         if (is_string($element) && strlen($element) > 0) {
             $element = strtolower($element);
             foreach ($terms as $term) {
                 $found = false;
                 if (strtolower($term->getName()) == strtolower($element)) {
                     $terms = $term->getChildren();
                     $found = $term;
                     $ancestorsFound++;
                     break;
                 }
             }
             if (!is_object($found)) {
                 break;
             }
         }
     }
     if (!is_object($found)) {
         throw new Exception\TermNotFoundException(sprintf('Could not find term with acestors: %s', implode(',', $ancestors)));
     }
     if ($ancestorsFound != count($ancestors)) {
         throw new Exception\TermNotFoundException(sprintf('Could not find term with acestors: %s. Ancestor ratio %s:%s does not equal 1:1', implode(',', $ancestors), $ancestorsFound, count($ancestors)));
     }
     return $found;
 }
Example #2
0
 /**
  * @param TaxonomyInterface|TaxonomyTermInterface|string $nameOrObject
  * @throws Exception\InvalidArgumentException
  * @return \Taxonomy\Options\TaxonomyOptions
  */
 public function getOptions($nameOrObject)
 {
     if ($nameOrObject instanceof TaxonomyInterface) {
         $name = $nameOrObject->getName();
     } elseif ($nameOrObject instanceof TaxonomyTermInterface) {
         $name = $nameOrObject->getTaxonomy()->getName();
     } elseif (is_string($nameOrObject)) {
         $name = $nameOrObject;
     } else {
         throw new Exception\InvalidArgumentException(sprintf('Expected $nameOrObject to be TaxonomyInterface, TaxonomyTermInterface or string but got "%s"', is_object($nameOrObject) ? get_class($nameOrObject) : gettype($nameOrObject)));
     }
     return $this->moduleOptions->getType($name);
 }