/** * {@inheritdoc} */ public function isWritable($node, $singlePath) { if (!$singlePath) { return false; } return $this->propertyAccess->isWritable($node, $singlePath); }
/** * @param mixed $entity * @param array $entityData * * @return mixed */ protected function populate($entity, array $entityData) { foreach ($entityData as $key => $value) { if ($this->propertyAccessor->isWritable($entity, $key)) { $this->propertyAccessor->setValue($entity, $key, $value); } } return $entity; }
/** * {@inheritDoc} */ public function connect(UserInterface $user, UserResponseInterface $response) { if (!$user instanceof User) { throw new UnsupportedUserException(sprintf('Expected an instance of FOS\\UserBundle\\Model\\User, but got "%s".', get_class($user))); } $property = $this->getProperty($response); if (!$this->accessor->isWritable($user, $property)) { throw new \RuntimeException(sprintf("Class '%s' must have defined setter method for property: '%s'.", get_class($user), $property)); } $username = $response->getUsername(); if (null !== ($previousUser = $this->userManager->findUserBy(array($property => $username)))) { $this->accessor->setValue($previousUser, $property, null); $this->userManager->updateUser($previousUser); } $this->accessor->setValue($user, $property, $username); $this->userManager->updateUser($user); }
/** * @param $object * @param $data * @param ObjectManager $manager For persistence * @param string $referenceName * * @return $object */ protected function add($object, $data, ObjectManager $manager = null, $referenceName = null) { $accessor = new PropertyAccessor(); foreach ($data as $key => $value) { if ($accessor->isWritable($object, $key)) { $accessor->setValue($object, $key, $value); } } if (null !== $manager) { $manager->persist($object); $manager->flush(); } if (null !== $referenceName) { $this->addReference($referenceName, $object); } return $object; }
/** * @param \Doctrine\ORM\Mapping\ClassMetadata $meta * @param \Nette\ComponentModel\Component $component * @param mixed $entity * @return boolean */ public function save(ClassMetadata $meta, Component $component, $entity) { if (!$component instanceof BaseControl) { return false; } $name = $component->getOption(self::FIELD_NAME, $component->getName()); $value = $component->getValue(); if ($this->accessor->isWritable($entity, $name) && !$meta->hasAssociation($name)) { try { $this->accessor->setValue($entity, $name, $value); return true; } catch (\Kdyby\Doctrine\MemberAccessException $e) { } } if (!$meta->hasAssociation($name)) { return false; } $value = $component->getValue(); $entityClass = $this->relatedMetadata($entity, $name)->getName(); $repository = $this->entityManager->getRepository($entityClass); if ($meta->isCollectionValuedAssociation($name)) { $property = \Doctrine\Common\Util\Inflector::singularize($name); foreach ($repository->findAll() as $associatedEntity) { if (in_array($associatedEntity->id, $value)) { $hasMethod = 'has' . ucfirst($property); if (!$entity->{$hasMethod}($associatedEntity)) { $addMethod = 'add' . ucfirst($property); $entity->{$addMethod}($associatedEntity); } } else { $removeMethod = 'remove' . ucfirst($property); $entity->{$removeMethod}($associatedEntity); } } } elseif ($value === null || ($value = $repository->find($value))) { if ($this->accessor->isWritable($entity, $name)) { try { $this->accessor->setValue($entity, $name, $value); } catch (\Kdyby\Doctrine\MemberAccessException $e) { return false; } } } return true; }
/** * @dataProvider getReferenceChainObjectsForIsWritable */ public function testIsWritableForReferenceChainIssue($object, $path, $value) { $this->assertEquals($value, $this->propertyAccessor->isWritable($object, $path)); }
public function testEmptyIsNotWritable() { $this->assertFalse($this->propertyAccessor->isWritable('', 'foobar')); }
/** * @dataProvider getPathsWithUnexpectedType */ public function testIsWritableReturnsFalseIfNotObjectOrArray($objectOrArray, $path) { $this->assertFalse($this->propertyAccessor->isWritable($objectOrArray, $path)); }
/** * @dataProvider getValidPropertyPaths */ public function testIsWritable($collection, $path) { $this->assertTrue($this->propertyAccessor->isWritable($collection, $path, 'Updated')); }
public function testIsWritableThrowsExceptionIfEmpty() { $this->assertFalse($this->propertyAccessor->isWritable('', 'foobar', 'Updated')); }
/** * {@inheritdoc} */ public function isWritable($object, $propertyPath) { return $this->accessor->isWritable($object, $propertyPath); }