/** * Create path for URI from method name * * @param \deco\essentials\util\annotation\AnnotationCollection $method * @param string $httpMethod * @return string */ protected static function getPathFromMethod($method, $httpMethod) { $customPath = $method->getValue('route', null); if (!is_null($customPath)) { return $customPath; } $reflector = $method->reflector; $name = preg_replace("#^{$httpMethod}#", "", $reflector->getName()); if (($pos = strpos($name, '_')) !== false) { $name = substr($name, 0, $pos); } $path = ''; while (strlen($name) > 0) { preg_match('@([A-Z])([a-z]*)([A-Z])?@', $name, $temp); if (count($temp) == 0) { $path .= '/' . strtolower($name); break; } else { $part = $temp[1] . $temp[2]; $path .= '/' . strtolower($part); $name = substr($name, strlen($part)); } } $path .= self::getPathPartDefinedByParameters($reflector); return $path; }
/** * @return \deco\essentials\util\annotation\AnnotationCollection */ public static function getAllAnnotations($docComment, $reflector = null) { $lines = explode(PHP_EOL, $docComment); $collection = new AnnotationCollection(); $ann = null; foreach ($lines as $line) { $line = preg_replace('#//.*$#', '', $line); // remove comment at the end of line if (preg_match('#\\*\\s@([a-zA-z]*)([\\([private|protected|public]\\)])?(\\((string|array|dictionary|int|bool)\\))?\\s(.*)#', $line, $matches)) { $annotation = trim($matches[1]); $visibility = $matches[2]; $type = $matches[4]; if (strlen($visibility) == 0) { if ($reflector instanceof \ReflectionProperty || $reflector instanceof \ReflectionMethod) { if ($reflector->isPrivate()) { $visibility = 'private'; } else { if ($reflector->isProtected()) { $visibility = 'protected'; } else { if ($reflector->isPublic()) { $visibility = 'public'; } } } } if (strlen($visibility) == 0) { $visibility = 'public'; } } else { preg_match('#\\((.*)\\)#', $visibility, $vis); $visibility = $vis[1]; } $value = self::parseAnnotationValue($matches[5], $type); if ($collection->hasAnnotation($annotation)) { $collection->get($annotation)->merge($value); } else { $ann = new Annotation($annotation, $value, $visibility); $collection->set($ann); } } else { if (preg_match('#\\\\(.*)#', $line, $matches)) { // needs to be array or dictionary $newValue = self::parseAnnotationValue($matches[1]); // picks previous annotation if (is_null($ann)) { throw new exc\Annotation(array('msg' => 'Cannot continue unexisting annotation.')); } $ann->merge($newValue); } } } return $collection; }
/** * Converts variable to correct type according to its annotation collection * * @param any &$value reference parameter * @throws exc\Deco */ public static function convertTo(&$value, \deco\essentials\util\annotation\AnnotationCollection $propertyAnnotations) { $type = $propertyAnnotations->getValue('type'); if (is_null($value) || $value === 'NULL') { $value = null; return; } switch ($type) { case 'int': case 'integer': case 'string': settype($value, $type); break; case 'bool': $value = filter_var($value, FILTER_VALIDATE_BOOLEAN); break; case 'timestamp': if (is_numeric($value)) { settype($value, 'integer'); } else { $dt = new \DateTime($value); $value = $dt->getTimestamp(); } break; case 'enum': if (func_num_args() == 3) { // Not using dynamic constants //$cls = $classAnnotations->reflector->getName(); //$value = constant($cls . '::' . $value); } break; case 'date': break; case 'json': if (is_array($value)) { $value = json_encode($value); } else { $value = json_decode($value, true); } break; default: throw new exc\Deco(array('msg' => "Cannot convert variable '{$value}' to '{$type}'", 'params' => array('type' => $type, 'value' => $value))); } }
/** * Get type for property based on annotation collection with annotation type * * @param \deco\essentials\util\annotation\AnnotationCollection $annCol * @return string * * @throws exc\Deco If annotation collection does not have annotation type */ public static function getTypeForProperty(\deco\essentials\util\annotation\AnnotationCollection $annCol) { if (!$annCol->hasAnnotation('type')) { throw new exc\Deco(array('msg' => 'Annotation collection does not have annotation type.')); } $type = $annCol->getValue('type'); // Custom behaviour if ($type == 'string') { if (count($val = $annCol->getValue('validation', array())) > 0 && array_key_exists('maxLength', $val)) { $type = "varchar({$val['maxLength']})"; } else { $type = 'text'; } } else { if ($type == 'enum') { $values = $annCol->getValue('values'); $type = " enum('" . implode("','", $values) . "')"; } else { if ($type == 'json') { return 'text'; } } } // Otherwise just return type return $type; }