Esempio n. 1
0
 /**
  * Simple test to see if array values are of specified type.
  *
  * @param  array $array Array of values.
  * @param  string $type Type to check
  * @param  string $className Named class, if type == 'class'
  * @return boolean Returns true is all values match type, otherwise false.
  */
 public static function arrayValuesCheck($array, $type, $className = '')
 {
     $status = true;
     if (is_array($array) && is_string($type)) {
         if ($type === 'class' && is_string($className) && !empty($className)) {
             foreach ($array as $item) {
                 if (!is_null($item)) {
                     if (!is_object($item)) {
                         $status = false;
                     } else {
                         $class = new \ReflectionClass($item);
                         if (!preg_match("/{$className}/", $class->getShortname())) {
                             $status = false;
                         }
                     }
                 }
             }
         } else {
             foreach ($array as $item) {
                 $function = 'is_' . $type;
                 if (function_exists($function)) {
                     if ($function($item) === false) {
                         $status = false;
                     }
                 } else {
                     $status = false;
                 }
             }
         }
     } else {
         $status = false;
     }
     return $status;
 }
 /**
  * {@inheritdoc}
  */
 public function normalize($violationList, $format = null, array $context = [])
 {
     if ($violationList instanceof \Exception) {
         if ($this->debug) {
             $trace = $violationList->getTrace();
         }
     }
     $data = ['@context' => '/api/contexts/ConstraintViolationList', '@type' => 'ConstraintViolationList', 'title' => 'An error occurred', 'violations' => []];
     foreach ($violationList as $violation) {
         $key = $violation->getPropertyPath();
         $invalidValue = $violation->getInvalidValue();
         if (method_exists($violation->getRoot(), '__toString')) {
             $invalidValue = $this->propertyAccessor->getValue($violation->getRoot(), $violation->getPropertyPath());
         }
         if ($violation->getConstraint() instanceof UniqueEntity) {
             $class = method_exists($violation->getRoot(), 'getConfig') ? $violation->getRoot()->getConfig() : $violation->getRoot();
             $reflexion = new \ReflectionClass($class);
             $key = strtolower($reflexion->getShortname());
         }
         $data['violations'][$key][] = ['property' => $violation->getPropertyPath(), 'invalidValue' => $invalidValue, 'message' => $violation->getMessage()];
     }
     if (isset($trace)) {
         $data['trace'] = $trace;
     }
     return $data;
 }
Esempio n. 3
0
 /**
  * Builds the ConfigOptions object.
  *
  * Passing an array of key value pairs will set the configuration for each
  * child object created from this parent object.
  *
  * @param  mixed                 $child  Child ConfigOption object.
  * @param  array                 $config Array of options.
  * @throws InvalidConfigValue
  * @throws InvalidConfigProperty
  * @return mixed
  */
 public function __construct($child, $config)
 {
     $class = new \ReflectionClass($child);
     $this->type = $class->getShortname();
     $this->options = array_map(function ($prop) {
         return $prop->name;
     }, $class->getProperties(\ReflectionProperty::IS_PUBLIC));
     if (is_array($config)) {
         foreach ($config as $option => $value) {
             if (in_array($option, $this->options)) {
                 $this->{$option}($value);
             } else {
                 throw new InvalidConfigProperty($this->type, __FUNCTION__, $option, $this->options);
             }
         }
     } else {
         throw new InvalidConfigValue(__FUNCTION__, 'array', 'with valid keys as ' . Utils::arrayToPipedString($this->options));
     }
 }