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
12 changes: 6 additions & 6 deletions src/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class Annotation implements AnnotationInterface
* in the line ',', example: key='param', key2='param2'
* in the array ';', example: {key:'param'; key2:'param2'}
*/
public const array DELIMITER = ["string" => ',', "array" => ';'];
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" => ':'];
public const array ASSIGNMENT = ['string' => '=', 'array' => ':'];

/**
* Each parameter must be on its own line.
Expand Down Expand Up @@ -84,15 +84,15 @@ private function parseAnnotations(string $docBlock): array
* $matches[1][0] - Annotation
* $matches[2][0] - param1, param2 = 'param2', param3={param1;param2:'param2'}
*/
if (preg_match_all("/@([A-Za-z0-9_-]+)\((.*?)\)/", $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"], '"', ''),
self::ASSIGNMENT["string"]
str_getcsv(trim($matches[2][$i]), self::DELIMITER['string'], '"', ''),
self::ASSIGNMENT['string']
);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/ParamsExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ 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"]]`
* `from: 'param1, param2 = 'param2', param3={param1;param2:'param2'}'`
* `to: ['param1', 'param2' => 'param2', 'param3' => ['param1', 'param2' => 'param2']]`
*/
public function getParams(array $exploded, string $assignment): array
{
Expand Down Expand Up @@ -45,11 +45,11 @@ public function getParams(array $exploded, string $assignment): array
private function handleData(string $data, array $exploded): ?array
{
// If in data an array of type param3={param1;param2:'param2'}
if (preg_match("/=\s*{/", $data) && preg_match("/{(.*)}/", $exploded[1], $matches)) {
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(Annotation::DELIMITER['array'], trim($matches[1])),
Annotation::ASSIGNMENT['array']
),
];
}
Expand Down
64 changes: 32 additions & 32 deletions tests/AnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,41 @@ class AnnotationTest extends \PHPUnit\Framework\TestCase
*/
";
private array $result = [
"Routing" => [["url" => ""]],
"Defaults" => [
'Routing' => [['url' => '']],
'Defaults' => [
[
"name" => "user1",
"lastname" => "sample",
"age" => "0",
"address" => [
"country" => "Russia",
"state" => "Tambov",
'name' => 'user1',
'lastname' => 'sample',
'age' => '0',
'address' => [
'country' => 'Russia',
'state' => 'Tambov',
],
"phone" => "000-00000000",
'phone' => '000-00000000',
],
],
"assertResult" => [["false"]],
"Validate" => [
'assertResult' => [['false']],
'Validate' => [
[
"name" => "min:150",
"phone" => "max:9",
'name' => 'min:150',
'phone' => 'max:9',
],
],
"Middleware" => [
'Middleware' => [
[
"'Middleware'",
"params" => [
"int1" => "123",
'params' => [
'int1' => '123',
],
],
],
"Annotation" => [
'Annotation' => [
[
"param1",
"param2" => "param2",
"param3" => [
"param1",
"param2" => "param2",
'param1',
'param2' => 'param2',
'param3' => [
'param1',
'param2' => 'param2',
],
],
],
Expand All @@ -85,7 +85,7 @@ private function getMethod(string $name): \ReflectionMethod

public function testParseAnnotations(): void
{
$parseAnnotations = $this->getMethod("parseAnnotations");
$parseAnnotations = $this->getMethod('parseAnnotations');
$this->assertEquals($this->result, $parseAnnotations->invokeArgs($this->annotation, [$this->docBlock]));
}

Expand All @@ -96,7 +96,7 @@ public function testGetClassAnnotations(): void

public function testGetMethodAnnotations(): void
{
$this->assertEquals($this->result, $this->annotation->getAnnotations(PageController::class, "indexAction"));
$this->assertEquals($this->result, $this->annotation->getAnnotations(PageController::class, 'indexAction'));
}

public function testGetClassAttributes(): void
Expand All @@ -106,12 +106,12 @@ public function testGetClassAttributes(): void

public function testGetMethodAttributes(): void
{
$this->assertEquals($this->result, $this->annotation->getAttributes(PageController::class, "secondAction"));
$this->assertEquals($this->result, $this->annotation->getAttributes(PageController::class, 'secondAction'));
}

public function testGetMethodWithoutAnnotations(): void
{
$this->assertEquals([], $this->annotation->getAnnotations(PageController::class, "withoutDocblock"));
$this->assertEquals([], $this->annotation->getAnnotations(PageController::class, 'withoutDocblock'));
}

public function testParseAnnotationsWithEqualsInValue(): void
Expand All @@ -124,20 +124,20 @@ public function testParseAnnotationsWithEqualsInValue(): void
";

$expected = [
"Routing" => [
'Routing' => [
[
"url" => "site/com?a=1&b=2",
"method" => "GET",
'url' => 'site/com?a=1&b=2',
'method' => 'GET',
],
],
"Validate" => [
'Validate' => [
[
"pattern" => "^[a-z]+=[0-9]+$",
'pattern' => '^[a-z]+=[0-9]+$',
],
],
];

$parseAnnotations = $this->getMethod("parseAnnotations");
$parseAnnotations = $this->getMethod('parseAnnotations');
$this->assertEquals($expected, $parseAnnotations->invokeArgs($this->annotation, [$docBlock]));
}
}
4 changes: 2 additions & 2 deletions tests/Stub/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#[assertResult('false')]
#[Validate(name:'min:150', phone:'max:9')]
#[Middleware("'Middleware'", params:['int1' => '123'])]
#[Annotation("param1", param2:'param2', param3:['param1', 'param2' => 'param2'])]
#[Annotation('param1', param2:'param2', param3:['param1', 'param2' => 'param2'])]
class PageController
{
/**
Expand All @@ -44,7 +44,7 @@ public function indexAction()
#[assertResult('false')]
#[Validate(name:'min:150', phone:'max:9')]
#[Middleware("'Middleware'", params:['int1' => '123'])]
#[Annotation("param1", param2:'param2', param3:['param1', 'param2' => 'param2'])]
#[Annotation('param1', param2:'param2', param3:['param1', 'param2' => 'param2'])]
public function secondAction()
{
}
Expand Down
Loading