本文整理匯總了PHP中Symfony\Component\PropertyAccess\PropertyAccessorInterface::setValue方法的典型用法代碼示例。如果您正苦於以下問題:PHP PropertyAccessorInterface::setValue方法的具體用法?PHP PropertyAccessorInterface::setValue怎麽用?PHP PropertyAccessorInterface::setValue使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Symfony\Component\PropertyAccess\PropertyAccessorInterface
的用法示例。
在下文中一共展示了PropertyAccessorInterface::setValue方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: apply
/**
* {@inheritdoc}
*/
public function apply(Request $request, ParamConverter $configuration) : bool
{
$class = $configuration->getClass();
$constant = sprintf('%s::EMPTY_PROPERTIES', $class);
$propertiesToBeSkipped = [];
$instance = new $class();
$declaredProperties = array_filter((new \ReflectionClass($class))->getProperties(), function (ReflectionProperty $property) use($class) {
return $property->getDeclaringClass()->name === $class;
});
// fetch result properties that are optional and to be skipped as those must not be processed
if (defined($constant)) {
$propertiesToBeSkipped = constant($constant);
}
/** @var ReflectionProperty $property */
foreach ($declaredProperties as $property) {
$propertyName = $property->getName();
if (in_array($propertyName, $propertiesToBeSkipped, true)) {
continue;
}
// non-writable properties cause issues with the DTO creation
if (!$this->propertyAccess->isWritable($instance, $propertyName)) {
throw new \RuntimeException($this->getInvalidPropertyExceptionMessage($class, $propertyName));
}
$this->propertyAccess->setValue($instance, $propertyName, $this->findAttributeInRequest($request, $property, $this->propertyAccess->getValue($instance, $propertyName)));
}
$request->attributes->set($configuration->getName(), $instance);
return true;
}
示例2: mapFormsToData
/**
* {@inheritdoc}
*/
public function mapFormsToData($forms, &$data)
{
if (null === $data) {
return;
}
if (!is_array($data) && !is_object($data)) {
throw new UnexpectedTypeException($data, 'object, array or empty');
}
foreach ($forms as $form) {
$propertyPath = $form->getPropertyPath();
$config = $form->getConfig();
// Write-back is disabled if the form is not synchronized (transformation failed),
// if the form was not submitted and if the form is disabled (modification not allowed)
if (null !== $propertyPath && $config->getMapped() && $form->isSubmitted() && $form->isSynchronized() && !$form->isDisabled()) {
// If the field is of type DateTime and the data is the same skip the update to
// keep the original object hash
if ($form->getData() instanceof \DateTime && $form->getData() == $this->propertyAccessor->getValue($data, $propertyPath)) {
continue;
}
// If the data is identical to the value in $data, we are
// dealing with a reference
if (!is_object($data) || !$config->getByReference() || $form->getData() !== $this->propertyAccessor->getValue($data, $propertyPath)) {
$this->propertyAccessor->setValue($data, $propertyPath, $form->getData());
}
}
}
}
示例3: setDefaultValues
/**
* @param object $entity Entity
*
* @throws \Darvin\Utils\DefaultValue\DefaultValueException
*/
protected function setDefaultValues($entity)
{
$entityClass = ClassUtils::getClass($entity);
$meta = $this->extendedMetadataFactory->getExtendedMetadata($entityClass);
if (!isset($meta['defaultValues']) || empty($meta['defaultValues'])) {
return;
}
$defaultValuesMap = $meta['defaultValues'];
$this->filterDefaultValuesMap($defaultValuesMap, $entity, $entityClass);
if (empty($defaultValuesMap)) {
return;
}
$sourcePropertyValues = $this->getSourcePropertyValues(array_unique(array_values($defaultValuesMap)), $entity, $entityClass);
$recomputeChangeSet = false;
foreach ($defaultValuesMap as $targetProperty => $sourcePropertyPath) {
if (null === $sourcePropertyValues[$sourcePropertyPath]) {
continue;
}
if (!$this->propertyAccessor->isWritable($entity, $targetProperty)) {
throw new DefaultValueException(sprintf('Property "%s::$%s" is not writable.', $entityClass, $targetProperty));
}
$this->propertyAccessor->setValue($entity, $targetProperty, $sourcePropertyValues[$sourcePropertyPath]);
$recomputeChangeSet = true;
}
if ($recomputeChangeSet) {
$this->recomputeChangeSet($entity);
}
}
示例4: mapFormsToData
/**
* {@inheritdoc}
*/
public function mapFormsToData(array $forms, &$data)
{
if (null === $data) {
return;
}
if (!is_array($data) && !is_object($data)) {
throw new UnexpectedTypeException($data, 'object, array or empty');
}
$iterator = new VirtualFormAwareIterator($forms);
$iterator = new \RecursiveIteratorIterator($iterator);
foreach ($iterator as $form) {
/* @var \Symfony\Component\Form\FormInterface $form */
$propertyPath = $form->getPropertyPath();
$config = $form->getConfig();
// Write-back is disabled if the form is not synchronized (transformation failed)
// and if the form is disabled (modification not allowed)
if (null !== $propertyPath && $config->getMapped() && $form->isSynchronized() && !$form->isDisabled()) {
// If the data is identical to the value in $data, we are
// dealing with a reference
if (!is_object($data) || !$config->getByReference() || $form->getData() !== $this->propertyAccessor->getValue($data, $propertyPath)) {
$this->propertyAccessor->setValue($data, $propertyPath, $form->getData());
}
}
}
}
示例5:
function it_creates_theme_from_valid_array($themeClassName, PropertyAccessorInterface $propertyAccessor)
{
$data = ['name' => 'Foo bar', 'logical_name' => 'foo/bar'];
$propertyAccessor->setValue(Argument::any(), 'name', 'Foo bar')->shouldBeCalled();
$propertyAccessor->setValue(Argument::any(), 'logical_name', 'foo/bar')->shouldBeCalled();
$propertyAccessor->setValue(Argument::any(), 'parentsNames', [])->shouldBeCalled();
$this->createFromArray($data)->shouldHaveType($themeClassName);
}
示例6: setValue
/**
* @inheritdoc
*/
public function setValue(&$objectOrArray, $propertyPath, $value)
{
if ($objectOrArray instanceof \stdClass) {
$objectOrArray->{$propertyPath} = $value;
return;
}
$this->decoratedPropertyAccessor->setValue($objectOrArray, $propertyPath, $value);
}
示例7: setState
/**
* {@inheritdoc}
*/
public function setState(&$object, $value)
{
try {
$this->propertyAccessor->setValue($object, $this->propertyPath, $value);
} catch (SymfonyNoSuchPropertyException $e) {
throw new NoSuchPropertyException(sprintf('Property path "%s" on object "%s" does not exist.', $this->propertyPath, get_class($object)), $e->getCode(), $e);
}
}
示例8: setAttributeValue
/**
* {@inheritdoc}
*/
protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
{
try {
$this->propertyAccessor->setValue($object, $attribute, $value);
} catch (NoSuchPropertyException $exception) {
// Properties not found are ignored
}
}
示例9: set
/**
* @param mixed $value
*/
public function set($value)
{
if ($this->path) {
$this->accessor->setValue($this->object, $this->path, $value);
} else {
$this->object = $value;
}
}
示例10: create
/**
* {@inheritdoc}
*/
public function create($entityClass, array $defaultValues = array(), array $options = array())
{
$object = new $entityClass();
foreach ($defaultValues as $propertyPath => $value) {
$this->propertyAccessor->setValue($object, $propertyPath, $value);
}
return $object;
}
示例11: create
/**
* {@inheritdoc}
*/
public function create(array $options = [])
{
$class = $this->resource->getModel();
$object = new $class();
foreach ($options as $propertyPath => $value) {
$this->propertyAccessor->setValue($object, $propertyPath, $value);
}
return $object;
}
示例12: create
/**
* @inheritdoc
*/
public function create(array $parameters = array())
{
$class = $this->options['class'];
$object = new $class();
foreach ($parameters as $key => $value) {
$this->propertyAccessor->setValue($object, $key, $value);
}
return $object;
}
示例13: setValue
/**
* {@inheritdoc}
*/
public function setValue($object, ColumnInfoInterface $columnInfo, $data, array $options = array())
{
if (!isset($options['propertyPath'])) {
throw new \InvalidArgumentException('propertyPath option is required');
}
foreach ($data as $locale => $value) {
$object->setLocale($locale);
$this->propertyAccessor->setValue($object, 'translation.' . $options['propertyPath'], $value);
}
}
示例14: createFromArray
/**
* {@inheritdoc}
*/
public function createFromArray(array $themeData)
{
/** @var ThemeInterface $theme */
$theme = new $this->themeClassName();
$themeData = $this->optionsResolver->resolve($themeData);
foreach ($themeData as $attributeKey => $attributeValue) {
$this->propertyAccessor->setValue($theme, $this->normalizeAttributeKey($attributeKey), $attributeValue);
}
return $theme;
}
示例15: setValue
/**
* {@inheritdoc}
*/
public function setValue($object, ColumnInfoInterface $columnInfo, $data, array $options = array())
{
if ($columnInfo->getLocale()) {
$locale = $columnInfo->getLocale();
} else {
$suffixes = $columnInfo->getSuffixes();
$locale = array_shift($suffixes);
}
$object->setLocale($locale);
$this->propertyAccessor->setValue($object, 'translation.' . $columnInfo->getPropertyPath(), $data);
}