/** * {@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']]); } }
/** * 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(); }
public static function access() { if (!self::$_access) { self::$_access = PropertyAccess::createPropertyAccessorBuilder()->disableExceptionOnInvalidIndex()->getPropertyAccessor(); } return self::$_access; }
/** * @return PropertyAccessorInterface */ protected function getPropertyAccessor() { if (is_null($this->_propertyAccessor)) { $this->_propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()->enableMagicCall()->getPropertyAccessor(); } return $this->_propertyAccessor; }
/** * @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; }
/** * @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); } }
/** * 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'); }); }
/** @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); } }
/** * {@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 } } }
/** * @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; }
/** @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); } }
/** * 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; }
public function __construct(Container $container, $idPrefix = null) { $this->container = $container; $this->idPrefix = (string) $idPrefix; $this->accessor = PropertyAccess::createPropertyAccessorBuilder()->enableExceptionOnInvalidIndex()->getPropertyAccessor(); }
public function __construct($evaluationMode) { $this->evaluationMode = $evaluationMode; $this->propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()->enableExceptionOnInvalidIndex()->getPropertyAccessor(); }
/** * @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]'); }
/** * @return \Symfony\Component\PropertyAccessor\PropertyAccessorInterface */ protected function _getAccessor() { if (empty($this->_accessor)) { $builder = PropertyAccess::createPropertyAccessorBuilder(); $this->_accessor = $builder->getPropertyAccessor(); } return $this->_accessor; }
public function __construct() { $this->accessor = PropertyAccess::createPropertyAccessorBuilder()->enableExceptionOnInvalidIndex()->getPropertyAccessor(); }
/** * @return \Symfony\Component\PropertyAccess\PropertyAccessorInterface */ private function getPropertyAccessor() { if (isset($this->accessor)) { return $this->accessor; } $accessorBuilder = PropertyAccess::createPropertyAccessorBuilder(); $this->accessor = $accessorBuilder->getPropertyAccessor(); return $this->accessor; }
/** * @return \Symfony\Component\PropertyAccess\PropertyAccessorInterface */ private function createPropertyAccessor() { $accessorBuilder = PropertyAccess::createPropertyAccessorBuilder(); $accessorBuilder->enableMagicCall(); return $accessorBuilder->getPropertyAccessor(); }
/** * Returns property accessor instance. * * @return PropertyAccessorInterface */ private function getAccessor() { if (!$this->accessor) { $this->accessor = PropertyAccess::createPropertyAccessorBuilder()->enableExceptionOnInvalidIndex()->enableMagicCall()->getPropertyAccessor(); } return $this->accessor; }
/** * Edit a job instance * * @param Request $request * @param int $id * * @return Response */ public function editAction(Request $request, $id) { try { $jobInstance = $this->getJobInstance($id); } catch (NotFoundHttpException $e) { $this->request->getSession()->getFlashBag()->add('error', new Message($e->getMessage())); return $this->redirectToIndexView(); } $this->eventDispatcher->dispatch(JobProfileEvents::PRE_EDIT, new GenericEvent($jobInstance)); $form = $this->formFactory->create($this->jobInstanceFormType, $jobInstance, ['method' => 'PATCH']); if ($request->isMethod('PATCH')) { $form->handleRequest($request); if ($form->isValid()) { $this->entityManager->persist($jobInstance); $this->entityManager->flush(); $this->request->getSession()->getFlashBag()->add('success', new Message(sprintf('flash.%s.updated', $this->getJobType()))); return $this->redirectToShowView($jobInstance->getId()); } } $this->eventDispatcher->dispatch(JobProfileEvents::POST_EDIT, new GenericEvent($jobInstance)); $job = $this->jobRegistry->get($jobInstance->getJobName()); $errors = []; $accessor = PropertyAccess::createPropertyAccessorBuilder()->getPropertyAccessor(); foreach ($form->getErrors() as $error) { if (0 === strpos($error->getCause()->getPropertyPath(), 'children[parameters].children[filters].data')) { $propertyPath = substr($error->getCause()->getPropertyPath(), strlen('children[parameters].children[filters].data')); $accessor->setValue($errors, $propertyPath, $error->getMessage()); } } return $this->templating->renderResponse($this->jobTemplateProvider->getEditTemplate($jobInstance), ['jobInstance' => $jobInstance, 'job' => $job, 'form' => $form->createView(), 'errors' => $errors]); }
public function parse($content) { $content = $this->parseFrontmatter($content); if (!($headParser = $this->findParser($this->headParser))) { throw new ParserNotFoundException($this->headParser, 'head'); } $head = $headParser->parse($content['head']); // find body parser in head data $bodyParserName = null; if ($this->bodyFormatPath) { $accessor = PropertyAccess::createPropertyAccessorBuilder()->enableMagicCall()->getPropertyAccessor(); $bodyParserName = $accessor->getValue($head, $this->bodyFormatPath); } $bodyParserName = $bodyParserName ?: $this->bodyParser; if (!($bodyParser = $this->findParser($bodyParserName))) { throw new ParserNotFoundException($bodyParserName, 'body'); } $body = $bodyParser->parse($content['body']); return new Frontmatter($head, $body); }
/** * Returns property accessor instance. * * @return PropertyAccessor */ private function getPropertyAccessor() { if (!$this->propertyAccessor) { $this->propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()->enableMagicCall()->getPropertyAccessor(); } return $this->propertyAccessor; }
protected function createPropertyAccessor() : PropertyAccessorInterface { return new StdPropertyAccessor(PropertyAccess::createPropertyAccessorBuilder()->enableMagicCall()->getPropertyAccessor()); }
protected function initialize() { $this->propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()->getPropertyAccessor(); }