/**
  * @param $term
  * @throws \InvalidArgumentException
  * @return $this
  */
 public function setTerm($term = null)
 {
     $this->initialize();
     if (null === $term) {
         $this->entityTerm = null;
         return $this;
     }
     if (!$term instanceof Term) {
         throw new \InvalidArgumentException(sprintf('Expected instance of Term. "%s" given.', get_class($term)));
     }
     if ($this->em->getUnitOfWork()->getEntityState($term) == UnitOfWork::STATE_DETACHED) {
         $term = $this->em->merge($term);
     }
     if ($term->getVocabulary() !== $this->vocabulary) {
         throw new \InvalidArgumentException(sprintf('Term "%s" (#%d) does not belong in "%s" vocabulary.', $term->getName(), $term->getId(), $this->vocabulary->getName()));
     }
     if ($this->entityTerm && $this->entityTerm->getTerm() === $term) {
         return $this;
     }
     $className = $this->entityTerms->getClassName();
     $this->setDirty(true);
     $eTerm = new $className();
     /** @var $eTerm EntityTermInterface */
     $eTerm->setTerm($term);
     $this->entityTerm = $eTerm;
     return $this;
 }
 /**
  * @param $element
  * @throws \InvalidArgumentException
  */
 protected function checkType($element)
 {
     if (!$element instanceof Term) {
         throw new \InvalidArgumentException(sprintf('Expected instance of %s. "%s" given.', __NAMESPACE__ . '\\Term', get_class($element)));
     }
     if ($this->em->getUnitOfWork()->getEntityState($element) == UnitOfWork::STATE_DETACHED) {
         $element = $this->em->merge($element);
     }
     if ($element->getVocabulary() !== $this->vocabulary) {
         throw new \InvalidArgumentException(sprintf('Term "%s" (#%d) does not belong in the "%s" vocabulary.', $element->getName(), $element->getId(), $this->vocabulary->getName()));
     }
 }
Esempio n. 3
0
 /**
  * @param VocabularyInterface $vocabulary
  * @return void
  */
 public function setVocabulary(VocabularyInterface $vocabulary = null)
 {
     $this->vocabulary = $vocabulary;
     if ($vocabulary !== null) {
         $vocabulary->addTerm($this);
     }
 }