本文整理汇总了PHP中Symfony\Component\PropertyAccess\PropertyAccess::createPropertyAccessorBuilder方法的典型用法代码示例。如果您正苦于以下问题:PHP PropertyAccess::createPropertyAccessorBuilder方法的具体用法?PHP PropertyAccess::createPropertyAccessorBuilder怎么用?PHP PropertyAccess::createPropertyAccessorBuilder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\PropertyAccess\PropertyAccess
的用法示例。
在下文中一共展示了PropertyAccess::createPropertyAccessorBuilder方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: call
/**
* {@inheritdoc}
*/
public function call(ActionEvent $event, string $format) : Response
{
$controller = $event->getController();
$dataProvider = $event->getDataProvider();
$request = $event->getRequest();
$data = $dataProvider->get($request->get('id'));
$this->checkRole($controller, $data);
$allowed = $this->getOptions()['allow'];
$properties = $request->get('data', []);
$accessor = PropertyAccess::createPropertyAccessorBuilder()->enableMagicCall()->getPropertyAccessor();
foreach ($properties as $property => $value) {
if (in_array($property, $allowed) && $accessor->isWritable($data, $property)) {
$accessor->setValue($data, $property, $value);
}
}
$validator = $controller->get('validator');
$errors = $validator->validate($data, null, ["update"]);
$responseHandler = $controller->get('vardius_crud.response.handler');
if (count($errors) > 0) {
return new JsonResponse(['message' => 'Invalid data', 'errors' => $errors], 400);
} else {
$source = $dataProvider->getSource();
$crudEvent = new CrudEvent($source, $controller);
$dispatcher = $controller->get('event_dispatcher');
$dispatcher->dispatch(CrudEvents::CRUD_PRE_UPDATE, $crudEvent);
$dataProvider->update($data);
$dispatcher->dispatch(CrudEvents::CRUD_POST_UPDATE, $crudEvent);
return $responseHandler->getResponse($format, '', '', $data, 200, [], ['groups' => ['update']]);
}
}
示例2: __construct
/**
* Construct the snapshot
*
* The following options are taken into account :
* - snapshotClass : snapshot class to use for each elements. If none are
* given, the normalize() method will transform this into
* either an ArraySnapshot or an ObjectSnapshot, depending
* on the situation.
*
* @param mixed $data Either an array or a traversable, data to take a snapshot of
* @param mixed $primary Property path compatible value to use to get the primary key of each elements
* @param array $options Array of options
*
* @throws InvalidArgumentException the $data is not an array or a Traversable
* @throws InvalidArgumentException the $data is not an at least 2 dimensional array
* @throws InvalidArgumentException the snapshotClass in the options is not loadable
* @throws InvalidArgumentException the snapshotClass in the options is not a valid snapshot class
* @throws InvalidArgumentException one of the elements of the collection does not have a $primary key
*/
public function __construct($data, $primary, array $options = [])
{
$this->data = [];
$this->raw = $data;
$primary = new PropertyPath($primary);
$snapshot = null;
$accessor = PropertyAccess::createPropertyAccessorBuilder()->enableExceptionOnInvalidIndex()->getPropertyAccessor();
if (isset($options['snapshotClass'])) {
if (!class_exists($options['snapshotClass'])) {
throw new InvalidArgumentException(sprintf('The snapshot class "%s" does not seem to be loadable', $options['snapshotClass']));
}
$refl = new ReflectionClass($options['snapshotClass']);
if (!$refl->isInstantiable() || !$refl->isSubclassOf('Totem\\AbstractSnapshot')) {
throw new InvalidArgumentException('A Snapshot Class should be instantiable and extends abstract class Totem\\AbstractSnapshot');
}
$snapshot = $options['snapshotClass'];
}
if (!is_array($data) && !$data instanceof Traversable) {
throw new InvalidArgumentException(sprintf('An array or a Traversable was expected to take a snapshot of a collection, "%s" given', is_object($data) ? get_class($data) : gettype($data)));
}
foreach ($data as $key => $value) {
if (!is_int($key)) {
throw new InvalidArgumentException('The given array / Traversable is not a collection as it contains non numeric keys');
}
if (!$accessor->isReadable($value, $primary)) {
throw new InvalidArgumentException(sprintf('The key "%s" is not defined or readable in one of the elements of the collection', $primary));
}
$primaryKey = $accessor->getValue($value, $primary);
$this->link[$primaryKey] = $key;
$this->data[$primaryKey] = $this->snapshot($value, $snapshot);
}
parent::normalize();
}
示例3: access
public static function access()
{
if (!self::$_access) {
self::$_access = PropertyAccess::createPropertyAccessorBuilder()->disableExceptionOnInvalidIndex()->getPropertyAccessor();
}
return self::$_access;
}
示例4: getPropertyAccessor
/**
* @return PropertyAccessorInterface
*/
protected function getPropertyAccessor()
{
if (is_null($this->_propertyAccessor)) {
$this->_propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()->enableMagicCall()->getPropertyAccessor();
}
return $this->_propertyAccessor;
}
示例5: getField
/**
* @param string|PropertyPathInterface $propertyPath The property path to modify
* @param mixed|null $default The default value if the field does not exist
*
* @throws NoSuchIndexException
*
* @return mixed
*/
public function getField($propertyPath, $default = null)
{
$propertyPathAccessor = PropertyAccess::createPropertyAccessorBuilder()->enableExceptionOnInvalidIndex()->getPropertyAccessor();
if ($propertyPathAccessor->isReadable($this->fields, $propertyPath)) {
return $propertyPathAccessor->getValue($this->fields, $propertyPath);
}
return $default;
}
示例6: getObjectFieldValue
/**
* @param object $object
* @param string $field
*
* @return mixed
*/
public static function getObjectFieldValue($object, $field)
{
$propertyAccess = PropertyAccess::createPropertyAccessorBuilder()->enableMagicCall()->enableExceptionOnInvalidIndex()->getPropertyAccessor();
try {
return $propertyAccess->getValue($object, $field);
} catch (NoSuchPropertyException $e) {
return parent::getObjectFieldValue($object, $field);
}
}
示例7: testHandleEventWithInvalidConfigIncrement
/**
* test handle event with an invalid stats
*/
public function testHandleEventWithInvalidConfigIncrement()
{
$client = $this->getMockedClient();
$client->setPropertyAccessor(PropertyAccess\PropertyAccess::createPropertyAccessorBuilder()->enableMagicCall()->getPropertyAccessor());
$client->addEventToListen('test', array('increment' => 'stats.<toto>'));
$this->exception(function () use($client) {
$event = new \Symfony\Component\EventDispatcher\Event();
$client->handleEvent($event, 'test');
});
}
示例8: get
/** @inheritdoc */
public function get($id, $key)
{
$config = $this->provider->read($id);
$accessor = PropertyAccess::createPropertyAccessorBuilder()->enableExceptionOnInvalidIndex()->getPropertyAccessor();
try {
return $accessor->getValue($config, $key);
} catch (\Exception $e) {
throw new \RuntimeException("Configuration does not have a key {$key} or {$key} is empty.", $e->getCode(), $e);
}
}
示例9: setValues
/**
* {@inheritdoc}
*
* @param BasePageDocument $object
*/
public function setValues($object, $locale, array $data)
{
$propertyAccess = PropertyAccess::createPropertyAccessorBuilder()->enableMagicCall()->getPropertyAccessor();
$structure = $object->getStructure();
foreach ($data as $property => $value) {
try {
$propertyAccess->setValue($structure, $property, $value);
} catch (\InvalidArgumentException $e) {
//ignore not existing properties
}
}
}
示例10: field
/**
* @param Field $field
* @param $entity
* @param $applicationConfiguration
* @return mixed
*/
public function field(Field $field, $entity, ApplicationConfiguration $applicationConfiguration)
{
$accessor = PropertyAccess::createPropertyAccessorBuilder()->enableMagicCall()->getPropertyAccessor();
$value = $accessor->getValue($entity, $field->getName());
if ($value instanceof DateTime) {
$value = $value->format($applicationConfiguration->dateFormat);
} else {
if (is_array($value)) {
$value = $this->recursiveImplode(', ', $value);
}
}
return $value;
}
示例11: readYamlFile
/** @internal */
public static function readYamlFile($filename, $propertyPath = null, $allowUndefinedIndex = false)
{
if (!file_exists($filename)) {
throw new \InvalidArgumentException(sprintf('read_yaml_file: Unable to parse Yaml document. No such file: "%s".', $filename));
}
try {
$yaml = Yaml::parse(file_get_contents($filename), Yaml::PARSE_DATETIME);
return PropertyAccess::createPropertyAccessorBuilder()->enableExceptionOnInvalidIndex()->getPropertyAccessor()->getValue($yaml, $propertyPath);
} catch (YamlParseException $e) {
$e->setParsedFile($filename);
throw new \InvalidArgumentException(sprintf('read_yaml_file: Unable to parse Yaml document. syntax error: "%s".', $e->getMessage()), 0, $e);
} catch (NoSuchIndexException $e) {
if ($allowUndefinedIndex) {
return;
}
throw new \InvalidArgumentException(sprintf('read_yaml_file: Unable to read property from Yaml document "%s". no such index: "%s".', $filename, $e->getMessage()), 0, $e);
}
}
示例12: replaceInNodeFormMethod
/**
* Replaces a string with a method name
*
* @param EventInterface $event An event
* @param string $eventName The name of the event
* @param string $node The node in which the replacing will happen
*
* @return string
*/
private static function replaceInNodeFormMethod($event, $eventName, $node)
{
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()->enableMagicCall()->getPropertyAccessor();
// `event->getName()` is deprecated, we have to replace <name> directly with $eventName
$node = str_replace('<name>', $eventName, $node);
if (preg_match_all('/<([^>]*)>/', $node, $matches) > 0) {
$tokens = $matches[1];
foreach ($tokens as $token) {
$value = $propertyAccessor->getValue($event, $token);
$node = str_replace('<' . $token . '>', $value, $node);
}
}
return $node;
}
示例13: __construct
public function __construct(Container $container, $idPrefix = null)
{
$this->container = $container;
$this->idPrefix = (string) $idPrefix;
$this->accessor = PropertyAccess::createPropertyAccessorBuilder()->enableExceptionOnInvalidIndex()->getPropertyAccessor();
}
示例14: __construct
public function __construct($evaluationMode)
{
$this->evaluationMode = $evaluationMode;
$this->propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()->enableExceptionOnInvalidIndex()->getPropertyAccessor();
}
示例15: testGetValueFailsIfNoSuchIndex
/**
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchIndexException
*/
public function testGetValueFailsIfNoSuchIndex()
{
$this->propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()->enableExceptionOnInvalidIndex()->getPropertyAccessor();
$object = $this->getContainer(array('firstName' => 'Bernhard'));
$this->propertyAccessor->getValue($object, '[lastName]');
}