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
25 changes: 7 additions & 18 deletions src/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,10 @@

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' => ':'];
private string $lineDelimiter = ',';
private string $lineAssignment = '=';
private string $arrayDelimiter = ';';
private string $arrayAssignment = ':';

/**
* Each parameter must be on its own line.
Expand Down Expand Up @@ -86,13 +75,13 @@ private function parseAnnotations(string $docBlock): array
*/
if (preg_match_all('/@([A-Za-z0-9_-]+)\((.*?)\)/', $docBlock, $matches)) {
$count = count($matches[0]);
$extractor = new ParamsExtractor();
$extractor = new ParamsExtractor($this->arrayDelimiter, $this->arrayAssignment);

// $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'], '"', ''),
self::ASSIGNMENT['string']
str_getcsv(trim($matches[2][$i]), $this->lineDelimiter, '"', '\\'),
$this->lineAssignment
);
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/ParamsExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

class ParamsExtractor
{
public function __construct(private string $delimiter, private string $assignment)
{
}

/**
* Parses an array of parameter strings into an associative array
*
Expand Down Expand Up @@ -48,8 +52,8 @@ private function handleData(string $data, array $exploded): ?array
if (preg_match('/=\s*{/', $data) && preg_match('/{(.*)}/', $exploded[1], $matches)) {
return [
trim($exploded[0]) => $this->getParams(
explode(Annotation::DELIMITER['array'], trim($matches[1])),
Annotation::ASSIGNMENT['array']
explode($this->delimiter, trim($matches[1])),
$this->assignment
),
];
}
Expand Down
Loading