private function getArguments($payload) { if ($this->accessor->isReadable($payload, '[args]')) { return $this->accessor->getValue($payload, '[args]'); } if ($this->accessor->isReadable($payload, 'args')) { return $this->accessor->getValue($payload, 'args'); } return []; }
private function resolveByPath($payload, $callablePath, $argsPath) { if (!$this->accessor->isReadable($payload, $callablePath)) { return; } $id = $this->accessor->getValue($payload, $callablePath); if (!is_string($id)) { return; } $args = $this->accessor->isReadable($payload, $argsPath) ? $this->accessor->getValue($payload, $argsPath) : []; return [$this->container[$this->idPrefix . $id], $args]; }
private function createProcess($payload) { if ($payload instanceof Process) { return $payload; } if (is_string($payload)) { return new Process($payload); } if ($this->accessor->isReadable($payload, '[commandline]')) { return new Process($this->accessor->getValue($payload, '[commandline]')); } if ($this->accessor->isReadable($payload, 'commandline')) { return new Process($this->accessor->getValue($payload, 'commandline')); } throw new \InvalidArgumentException('Unsupported payload type.'); }
/** * @param object $entity Entity * @param string $propertyPath Property path * * @return mixed * @throws \Darvin\Utils\CustomObject\CustomObjectException */ private function getPropertyValue($entity, $propertyPath) { if (!$this->propertyAccessor->isReadable($entity, $propertyPath)) { throw new CustomObjectException(sprintf('Property "%s::$%s" is not readable.', ClassUtils::getClass($entity), $propertyPath)); } return $this->propertyAccessor->getValue($entity, $propertyPath); }
/** * @param object $object Object to clone * * @return object * @throws \Darvin\Utils\Cloner\ClonerException */ private function cloneObject($object) { $objectHash = spl_object_hash($object); if (isset($this->clonedObjects[$objectHash])) { return $this->clonedObjects[$objectHash]; } $class = ClassUtils::getClass($object); if (false === strpos($class, '\\')) { $clone = clone $object; $this->clonedObjects[$objectHash] = $clone; return $clone; } $meta = $this->extendedMetadataFactory->getExtendedMetadata($class); if (!isset($meta['clonable'])) { $message = sprintf('Class "%s" must be annotated with "%s" annotation in order to create clone of it\'s instance.', $class, Clonable::ANNOTATION); throw new ClonerException($message); } $clone = new $class(); $this->clonedObjects[$objectHash] = $clone; foreach ($meta['clonable']['properties'] as $property) { if (!$this->propertyAccessor->isReadable($object, $property)) { throw new ClonerException(sprintf('Property "%s::$%s" is not readable.', $class, $property)); } if (!$this->propertyAccessor->isWritable($clone, $property)) { throw new ClonerException(sprintf('Property "%s::$%s" is not writable.', $class, $property)); } $value = $this->propertyAccessor->getValue($object, $property); $valueCopy = $this->copyValue($value); $this->propertyAccessor->setValue($clone, $property, $valueCopy); } $this->eventDispatcher->dispatch(Events::POST_CLONE, new CloneEvent($object, $clone)); return $clone; }
/** * @param ClassMetadata $classMetadata * @param array $options * @return bool * @throws \InvalidArgumentException */ public function getResult(ClassMetadata $classMetadata, array $options = []) { if (!isset($options['property'])) { throw new \InvalidArgumentException('The property option must be specified!'); } if (!$this->propertyAccessor->isReadable($classMetadata->newInstance(), $options['property'])) { return false; } if ($annotation = $this->annotationQuery->getResult($classMetadata, ['property' => $options['property'], 'annotationClass' => 'Sentient\\Data\\Metadata\\Annotation\\InvisibleProperty', 'checkSetter' => false])) { return false; } if ($annotation = $this->annotationQuery->getResult($classMetadata, ['property' => $options['property'], 'annotationClass' => 'Sentient\\Data\\Metadata\\Annotation\\CrudProperty', 'checkSetter' => false])) { if (!empty($options['important'])) { return !empty($annotation->important); } return !empty($annotation->visible); } return true; }
/** * {@inheritdoc} */ public function getResult(ClassMetadata $classMetadata, array $options = []) { if (!isset($options['property'])) { throw new \InvalidArgumentException('The property option was not specified.'); } $instance = $classMetadata->newInstance(); if (!$this->propertyAccessor->isWritable($instance, $options['property']) || !$this->propertyAccessor->isReadable($instance, $options['property'])) { return false; } if (isset($options['create'])) { $create = $options['create']; unset($options['create']); } else { $create = true; } if ($annotation = $this->annotationQuery->getResult($classMetadata, array_merge($options, ['annotationClass' => 'Sentient\\Data\\Metadata\\Annotation\\LockedProperty']))) { if ($create ? $annotation->onCreate : $annotation->onUpdate) { return false; } } if ($annotation = $this->annotationQuery->getResult($classMetadata, array_merge($options, ['annotationClass' => 'Sentient\\Data\\Metadata\\Annotation\\CrudProperty']))) { if ($annotation->editable === CrudProperty::EDITABLE_ALWAYS) { return true; } if ($annotation->editable === CrudProperty::EDITABLE_NEVER) { return false; } if ($annotation->editable === CrudProperty::EDITABLE_ON_CREATE) { return $create; } if ($annotation->editable === CrudProperty::EDITABLE_ON_UPDATE) { return !$create; } throw new \RuntimeException('The editable property has an invalid value.'); } foreach ($this->bannedAnnotations as $annotationClass) { if ($this->annotationQuery->getResult($classMetadata, array_merge($options, compact('annotationClass')))) { return false; } } return true; }
/** * @param array $sourcePropertyPaths Source property paths * @param object $entity Entity * @param string $entityClass Entity class * * @return array * @throws \Darvin\Utils\DefaultValue\DefaultValueException */ private function getSourcePropertyValues(array $sourcePropertyPaths, $entity, $entityClass) { $sourcePropertyValues = []; foreach ($sourcePropertyPaths as $sourcePropertyPath) { if (!$this->propertyAccessor->isReadable($entity, $sourcePropertyPath)) { throw $this->createPropertyNotReadableException($entityClass, $sourcePropertyPath); } $sourcePropertyValues[$sourcePropertyPath] = $this->propertyAccessor->getValue($entity, $sourcePropertyPath); } return $sourcePropertyValues; }
/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { if (!$constraint instanceof UniqueDTOParams) { throw new UnexpectedTypeException($constraint, UniqueDTOParams::class); } /** @var ExecutionContextInterface $context */ $context = $this->context; if (!$context instanceof ExecutionContextInterface) { throw new UnexpectedTypeException($context, ExecutionContextInterface::class); } if (!is_object($value)) { throw new UnexpectedTypeException($value, 'object'); } $resolver = new OptionsResolver(); $resolver->setRequired(['field', 'entity']); $resolver->setDefined(['message', 'propertyPath', 'field', 'entity', 'manager']); $fieldConfig = $constraint->fieldConfig; if (0 === count($fieldConfig)) { throw new ConstraintDefinitionException(sprintf('Field "%s" at constraint "%s" must not have no items!', 'fieldConfig', UniqueDTOParams::class)); } $contextualValidator = $context->getValidator()->inContext($context); foreach ($constraint->fieldConfig as $configItem) { $item = $resolver->resolve($configItem); $options = []; if (isset($item['message'])) { $options['message'] = $item['message']; } $options['field'] = $item['field']; $options['entity'] = $item['entity']; if (isset($item['manager'])) { $options['manager'] = $item['manager']; } $options['propertyPath'] = $propertyPath = !isset($item['propertyPath']) ? $item['field'] : $item['propertyPath']; if (!$this->propertyAccess->isReadable($value, $propertyPath)) { throw new ConstraintDefinitionException(sprintf('The property path "%s" on object "%s" is not readable!', $propertyPath, get_class($value))); } $propertyValue = $this->propertyAccess->getValue($value, $propertyPath); $constraint = new UniqueProperty($options); $contextualValidator->validate($propertyValue, $constraint); } }
/** * @param \Kdyby\Doctrine\Entities\BaseEntity $object * @param string[] $keys * @return mixed[] */ public function getValuesByObject($object, $keys) { $values = array(); $metadata = $this->entityManager->getClassMetadata(get_class($object)); foreach ($metadata->getFieldNames() as $columnName) { try { if ($this->propertyAccessor->isReadable($object, $columnName)) { $values[$columnName] = $this->propertyAccessor->getValue($object, $columnName); } } catch (MemberAccessException $e) { } } foreach ($metadata->getAssociationMappings() as $association) { $columnName = $association['fieldName']; try { if ($this->propertyAccessor->isReadable($object, $columnName)) { $values[$columnName] = $this->propertyAccessor->getValue($object, $columnName); } } catch (MemberAccessException $e) { } } return $values; }
/** * @inheritdoc */ public function isReadable($objectOrArray, $propertyPath) { return $objectOrArray instanceof \stdClass ? isset($objectOrArray->{$propertyPath}) : $this->decoratedPropertyAccessor->isReadable($objectOrArray, $propertyPath); }
/** * Generate timestamp data used for cache management * * @param PropertyAccessorInterface $accessor * @param mixed $parentData * @return int */ private function generateTimestamp(PropertyAccessorInterface $accessor, $parentData) { if ($accessor->isReadable($parentData, 'updatedAt') && $accessor->getValue($parentData, 'updatedAt') instanceof \DateTime) { return $accessor->getValue($parentData, 'updatedAt')->format('U'); } return date('U'); }
public function assert($data) { $testCase = $this->requestHelper->getTestCase(); if ($this->getPath() === "") { $value = $data; } else { if ($this->getDoesNotExists()) { $testCase->assertFalse($this->propertyAccessor->isReadable($data, $this->path), "Property does exists.\nProperty path: " . $this->path . "\nData:\n" . json_encode($data, JSON_PRETTY_PRINT) . "\nBe careful for assoc array and object"); return $data; } $testCase->assertTrue($this->propertyAccessor->isReadable($data, $this->path), "Property does not exists.\nProperty path: " . $this->path . "\nData:\n" . json_encode($data, JSON_PRETTY_PRINT) . "\nBe careful for assoc array and object"); $value = $this->propertyAccessor->getValue($data, $this->path); } foreach ($this->assertions as $assertion) { list($method, $arguments) = $assertion; $reflectionMethod = new \ReflectionMethod(get_class($testCase), $method); $parameterName = self::$assertMethodParameterReplacements[$method]; $position = null; foreach ($reflectionMethod->getParameters() as $key => $parameter) { if ($parameter->getName() == $parameterName) { $position = $key; break; } } array_splice($arguments, $position, 0, array($value)); $reflectionMethod->invokeArgs($testCase, $arguments); } }