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