/**
  * This function performs a kind of "intersection union" operation, and is useful especially when dealing
  * with dynamic forms. For instance, if a collection contains existing elements and a form remove one of those
  * elements, this function will return a Collection that contains all the elements from $collection1, minus ones
  * that are not present in $collection2. This is used internally in the DoctrineModule hydrator, so that the
  * work is done for you automatically
  *
  * @param  Collection $collection1
  * @param  Collection $collection2
  * @return Collection
  */
 public static function intersectUnion(Collection $collection1, Collection $collection2)
 {
     // Don't make the work both
     if ($collection1 === $collection2) {
         return $collection1;
     }
     $toRemove = array();
     foreach ($collection1 as $key1 => $value1) {
         $elementFound = false;
         foreach ($collection2 as $key2 => $value2) {
             if ($value1 === $value2) {
                 $elementFound = true;
                 unset($collection2[$key2]);
                 break;
             }
         }
         if (!$elementFound) {
             $toRemove[] = $key1;
         }
     }
     // Remove elements that are in $collection1 but not in $collection2
     foreach ($toRemove as $key) {
         $collection1->remove($key);
     }
     // Add elements that are in $collection2 but not in $collection1
     foreach ($collection2 as $value) {
         $collection1->add($value);
     }
     return $collection1;
 }
Example #2
0
 /**
  * @param Empresa $empresa
  */
 public function removeEmpresa($empresa)
 {
     if ($this->empresas->contains($empresa)) {
         $empresa->setDirector(null);
         $this->empresas->removeElement($empresa);
     }
 }
Example #3
0
 /**
  * Remove item.
  *
  * @param \SWP\Component\Bridge\Model\Item $item
  */
 public function removeItem(\SWP\Component\Bridge\Model\Item $item)
 {
     if ($this->items->contains($item)) {
         $this->items->removeElement($item);
         $item->setPackage(null);
     }
 }
Example #4
0
 /**
  * Adds a post top the User's authored Posts.
  *
  * Post Author needs to be updated manually.
  *
  * @param Post $addPost
  * @return User
  */
 public function addPost(Post $addPost)
 {
     if (!$this->getAuthoredPosts()->contains($addPost)) {
         $this->authoredPosts->add($addPost);
     }
     return $this;
 }
 /**
  * Set the list of roles
  * @param Collection $roles
  */
 public function setRoles(Collection $roles)
 {
     $this->roles->clear();
     foreach ($roles as $role) {
         $this->roles[] = $role;
     }
 }
Example #6
0
 /**
  * @param mixed $context
  * @param Collection|null $errors
  */
 protected function addError($context, Collection $errors = null)
 {
     if ($errors && $this->getMessage()) {
         $messageParameters = $this->getMessageParameters($context);
         $errors->add(array('message' => $this->getMessage(), 'parameters' => $messageParameters));
     }
 }
 function let(LoaderInterface $loader, Collection $resourcesToThemes, ThemeInterface $theme)
 {
     $theme->getLogicalName()->willReturn("sylius/sample-theme");
     $resourcesToThemes->get(realpath($this->getThemeTranslationResourcePath()))->willReturn($theme);
     $resourcesToThemes->get(realpath($this->getVanillaTranslationResourcePath()))->willReturn(null);
     $this->beConstructedWith($loader, $resourcesToThemes);
 }
Example #8
0
 /**
  * @param Role $parent
  */
 public function addParent(Role $parent)
 {
     if ($this->parents->contains($parent)) {
         throw new LogicException(sprintf("Parent role '%s' already assigned.", $parent->getId()));
     }
     $this->parents->add($parent);
 }
Example #9
0
 /**
  * {@inheritdoc}
  */
 public function removeImage(TaxonImageInterface $image)
 {
     if ($this->hasImage($image)) {
         $image->setTaxon(null);
         $this->images->removeElement($image);
     }
 }
 function it_applies_choice_filter_on_datasource_for_collection_value(FilterDatasourceAdapterInterface $datasource, Collection $collection, $utility)
 {
     $collection->count()->willReturn(2);
     $collection->getValues()->willReturn(['foo', 'bar']);
     $utility->applyFilter($datasource, 'data_name_key', 'IN', ['foo', 'bar'])->shouldBeCalled();
     $this->apply($datasource, ['value' => $collection, 'type' => AjaxChoiceFilterType::TYPE_CONTAINS]);
 }
Example #11
0
 /**
  * @param Archivo $archivo
  */
 public function removeArchivo($archivo)
 {
     if ($this->archivos->contains($archivo)) {
         $this->archivos->removeElement($archivo);
         $archivo->setDocumentable(null);
     }
 }
Example #12
0
 /**
  * Remove articles
  *
  * @param Article $article
  */
 public function removeArticle(Article $article)
 {
     if ($this->articles->contains($article)) {
         $this->articles->removeElement($article);
         $article->removeCitation($this);
     }
 }
 /**
  * @param Collection $collection
  *
  * @return ArrayCollection
  */
 private function filterDeleted(Collection $collection) : ArrayCollection
 {
     // getValues => reset keys
     return new ArrayCollection($collection->filter(function ($entity) {
         return !$entity instanceof SoftDeletableInterface || $entity->isDeleted() === false;
     })->getValues());
 }
 private function addCategoryParent(CategoryInterface $category = null, Collection $collection)
 {
     if (null !== $category) {
         $collection->add($category);
         $this->addCategoryParent($category->getParent(), $collection);
     }
 }
 /**
  * Twitter requires domains without scheme
  *
  * @param Doctrine\Common\Collections\Collection
  */
 public function setDomains(Collection $domains)
 {
     $remap = $domains->map(function ($item) {
         return str_replace(array("https://", "http://"), '', $item);
     }, $domains);
     $this->domains = $remap;
 }
 /**
  * Remove a given option
  *
  * @param OptionInterface $option
  * @return $this
  */
 public function removeOption(OptionInterface $option)
 {
     if ($this->hasOption($option)) {
         $this->options->removeElement($option);
     }
     return $this;
 }
Example #17
0
 public function addOutpost(Outpost $outpost, $sync = true)
 {
     $this->outposts->add($outpost);
     if ($sync) {
         $outpost->setOwner($this, false);
     }
 }
 /**
  * @inheritdoc
  */
 public function addField(FieldInterface $field)
 {
     if (is_null($field->getSequential())) {
         $field->setSequential($this->getFields()->count());
     }
     $this->fields->add($field);
     return $this;
 }
 /**
  * {@inheritdoc}
  */
 public function processConfiguration(Collection $collection)
 {
     $config = [];
     $collection->map(function (PaymentMethodConfigurationInterface $configuration) use(&$config) {
         $config[$configuration->getName()] = $configuration->getValue();
     });
     return $config;
 }
 /**
  * Resets previous photo collection
  *
  * @param Collection $collection
  */
 protected function clearPreviousCollection(Collection $collection)
 {
     if ($collection->count()) {
         foreach ($collection as $item) {
             $collection->removeElement($item);
         }
     }
 }
Example #21
0
 public function getRoles()
 {
     $roles = array();
     foreach ($this->roles->getValues() as $roleObject) {
         $roles[] = $roleObject->getName();
     }
     return $roles;
 }
Example #22
0
 protected function synchronizeOptions(Collection $options)
 {
     $this->options->map(function (VariantOptionInterface $option) use($options) {
         if (false === $options->contains($option)) {
             $this->options->removeElement($option);
         }
     });
 }
 function its_normalize_method_throw_exception_when_required_field_name_key_is_not_passed($serializer, Collection $collection)
 {
     $collection->getIterator()->willReturn(new \ArrayIterator([4, 8, 15]));
     $serializer->normalize(4, null, ['foo' => 'bar'])->willReturn('Four');
     $serializer->normalize(8, null, ['foo' => 'bar'])->willReturn('Eight');
     $serializer->normalize(15, null, ['foo' => 'bar'])->willReturn('Fifteen');
     $this->shouldThrow(new InvalidArgumentException('Missing required "field_name" context value, got "foo"'))->duringNormalize($collection, null, ['foo' => 'bar']);
 }
 /**
  * Get steps sorted by order.
  *
  * @return Collection|Step[]
  */
 public function getOrderedSteps()
 {
     $steps = $this->steps->toArray();
     usort($steps, function (Step $stepOne, Step $stepTwo) {
         return $stepOne->getOrder() >= $stepTwo->getOrder() ? 1 : -1;
     });
     return new ArrayCollection($steps);
 }
 /**
  * Extracts information from collection and appends it to an array of attributes
  *
  * @param Collection $collection
  * @param array      $attributes
  */
 protected function extractValues(Collection $collection, &$attributes)
 {
     $collection->map(function (AttributeValueInterface $attributeValue) use(&$attributes) {
         $attribute = $attributeValue->getAttribute();
         $attributes[$attribute->getId()]['name'] = $attribute->translate()->getName();
         $attributes[$attribute->getId()]['values'][$attributeValue->getId()] = $attributeValue->translate()->getName();
     });
 }
 protected function syncNewAttributes(Collection $attributes)
 {
     $attributes->map(function (AttributeInterface $attribute) {
         if (false === $this->attributes->contains($attribute)) {
             $attribute->addValue($this);
         }
     });
 }
 private function getCalculator(ShippingMethodInterface $shippingMethod) : ShippingCalculatorInterface
 {
     $calculator = $shippingMethod->getCalculator();
     if (false === $this->calculators->containsKey($calculator)) {
         throw new CalculatorNotFoundException($calculator);
     }
     return $this->calculators->get($calculator);
 }
Example #28
0
 /**
  * @param Collection $collection
  * @param array      $entities
  *
  * @return Collection
  */
 protected function initEntitiesCollectionFromArray(Collection $collection, array $entities)
 {
     foreach ($entities as $entity) {
         $el = new EntityName($entity);
         $collection->add($el);
     }
     return $collection;
 }
 /**
  * Removes all elements from old collection which have not been passed in new collection
  *
  * @param Collection $oldEntities
  * @param Collection $newEntities
  */
 protected function synchronizeCollection(Collection $oldCollection, Collection $newCollection)
 {
     foreach ($oldCollection as $oldEntity) {
         if (!$newCollection->contains($oldEntity)) {
             $newCollection->removeElement($oldEntity);
         }
     }
 }
Example #30
0
 /**
  * @param User $user
  */
 public function removeUser(User $user)
 {
     if (!$this->users->contains($user)) {
         return;
     }
     $this->users->removeElement($user);
     $user->removeGroup($this);
 }