/** * (Partially) hydrates the object and removes the affected (key, value) pairs from the return set. * * @param object $object * @param array $data * @return array */ public function hydrate($object, array $data) { if (!$object instanceof TaxonomyTermAwareInterface) { return $data; } $metadata = $this->objectManager->getClassMetadata(get_class($object)); foreach ($data as $field => $value) { if ($metadata->hasAssociation($field) && is_object($value)) { $target = $metadata->getAssociationTargetClass($field); if ($target == 'Taxonomy\\Entity\\TaxonomyTerm') { $this->taxonomyManager->associateWith($value, $object); unset($data[$field]); } } } return $data; }
/** * @param TaxonomyTermInterface|TaxonomyInterface $object * @return TaxonomyTermInterface * @throws \Taxonomy\Exception\InvalidArgumentException */ public function getAllowedChildren($object) { if ($object instanceof TaxonomyInterface) { $name = $object->getName(); } elseif ($object instanceof TaxonomyTermInterface) { $name = $object->getTaxonomy()->getName(); } else { throw new Exception\InvalidArgumentException(sprintf('Expected $nameOrObject to be TaxonomyInterface, TaxonomyTermInterface or string but got "%s"', is_object($object) ? get_class($object) : gettype($object))); } $taxonomies = []; $children = $this->moduleOptions->getType($name)->getAllowedChildren(); $instance = $object->getInstance(); foreach ($children as $child) { $taxonomies[] = $this->taxonomyManager->findTaxonomyByName($child, $instance); } return $taxonomies; }
protected function refreshTerms(AdapterInterface $console, $percentile) { $filter = new NotTrashedCollectionFilter(); $terms = $this->taxonomyManager->findAllTerms(true); $terms = $filter->filter($terms); foreach ($terms as $term) { if (rand(0, 100) > $percentile) { continue; } $instance = $term->getInstance(); $normalizer = $this->normalizer->normalize($term); $routeName = $normalizer->getRouteName(); $routeParams = $normalizer->getRouteParams(); $router = $this->aliasManager->getRouter(); $url = $router->assemble($routeParams, ['name' => $routeName]); $alias = $this->aliasManager->autoAlias('taxonomyTerm', $url, $term, $instance); $console->writeLine('Updated taxonomy term ' . $term->getName() . ' (' . $term->getId() . '): ' . $alias->getAlias()); } }
/** * Returns true if and only if $value meets the validation requirements * If $value fails validation, then this method returns false, and * getMessages() will return an array of messages that explain why the * validation failed. * * @param mixed $value * @return bool * @throws Exception\RuntimeException If validation of $value is impossible */ public function isValid($value) { try { $target = $this->target; if ($target instanceof FormInterface) { $target = $target->getObject(); if (!$target instanceof TaxonomyTermAwareInterface) { throw new Exception\RuntimeException(sprintf('Target supplied by FormInterface is not of type TaxonomyTermAwareInterface', is_object($target) ? get_class($target) : gettype($target))); } } $result = $this->taxonomyManager->isAssociableWith($value, $target); if ($result) { return true; } $this->error(self::NOT_ALLOWED); return false; } catch (UnauthorizedException $e) { $this->error(self::NOT_AUTHORIZED); } catch (\Exception $e) { throw new Exception\RuntimeException($e->getMessage()); } throw new Exception\RuntimeException("Validation failed"); }
/** * @param array $data * @return array * @throws \Taxonomy\Exception\RuntimeException */ protected function validate(array $data) { $taxonomy = $data['taxonomy']; $parent = isset($data['parent']) ? $data['parent'] : null; if (!is_object($taxonomy)) { $taxonomy = $data['taxonomy'] = $this->taxonomyManager->getTaxonomy($taxonomy); } if ($parent && !is_object($parent)) { $parent = $data['parent'] = $this->taxonomyManager->getTerm($parent); } $options = $this->getModuleOptions()->getType($taxonomy->getName()); if ($parent === null && !$options->isRootable()) { throw new RuntimeException(sprintf('Taxonomy "%s" is not rootable.', $taxonomy->getName())); } elseif ($parent instanceof TaxonomyTermInterface) { $parentType = $parent->getTaxonomy()->getName(); $objectType = $taxonomy->getName(); $objectOptions = $this->getModuleOptions()->getType($objectType); if (!$objectOptions->isParentAllowed($parentType)) { throw new RuntimeException(sprintf('Parent "%s" does not allow child "%s"', $parentType, $objectType)); } } $data['term'] = $this->getTermManager()->createTerm($data['term']['name'], $taxonomy->getInstance()); return $data; }