/**
  * {@inheritDoc}
  */
 public function check($object)
 {
     if (!$object instanceof EnabledIndicateInterface) {
         throw UnexpectedTypeException::create($object, 'FivePercent\\Component\\EnabledChecker\\EnabledIndicateInterface');
     }
     return $object->isEnabled();
 }
 /**
  * {@inheritDoc}
  */
 public function resolve(ActionInterface $action)
 {
     if (!$action instanceof CallableAction) {
         throw UnexpectedTypeException::create($action, 'FivePercent\\Component\\Api\\SMD\\Action\\CallableAction');
     }
     $callable = $action->getCallable();
     $object = null;
     if ($callable instanceof \Closure) {
         $reflection = new \ReflectionFunction($action->getCallable());
     } else {
         if (is_array($callable)) {
             if (is_object($callable[0])) {
                 $object = $callable[0];
                 $reflection = new \ReflectionMethod(get_class($callable[0]), $callable[1]);
             } else {
                 $reflection = new \ReflectionMethod($callable[0], $callable[1]);
             }
         } else {
             if (function_exists($callable)) {
                 $reflection = new \ReflectionFunction($callable);
             } else {
                 throw new \RuntimeException(sprintf('Could not resolve reflection for callable "%s".', is_object($callable) ? get_class($callable) : gettype($callable)));
             }
         }
     }
     return new BaseCallable($reflection, $object);
 }
 /**
  * Construct
  *
  * @param object                      $object
  * @param TransformerContextInterface $transformerContext
  * @param NormalizerContextInterface  $normalizerContext
  *
  * @throws UnexpectedTypeException
  */
 public function __construct($object, TransformerContextInterface $transformerContext = null, NormalizerContextInterface $normalizerContext = null)
 {
     if (!is_object($object)) {
         throw UnexpectedTypeException::create($object, 'object');
     }
     $this->action = self::ACTION_TRANSFORM | self::ACTION_NORMALIZE;
     $this->object = $object;
 }
 /**
  * {@inheritDoc}
  */
 public function recreate($className, $data)
 {
     if (!is_array($data)) {
         throw UnexpectedTypeException::create($data, 'array');
     }
     if (empty($data['serialized'])) {
         throw new ObjectRecreateException('Missing requires parameter "serialized".');
     }
     $object = unserialize($data['serialized']);
     return $object;
 }
 /**
  * {@inheritDoc}
  */
 public function resolve(ActionInterface $action)
 {
     if (!$action instanceof ServiceAction) {
         throw UnexpectedTypeException::create($action, 'FivePercent\\Bundle\\ApiBundle\\SMD\\Action\\ServiceAction');
     }
     $serviceId = $action->getServiceId();
     $method = $action->getMethod();
     $service = $this->container->get($serviceId);
     $reflectionService = Reflection::loadClassReflection($service);
     $reflectionMethod = $reflectionService->getMethod($method);
     return new BaseCallable($reflectionMethod, $service);
 }
 /**
  * {@inheritDoc}
  */
 public function convertProperties($object, $group)
 {
     if (!is_object($object)) {
         throw UnexpectedTypeException::create($object, 'object');
     }
     $converter = $this->getConverter($group);
     $classReflection = Reflection::loadClassReflection($object);
     $properties = $classReflection->getProperties();
     foreach ($properties as $property) {
         if ($converter->isSupported($property)) {
             $this->convertPropertyReflection($object, $property, $group);
         }
     }
 }
 /**
  * {@inheritDoc}
  */
 public function normalize($object, ContextInterface $context = null)
 {
     if (!is_object($object)) {
         throw UnexpectedTypeException::create($object, 'object');
     }
     if (!$context) {
         $context = new Context();
     }
     $normalizer = $this->getNormalizerForClass(get_class($object));
     if ($normalizer instanceof ModelNormalizerManagerAwareInterface) {
         $normalizer->setModelNormalizerManager($this);
     }
     return $normalizer->normalize($object, $context);
 }
 /**
  * {@inheritDoc}
  */
 public function transform($object, ContextInterface $context = null)
 {
     if (!is_object($object)) {
         throw UnexpectedTypeException::create($object, 'object');
     }
     if (!$context) {
         $context = new Context();
     }
     $transformer = $this->getTransformerForClass($object);
     if ($transformer instanceof ModelTransformerManagerAwareInterface) {
         $transformer->setModelTransformerManager($this);
     }
     $transformed = $transformer->transform($object, $context);
     return $transformed;
 }
 /**
  * {@inheritDoc}
  */
 public function map(PropertyMetadata $property, $object, $value)
 {
     if (!is_object($object)) {
         throw UnexpectedTypeException::create($object, 'object');
     }
     if (!$property->reflection) {
         $objectReflection = Reflection::loadObjectReflection($object);
         $propertyName = $property->getPropertyName();
         $propertyReflection = $objectReflection->getProperty($propertyName);
         if (!$propertyReflection->isPublic()) {
             $propertyReflection->setAccessible(true);
         }
         $property->reflection = $propertyReflection;
     }
     $property->reflection->setValue($object, $value);
 }
 /**
  * {@inheritDoc}
  */
 public function vote(Rule $rule, array $attributes = array())
 {
     if (!$rule instanceof CallbackRule) {
         throw UnexpectedTypeException::create($rule, CallbackRule::class);
     }
     $arguments = $rule->getArguments();
     foreach ($arguments as $key => $value) {
         $arguments[$key] = $this->transformParameter($this->expressionLanguage, $value, $attributes);
     }
     $result = call_user_func_array($rule->getCallback(), $arguments);
     if ($result === true) {
         return self::ACCESS_GRANTED;
     } elseif ($result === false) {
         return self::ACCESS_DENIED;
     } else {
         return self::ACCESS_ABSTAIN;
     }
 }
 /**
  * {@inheritDoc}
  */
 public function vote(Rule $rule, array $attributes = array())
 {
     if (!$rule instanceof RoleRule) {
         throw UnexpectedTypeException::create($rule, RoleRule::class);
     }
     $object = null;
     if ($rule->getObject()) {
         $object = $this->transformParameter($this->expressionLanguage, $rule->getObject(), $attributes);
         if (null !== $object && !is_object($object)) {
             throw new RuleVotingException(sprintf('Can not transform object from parameter "%s" for RoleRuleVoter. ' . 'Available keys in attributes: "%s". Returned value: "%s" but this is not object. ' . 'Maybe incorrect evaluate?', $object, implode('", "', array_keys($attributes)), is_scalar($object) ? $object : gettype($object)));
         }
     }
     $status = $this->authorizationChecker->isGranted($rule->getRoles(), $object);
     if ($status === true) {
         return self::ACCESS_GRANTED;
     } elseif ($status === false) {
         return self::ACCESS_DENIED;
     } else {
         return self::ACCESS_ABSTAIN;
     }
 }
 /**
  * {@inheritDoc}
  */
 public function check($object)
 {
     if (!is_object($object)) {
         throw UnexpectedTypeException::create($object, 'object');
     }
     if (!$this->isSupported($object)) {
         throw new NotSupportedException(sprintf('The object "%s" not supported for check "enabled" status.', get_class($object)));
     }
     if (!$this->checker->check($object)) {
         $code = 0;
         if ($object instanceof ExceptionAwareInterface) {
             $exception = $object->getExceptionForNotEnabled();
             if ($exception instanceof \Exception) {
                 throw $exception;
             }
             if ($this->logger) {
                 $this->logger->warning(sprintf('The method %s::%s should be return \\Exception instance, but returned "%s".', get_class($object), 'getExceptionForNotEnabled', is_object($object) ? get_class($object) : gettype($object)));
             }
         }
         throw new NotEnabledException(sprintf('The object %s::%s not enabled.', get_class($object), spl_object_hash($object)), $code);
     }
 }
 /**
  * {@inheritDoc}
  */
 public function validateObjectByVarTags($object)
 {
     if (!is_object($object)) {
         throw UnexpectedTypeException::create($object, 'object');
     }
     $classMetadata = $this->metadataFactory->loadMetadata(get_class($object));
     $constraintViolationList = new ConstraintViolationList();
     foreach ($classMetadata->getProperties() as $propertyName => $propertyMetadata) {
         $availableVarTypes = $propertyMetadata->getTypes();
         if (!count($availableVarTypes)) {
             continue;
         }
         $violationListForProperty = $this->validatePropertyValueByTypes($object, $propertyName, $availableVarTypes);
         if ($violationListForProperty && count($violationListForProperty)) {
             foreach ($violationListForProperty as $violationForProperty) {
                 // Recreate violation for save property path
                 $violation = new ConstraintViolation($violationForProperty->getMessage(), $violationForProperty->getMessageTemplate(), $violationForProperty->getMessageParameters(), $violationForProperty->getRoot(), $propertyName, $violationForProperty->getInvalidValue(), $violationForProperty->getMessagePluralization(), $violationForProperty->getCode());
                 $constraintViolationList->add($violation);
             }
         }
     }
     return $constraintViolationList;
 }
Beispiel #14
0
 /**
  * {@inheritDoc}
  */
 public function notify($object, $onEvent = null)
 {
     if (!is_object($object)) {
         throw UnexpectedTypeException::create($object, 'object');
     }
     $notifyClass = get_class($object);
     if (!$this->supportsObject($object)) {
         throw new ClassNotSupportedException(sprintf('The object instance "%s" not supports send notification with event "%s".', $notifyClass, $onEvent ? $onEvent : 'NULL'));
     }
     $classMetadata = $this->metadataFactory->loadMetadata($notifyClass);
     $metadata = $this->getNotificationMetadataForEvent($classMetadata, $onEvent, $notifyClass);
     $notification = $this->createNotification($metadata, $object);
     $this->senderStrategyManager->getStrategy($metadata->getStrategy())->sendNotification($this->sender, $notification);
 }
 /**
  * Process transform object response
  *
  * @param ObjectResponse $objectResponse
  *
  * @return Response
  */
 private function doTransformObjectResponse(ObjectResponse $objectResponse)
 {
     $responseData = $objectResponse;
     if ($objectResponse->isActionTransform()) {
         try {
             $responseData = $this->transformerManager->transform($responseData->getObject(), $objectResponse->getTransformerContext());
             if (!is_object($responseData)) {
                 throw UnexpectedTypeException::create($responseData, 'object');
             }
         } catch (TransformerUnsupportedObjectException $e) {
             throw new \RuntimeException(sprintf('Can not transform object with class "%s".', get_class($objectResponse)), 0, $e);
         }
     }
     try {
         $responseData = $this->normalizerManager->normalize($responseData instanceof ObjectResponse ? $responseData->getObject() : $responseData, $objectResponse->getNormalizerContext());
         if (!is_array($responseData)) {
             throw UnexpectedTypeException::create($responseData, 'array');
         }
     } catch (NormalizerUnsupportedObjectException $e) {
         throw new \RuntimeException(sprintf('Can not normalize object with class "%s".', get_class($responseData)), 0, $e);
     }
     if ($objectResponse->isEmptyResponse()) {
         $response = new EmptyResponse($responseData, $objectResponse->getHttpStatusCode());
     } else {
         $response = new Response($responseData, $objectResponse->getHttpStatusCode());
     }
     return $response;
 }
 /**
  * Map process
  *
  * @param object         $object
  * @param array          $parameters
  * @param string         $group
  * @param array          $paths
  *
  * @return object
  *
  * @throws MapException
  * @throws ObjectNotSupportedException
  */
 protected function doMap($object, array $parameters, $group, array $paths)
 {
     if (!is_object($object)) {
         throw UnexpectedTypeException::create($object, 'object');
     }
     $metadata = $this->metadataFactory->load($object, $group);
     if (!$metadata) {
         throw new ObjectNotSupportedException(sprintf('The object with class "%s" not supported for mapping in group "%s".', get_class($object), $group));
     }
     $strategy = $this->strategyManager->getStrategy($metadata->getStrategy());
     foreach ($metadata->getProperties() as $propertyMetadata) {
         if (!isset($parameters[$propertyMetadata->getFieldName()])) {
             // Not found parameter for property
             continue;
         }
         // Add path name to paths stack
         $paths[] = $propertyMetadata->getFieldName();
         $value = $parameters[$propertyMetadata->getFieldName()];
         if ($propertyMetadata->getCollectionMetadata()) {
             // Map as new collection
             $value = $this->doMapCollection($propertyMetadata, $value, $group, $paths);
         } else {
             if ($propertyMetadata->getClass()) {
                 // Map as new model
                 $value = $this->doMapNewObject($propertyMetadata, $value, $group, $paths);
             }
         }
         $strategy->map($propertyMetadata, $object, $value);
         // Pop path name from paths stack
         array_pop($paths);
     }
     return $object;
 }
 /**
  * {@inheritDoc}
  */
 public function denormalize($data, ContextInterface $context)
 {
     $class = $context->getAttribute('_class');
     if (!$class) {
         throw new DenormalizationFailedException('Undefined class for denormalization');
     }
     $metadata = $this->metadataFactory->loadMetadata($class);
     if ($context->getGroups()) {
         $denormalizeProperties = $metadata->getPropertiesForGroups($context->getGroups());
     } else {
         $denormalizeProperties = $metadata->getProperties();
     }
     $classReflection = Reflection::loadClassReflection($class);
     if ($object = $context->getAttribute('_object')) {
         if (!is_object($object)) {
             throw UnexpectedTypeException::create($object, 'object');
         }
         if (get_class($object) != $classReflection && !is_a($object, $class)) {
             throw UnexpectedTypeException::create($object, $class);
         }
     } else {
         $object = $classReflection->newInstanceWithoutConstructor();
     }
     foreach ($denormalizeProperties as $denormalizePropertyName => $propertyMetadata) {
         $fieldName = $propertyMetadata->getFieldName();
         if (!isset($data[$fieldName])) {
             continue;
         }
         $objectPropertyReflection = $classReflection->getProperty($denormalizePropertyName);
         if (!$objectPropertyReflection->isPublic()) {
             $objectPropertyReflection->setAccessible(true);
         }
         $denormalizedValue = $this->denormalizeValue($data[$fieldName], $propertyMetadata, $objectPropertyReflection);
         $objectPropertyReflection->setValue($object, $denormalizedValue);
     }
     return $object;
 }