/** * 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; }
/** * 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; }