Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function dump(array $exclude = [])
 {
     $array = array_diff_key(array_filter(get_object_vars($this), function ($value) {
         return $value || is_bool($value);
     }), array_flip(array_merge(['name', 'objectName', 'multiple'], $exclude)));
     return array_combine(array_map(function ($key) {
         return Caser::snake($key);
     }, array_keys($array)), array_values($array));
 }
 /**
  * Finds aliases for every property used in document including parent classes.
  *
  * @param \ReflectionClass $reflectionClass
  *
  * @return array
  */
 private function getAliases(\ReflectionClass $reflectionClass)
 {
     $reflectionName = $reflectionClass->getName();
     if (array_key_exists($reflectionName, $this->aliases)) {
         return $this->aliases[$reflectionName];
     }
     $alias = [];
     /** @var \ReflectionProperty[] $properties */
     $properties = $this->getDocumentPropertiesReflection($reflectionClass);
     foreach ($properties as $name => $property) {
         $type = $this->getPropertyAnnotationData($property);
         if ($type !== null) {
             $alias[$type->name] = ['propertyName' => $name, 'type' => $type->type];
             switch (true) {
                 case $property->isPublic():
                     $propertyType = 'public';
                     break;
                 case $property->isProtected():
                 case $property->isPrivate():
                     $propertyType = 'private';
                     $camelCaseName = ucfirst(Caser::camel($name));
                     if ($reflectionClass->hasMethod('get' . $camelCaseName) && $reflectionClass->hasMethod('set' . $camelCaseName)) {
                         $alias[$type->name]['methods'] = ['getter' => 'get' . $camelCaseName, 'setter' => 'set' . $camelCaseName];
                     } else {
                         $message = sprintf('Missing %s() method in %s class. Add it, or change property to public.', $name, $reflectionName);
                         throw new \LogicException($message);
                     }
                     break;
                 default:
                     $message = sprintf('Wrong property %s type of %s class types cannot ' . 'be static or abstract.', $name, $reflectionName);
                     throw new \LogicException($message);
             }
             $alias[$type->name]['propertyType'] = $propertyType;
             if ($type->objectName) {
                 $child = new \ReflectionClass($this->finder->getNamespace($type->objectName));
                 $alias[$type->name] = array_merge($alias[$type->name], ['multiple' => $type instanceof Property ? $type->multiple : false, 'aliases' => $this->getAliases($child), 'namespace' => $child->getName()]);
             }
         }
     }
     $this->aliases[$reflectionName] = $alias;
     return $this->aliases[$reflectionName];
 }
 /**
  * Checks if class have setter and getter, and returns them in array.
  *
  * @param \ReflectionClass $reflectionClass
  * @param string           $property
  *
  * @return array
  */
 private function getMutatorMethods(\ReflectionClass $reflectionClass, $property, $propertyType)
 {
     $camelCaseName = ucfirst(Caser::camel($property));
     $setterName = 'set' . $camelCaseName;
     if (!$reflectionClass->hasMethod($setterName)) {
         $message = sprintf('Missing %s() method in %s class. Add it, or change property to public.', $setterName, $reflectionClass->getName());
         throw new \LogicException($message);
     }
     if ($reflectionClass->hasMethod('get' . $camelCaseName)) {
         return ['getter' => 'get' . $camelCaseName, 'setter' => $setterName];
     }
     if ($propertyType === 'boolean') {
         if ($reflectionClass->hasMethod('is' . $camelCaseName)) {
             return ['getter' => 'is' . $camelCaseName, 'setter' => $setterName];
         }
         $message = sprintf('Missing %s() or %s() method in %s class. Add it, or change property to public.', 'get' . $camelCaseName, 'is' . $camelCaseName, $reflectionClass->getName());
         throw new \LogicException($message);
     }
     $message = sprintf('Missing %s() method in %s class. Add it, or change property to public.', 'get' . $camelCaseName, $reflectionClass->getName());
     throw new \LogicException($message);
 }