/**
  * @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;
 }
Пример #2
0
 /**
  * Sets an element in the collection at the specified key/index.
  *
  * @param string|integer $key The key/index of the element to set.
  * @param mixed $value The element to set.
  *
  * @return void
  */
 function set($key, $value)
 {
     $this->checkType($value);
     $this->initialize();
     $className = $this->entityTerms->getClassName();
     $eTerm = new $className();
     /** @var $eTerm EntityTermRepositoryInterface */
     $eTerm->setTerm($value);
     $this->collection->set($key, $eTerm);
 }