Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 8 additions & 47 deletions src/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,26 @@

namespace Rudra\Annotation;

use Rudra\Exceptions\LogicException;

class Annotation implements AnnotationInterface
{
/**
* ----------------------
* Parameter separator
* ----------------------
* Разделитель параметров
* ----------------------
*
* --------------------------------------------------------
* in the line ',', example: key='param', key2='param2'
* in the array ';', example: {key:'param'; key2:'param2'}
* --------------------------------------------------------
*/
public const array DELIMITER = ["string" => ',', "array" => ';'];

/**
* -----------------
* Assignment mark
* -----------------
* Знак присваивания
* -----------------
*
* ----------------------------------------
* in the line '=', example: key='param'
* in the array ':', example: {key:'param'}
* ----------------------------------------
*/
public const array ASSIGNMENT = ["string" => '=', "array" => ':'];

/**
* @param string $className
* @param string|null $methodName
* @return array
* Each parameter must be on its own line.
*/
#[\Override]
public function getAnnotations(string $className, ?string $methodName = null): array
Expand All @@ -61,17 +45,12 @@ public function getAnnotations(string $className, ?string $methodName = null): a
}

/**
* @param string $className
* @param string|null $methodName
* @return array
* Returns all attributes for a class or method.
* Returns short class names (e.g. `Cache`, not `App\Attributes\Cache`).
*/
#[\Override]
public function getAttributes(string $className, ?string $methodName = null): array
{
if (PHP_VERSION_ID < 80000) {
throw new LogicException('Attributes are only supported in PHP 8.0 and above.');
}

$reflection = $this->getReflection($className, $methodName);
$attributes = [];

Expand All @@ -83,54 +62,36 @@ public function getAttributes(string $className, ?string $methodName = null): ar
return $attributes;
}

/**
* @param string $fullyQualifiedName
* @return string
*/
private function extractShortClassName(string $fullyQualifiedName): string
{
return substr($fullyQualifiedName, (int) strrpos($fullyQualifiedName, '\\') + 1);
$pos = strrpos($fullyQualifiedName, '\\');
return $pos === false ? $fullyQualifiedName : substr($fullyQualifiedName, $pos + 1);
}

/**
* @param string $className
* @param string|null $methodName
* @return \ReflectionClass|\ReflectionMethod
*/
private function getReflection(string $className, ?string $methodName = null): \ReflectionClass|\ReflectionMethod
{
return $methodName !== null
? new \ReflectionMethod($className, $methodName)
: new \ReflectionClass($className);
}

/**
* @param string $docBlock
* @return array
*/
private function parseAnnotations(string $docBlock): array
{
$annotations = [];

/**
* --------------------------------------------------------------------------------------
* $matches[0][0] - @Annotation(param1, param2='param2', param3={param1;param2:'param2'})
* $matches[1][0] - Annotation
* $matches[2][0] - param1, param2 = 'param2', param3={param1;param2:'param2'}
* --------------------------------------------------------------------------------------
*/
if (preg_match_all("/@([A-Za-z_-]+)\((.*)?\)/", $docBlock, $matches)) {
if (preg_match_all("/@([A-Za-z0-9_-]+)\((.*?)\)/", $docBlock, $matches)) {
$count = count($matches[0]);
$extractor = new ParamsExtractor();

/**
* ----------------------------------------------------------------------------------------------------------------------
* $annotations = ["Annotation" => [[0 => "param1", "param2" => "param2", "param3" => ["param1", "param2" => "param2"]]]]
* ----------------------------------------------------------------------------------------------------------------------
*/
// $annotations = ["Annotation" => [[0 => "param1", "param2" => "param2", "param3" => ["param1", "param2" => "param2"]]]]
for ($i = 0; $i < $count; $i++) {
$annotations[$matches[1][$i]][] = $extractor->getParams(
str_getcsv(trim($matches[2][$i]), self::DELIMITER["string"]),
str_getcsv(trim($matches[2][$i]), self::DELIMITER["string"], '"', ''),
self::ASSIGNMENT["string"]
);
}
Expand Down
11 changes: 0 additions & 11 deletions src/AnnotationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@

interface AnnotationInterface
{
/**
* @param string $className
* @param string|null $methodName
* @return array
*/
public function getAnnotations(string $className, ?string $methodName = null): array;

/**
* @param string $className
* @param string|null $methodName
* @return array
*/
public function getAttributes(string $className, ?string $methodName = null): array;
}
38 changes: 3 additions & 35 deletions src/ParamsExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@
class ParamsExtractor
{
/**
* --------------------------------------------------------------
* Parses an array of parameter strings into an associative array
* --------------------------------------------------------------
* Преобразует массив строк с параметрами в ассоциативный массив
* --------------------------------------------------------------
*
* ------------------------------------------------------------------------------------
* `from: "param1, param2 = 'param2', param3={param1;param2:'param2'}"`
* `to: ["param1", "param2" => "param2", "param3" => ["param1", "param2" => "param2"]]`
* ------------------------------------------------------------------------------------
*
* @param array $exploded
* @param string $assignment
Expand All @@ -45,35 +39,16 @@ public function getParams(array $exploded, string $assignment): array
}

/**
* --------------------------------------------
* Parses data into `key => value` pairs
* --------------------------------------------
* Преобразует данные в пары `ключ => значение`
* --------------------------------------------
*
* ⚠️ IMPORTANT / ВАЖНО:
* --------------------------------------------
* Values inside arrays (curly braces) must not
* contain the array assignment mark (`:`)
* --------------------------------------------
* Значения внутри массивов (фигурные скобки)
* не должны содержать знак присваивания (`:`)
* --------------------------------------------
* ⚠️ IMPORTANT: Values inside arrays (curly braces)
* must not contain the array assignment mark (`:`)
*
* @param string $data
* @param array $exploded
* @return array|null
* @codeCoverageIgnore
*/
private function handleData(string $data, array $exploded): ?array
{
/**
* --------------------------------------------------------------
* If in data an array of type param3={param1;param2:'param2'}
* --------------------------------------------------------------
* Если в данных есть массив типа param3={param1;param2:'param2'}
* --------------------------------------------------------------
*/
// If in data an array of type param3={param1;param2:'param2'}
if (preg_match("/=\s*{/", $data) && preg_match("/{(.*)}/", $exploded[1], $matches)) {
return [
trim($exploded[0]) => $this->getParams(
Expand All @@ -84,15 +59,8 @@ private function handleData(string $data, array $exploded): ?array
}

/**
* ---------------------------------------
* Remove quotation marks around parameter
* ---------------------------------------
* Удаляет кавычки вокруг параметра
* ---------------------------------------
*
* ----------------------
* matches[1] = 'param2';
* ----------------------
*/
if (preg_match("/'(.*)'/", $exploded[1], $matches)) {
return [trim($exploded[0]) => $matches[1]];
Expand Down
Loading