/** * {@inheritdoc} */ public function isReadable($node, $singlePath) { if (!$singlePath) { return false; } return $this->propertyAccess->isReadable($node, $singlePath); }
/** * @param FormView $formView * @param array $data * * @return FormView */ public function formAttributes(FormView $formView, array $data) { $formView = clone $formView; foreach ($data as $key => $value) { $path = 'children[' . str_replace('.', '].children[', $key) . ']'; if (false === $this->accessor->isReadable($formView, $path)) { continue; } /** @var FormView $field */ $field = $this->accessor->getValue($formView, $path); if (false === $field instanceof FormView) { throw new \RuntimeException("Cannot set form attribute: {$key} is not a FormView instance"); } if (is_string($value)) { $value = ["class" => " {$value}"]; } if (false === isset($field->vars['attr'])) { $field->vars['attr'] = []; } foreach ($value as $name => $attribute) { if (isset($field->vars['attr'][$name]) && " " === substr($attribute, 0, 1)) { $attribute = $field->vars['attr'][$name] . $attribute; } $field->vars['attr'][$name] = trim($attribute); } } return $formView; }
/** * Applies the given mapping on a given object or array. * * @param object|array $raw The input object or array * @param array $mapping The mapping * @param object|array $transformed The output object or array. * @return array */ public function transform($raw, array $mapping, $transformed = []) { foreach ($mapping as $destination => $source) { $value = $this->propertyAccessor->isReadable($raw, $source) ? $this->propertyAccessor->getValue($raw, $source) : null; $this->propertyAccessor->setValue($transformed, $destination, $value); } return $transformed; }
/** * Supports anything that's mapped or readable directly. * * {@inheritdoc} */ public function supportsParameter($name, $object) { if (!is_object($object)) { return false; } $class = get_class($object); return isset($this->mapping[$class][$name]) || isset($this->mapping[$class]['_fallback']) || is_string($name) && $this->propertyAccessor->isReadable($object, $name); }
/** * preUpdate * * @param LifecycleEventArgs $event */ public function preUpdate(LifecycleEventArgs $event) { if ($this->accessor->isReadable($this->context, $this->property) && $event->getObject() == $this->accessor->getValue($this->context, $this->property)) { if (($resource = $event->getObject()) && $resource instanceof ResourceObjectInterface) { $this->loader->load($this->context, $this->property, $this->file); } } }
private function createDataProviderPath(ColumnInterface $column, $data, $customPath) : PropertyPath { try { if (null === $customPath) { $name = $column->getName(); if (!$this->propertyAccessor->isReadable($data, $path = new PropertyPath(sprintf('[%s]', $name))) && !$this->propertyAccessor->isReadable($data, $path = new PropertyPath($name))) { throw DataProviderException::autoAccessorUnableToGetValue($name); } return $path; } if (!$this->propertyAccessor->isReadable($data, $path = new PropertyPath($customPath))) { throw DataProviderException::pathAccessorUnableToGetValue($column->getName(), $path); } return $path; } catch (InvalidPropertyPathException $e) { throw DataProviderException::invalidPropertyPath($column->getName(), $e); } }
/** * @param $object * @param $path * * @return bool */ public function hasPropertyFunction($object, $path) { if (null == $this->accessor) { $this->accessor = new PropertyAccessor(); } static $hasIsReadableMethod = null; if (null === $hasIsReadableMethod) { $hasIsReadableMethod = method_exists($this->accessor, 'isReadable'); } if ($hasIsReadableMethod) { return $this->accessor->isReadable($object, $path); } else { try { $this->accessor->getValue($object, $path); return true; } catch (\Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException $ex) { return false; } } }
/** * {@inheritdoc} */ public function buildResponse(AutocompleteResults $results, AutocompleteContextInterface $context) { $array = []; $property = $context->getParameter('autocomplete'); $accessor = new PropertyAccessor(); foreach ($results as $id => $result) { if ($accessor->isReadable($result, $property)) { $array[] = $accessor->getValue($result, $property); } } return new JsonResponse($array); }
/** * Sets errors on element * * @param array $messages * @param ElementInterface $element */ protected function mapMessagesToElement(array $messages, ElementInterface $element) { if ($element->hasPropertyPath()) { $propertyPathParts = explode('.', $element->getPropertyPath(false)); $propertyPath = $this->buildPath($propertyPathParts); if ($this->propertyAccessor->isReadable($messages, $propertyPath)) { $errors = $this->propertyAccessor->getValue($messages, $propertyPath); $element->setError($errors); } } $children = $element->getChildren(); if ($children->count()) { $this->mapMessagesToElementCollection($messages, $children); } }
/** * {@inheritdoc} */ public function buildResponse(AutocompleteResults $results, AutocompleteContextInterface $context) { $accessor = new PropertyAccessor(); $data['results'] = []; $property = $context->getParameter('autocomplete'); foreach ($results as $id => $result) { if ($accessor->isReadable($result, $property)) { $value = $accessor->getValue($result, $property); $data['results'][] = ['id' => $value, 'text' => $value]; } } if ($max = $context->getParameter('max_results', false)) { $data['pagination'] = ['more' => $results->count() == $max]; } return new JsonResponse($data); }
/** * {@inheritDoc} */ public function refreshUser(UserInterface $user) { // Compatibility with FOSUserBundle < 2.0 if (class_exists('FOS\\UserBundle\\Form\\Handler\\RegistrationFormHandler')) { return $this->userManager->refreshUser($user); } $identifier = $this->properties['identifier']; if (!$user instanceof User || !$this->accessor->isReadable($user, $identifier)) { throw new UnsupportedUserException(sprintf('Expected an instance of FOS\\UserBundle\\Model\\User, but got "%s".', get_class($user))); } $userId = $this->accessor->getValue($user, $identifier); if (null === ($user = $this->userManager->findUserBy(array($identifier => $userId)))) { throw new UsernameNotFoundException(sprintf('User with ID "%d" could not be reloaded.', $userId)); } return $user; }
/** * @dataProvider getPathsWithUnexpectedType */ public function testIsReadableReturnsFalseIfNotObjectOrArray($objectOrArray, $path) { $this->assertFalse($this->propertyAccessor->isReadable($objectOrArray, $path)); }
/** * @Then the certificate for the domain :domain should contains: */ public function theCertificateForTheDomainShouldContains($domain, PyStringNode $content) { $certFile = $this->storageDir . '/domains/' . $domain . '/cert.pem'; $parser = new CertificateParser(); $certificateMetadata = $parser->parse(file_get_contents($certFile)); $accessor = new PropertyAccessor(); $yaml = new Yaml(); $expected = $yaml->parse($content->getRaw()); foreach ($expected as $key => $value) { PHPUnit_Framework_Assert::assertTrue($accessor->isReadable($certificateMetadata, $key)); $formattedValue = $accessor->getValue($certificateMetadata, $key); if (is_array($value) && is_array($formattedValue)) { sort($value); sort($formattedValue); } PHPUnit_Framework_Assert::assertSame($value, $formattedValue); } }
public function testIsReadableThrowsExceptionIfEmpty() { $this->assertFalse($this->propertyAccessor->isReadable('', 'foobar')); }
/** * parse name and replace tokens with context properties, ej. logo_{id} --> logo_1 * * @param $name * @param object $context * @param object $defaultName * * @return mixed */ protected function parseName($name, $context, $defaultName = null) { if ($context) { preg_match_all('/\\{([\\w\\.]+)\\}/', $name, $matches); $accessor = new PropertyAccessor(); if (isset($matches[1])) { foreach ($matches[1] as $token) { if ($accessor->isReadable($context, $token)) { $value = str_replace(' ', '_', $accessor->getValue($context, $token)); $name = str_replace("{{$token}}", $value, $name); } } } //render default file name into {} $defaultName = str_replace(' ', '_', Inflector::tableize($defaultName)); $name = str_replace('{}', $defaultName, $name); //unique id for each token {*} while (strpos($name, '{*}') !== false) { $name = preg_replace('/\\{\\*\\}/', substr(sha1(uniqid(mt_rand())), 0, 8), $name, 1); } } return $name; }
/** * @dataProvider getValidPropertyPaths */ public function testIsReadable($collection, $path) { $this->assertTrue($this->propertyAccessor->isReadable($collection, $path)); }
/** * @inheritdoc */ public function getUrl(ResourceObjectInterface $resource) { $url = $this->getLocationConfig('url', $resource->getLocation(), $this->config); preg_match_all('/\\{(\\w+)\\}/', $url, $matches); $accessor = new PropertyAccessor(); if (isset($matches[1])) { foreach ($matches[1] as $token) { if ($token === 'id') { //when mapping information contains {id} //for security reasons instead of set the real resource id //set a random value and save in session with the real id //the param converter resolve the real resource related for given hash //and keep the resource private for non public access $value = md5(mt_rand()); $this->session->set('_resource/' . $value, $resource->getId()); } else { if ($accessor->isReadable($resource, $token)) { $value = $accessor->getValue($resource, $token); } else { $msg = sprintf('Invalid parameter "{%s}" in %s resource mapping.', $token, $resource->getLocation()); throw new \InvalidArgumentException($msg); } } $url = str_replace("{{$token}}", $value, $url); } } return str_replace('//', '/', $url); }
/** * {@inheritdoc} */ public function isReadable($object, $propertyPath) { return $this->accessor->isReadable($object, $propertyPath); }
/** * {@inheritDoc} */ public function offsetExists($id) { return $this->accessor->isReadable($this->object, $id); }