Exemple #1
0
 private function formatVar(array $value)
 {
     $r = array();
     $data = array_shift($value);
     if (empty($data)) {
         $r['type'] = 'mixed';
     } elseif ($data[0] == '$') {
         $r['name'] = substr($data, 1);
         $r['type'] = 'mixed';
     } else {
         $data = explode('|', $data);
         $r['type'] = count($data) == 1 ? $data[0] : $data;
     }
     if (isset($r['type']) && Text::endsWith($r['type'], '[]')) {
         $r[static::$embeddedDataName]['type'] = substr($r['type'], 0, -2);
         $r['type'] = 'array';
     }
     if ($value) {
         $r['description'] = implode(' ', $value);
     }
     return $r;
 }
 /**
  * Get the type and associated model
  *
  * @param ReflectionClass $class
  * @param array           $scope
  *
  * @throws RestException
  * @throws \Exception
  * @return array
  *
  * @access protected
  */
 protected static function getTypeAndModel(ReflectionClass $class, array $scope, $prefix = '', array $rules = array())
 {
     $className = $class->getName();
     $dataName = CommentParser::$embeddedDataName;
     if (isset(static::$models[$prefix . $className])) {
         return static::$models[$prefix . $className];
     }
     $children = array();
     try {
         if ($magic_properties = static::parseMagic($class, empty($prefix))) {
             foreach ($magic_properties as $prop) {
                 if (!isset($prop['name'])) {
                     throw new Exception('@property comment is not properly defined in ' . $className . ' class');
                 }
                 if (!isset($prop[$dataName]['label'])) {
                     $prop[$dataName]['label'] = Text::title($prop['name']);
                 }
                 if (isset(static::$fieldTypesByName[$prop['name']]) && $prop['type'] == 'string' && !isset($prop[$dataName]['type'])) {
                     $prop[$dataName]['type'] = static::$fieldTypesByName[$prop['name']];
                 }
                 $children[$prop['name']] = $prop;
             }
         } else {
             $props = $class->getProperties(ReflectionProperty::IS_PUBLIC);
             foreach ($props as $prop) {
                 $name = $prop->getName();
                 $child = array('name' => $name);
                 if ($c = $prop->getDocComment()) {
                     $child += Util::nestedValue(CommentParser::parse($c), 'var') ?: [];
                 } else {
                     $o = $class->newInstance();
                     $p = $prop->getValue($o);
                     if (is_object($p)) {
                         $child['type'] = get_class($p);
                     } elseif (is_array($p)) {
                         $child['type'] = 'array';
                         if (count($p)) {
                             $pc = reset($p);
                             if (is_object($pc)) {
                                 $child['contentType'] = get_class($pc);
                             }
                         }
                     }
                 }
                 $child += array('type' => isset(static::$fieldTypesByName[$child['name']]) ? static::$fieldTypesByName[$child['name']] : 'string', 'label' => Text::title($child['name']));
                 isset($child[$dataName]) ? $child[$dataName] += array('required' => true) : ($child[$dataName]['required'] = true);
                 if ($prop->class != $className && ($qualified = Scope::resolve($child['type'], $scope))) {
                     list($child['type'], $child['children']) = static::getTypeAndModel(new ReflectionClass($qualified), $scope);
                 } elseif (($contentType = Util::nestedValue($child, $dataName, 'type')) && ($qualified = Scope::resolve($contentType, $scope))) {
                     list($child['contentType'], $child['children']) = static::getTypeAndModel(new ReflectionClass($qualified), $scope);
                 }
                 $children[$name] = $child;
             }
         }
     } catch (Exception $e) {
         if (Text::endsWith($e->getFile(), 'CommentParser.php')) {
             throw new RestException(500, "Error while parsing comments of `{$className}` class. " . $e->getMessage());
         }
         throw $e;
     }
     if ($properties = Util::nestedValue($rules, 'properties')) {
         if (is_string($properties)) {
             $properties = array($properties);
         }
         $c = array();
         foreach ($properties as $property) {
             if (isset($children[$property])) {
                 $c[$property] = $children[$property];
             }
         }
         $children = $c;
     }
     if ($required = Util::nestedValue($rules, 'required')) {
         //override required on children
         if (is_bool($required)) {
             // true means all are required false means none are required
             $required = $required ? array_keys($children) : array();
         } elseif (is_string($required)) {
             $required = array($required);
         }
         $required = array_fill_keys($required, true);
         foreach ($children as $name => $child) {
             $children[$name][$dataName]['required'] = isset($required[$name]);
         }
     }
     static::$models[$prefix . $className] = array($className, $children, $prefix . $className);
     return static::$models[$prefix . $className];
 }
Exemple #3
0
 protected function _mapResources(array $allRoutes, array &$map, $version = 1)
 {
     foreach ($allRoutes as $fullPath => $routes) {
         $path = explode('/', $fullPath);
         $resource = isset($path[0]) ? $path[0] : '';
         if ($resource == 'resources' || Text::endsWith($resource, 'index')) {
             continue;
         }
         foreach ($routes as $httpMethod => $route) {
             if (in_array($httpMethod, static::$excludedHttpMethods)) {
                 continue;
             }
             if (!static::verifyAccess($route)) {
                 continue;
             }
             foreach (static::$excludedPaths as $exclude) {
                 if (empty($exclude)) {
                     if ($fullPath == $exclude) {
                         continue 2;
                     }
                 } elseif (Text::beginsWith($fullPath, $exclude)) {
                     continue 2;
                 }
             }
             $res = $resource ? $version == 1 ? "/resources/{$resource}" : "/v{$version}/resources/{$resource}-v{$version}" : ($version == 1 ? "/resources/root" : "/v{$version}/resources/root-v{$version}");
             if (empty($map[$res])) {
                 $map[$res] = isset($route['metadata']['classDescription']) ? $route['metadata']['classDescription'] : '';
             }
         }
     }
 }