From f893e81790043516caec6d90512325e24d892d9a Mon Sep 17 00:00:00 2001 From: Bogdan Padalko Date: Mon, 6 Nov 2017 00:11:15 +0200 Subject: [PATCH] Reflect groupping and short array syntax in property spec --- src/Specs/Builder/PropertySpecBuilder.php | 2 +- .../Specs/Builder/PropertySpecBuilderTest.php | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Specs/Builder/PropertySpecBuilder.php b/src/Specs/Builder/PropertySpecBuilder.php index fda3b61..6fcfe84 100644 --- a/src/Specs/Builder/PropertySpecBuilder.php +++ b/src/Specs/Builder/PropertySpecBuilder.php @@ -100,7 +100,7 @@ protected function getSpecMethodsOrType(PropertySpecPrototype $proto, string $de return; } - if (preg_match('/^(?\w+(?:\(.*\))?)$/', $definition, $matches)) { + if (preg_match('/^(?([\w\-]*(?:\(.*\))?(?:\[\s*\])?)(?:\s*\|\s*(?-1))*)$/', $definition, $matches)) { $proto->definition = $this->builder->build($matches['type']); return; diff --git a/tests/Specs/Builder/PropertySpecBuilderTest.php b/tests/Specs/Builder/PropertySpecBuilderTest.php index 2b7dcf6..fbb2a90 100644 --- a/tests/Specs/Builder/PropertySpecBuilderTest.php +++ b/tests/Specs/Builder/PropertySpecBuilderTest.php @@ -87,6 +87,34 @@ public function testBuildingTyped() $this->assertNull($spec->getSetterName()); } + public function testBuildingTypedArray() + { + $this->extractorDefinitionShouldBuildOn('[]'); + + $spec = $this->builder->build('[]'); + + $this->assertInstanceOf(PropertySpecInterface::class, $spec); + + $this->assertInstanceOf(ExtractorDefinitionInterface::class, $spec->getExtractorDefinition()); + $this->assertFalse($spec->isReadonly()); + $this->assertNull($spec->getGetterName()); + $this->assertNull($spec->getSetterName()); + } + + public function testBuildingTypedAndGroupedArray() + { + $this->extractorDefinitionShouldBuildOn('(foo|bar[])[]'); + + $spec = $this->builder->build('(foo|bar[])[]'); + + $this->assertInstanceOf(PropertySpecInterface::class, $spec); + + $this->assertInstanceOf(ExtractorDefinitionInterface::class, $spec->getExtractorDefinition()); + $this->assertFalse($spec->isReadonly()); + $this->assertNull($spec->getGetterName()); + $this->assertNull($spec->getSetterName()); + } + public function testBuildingReadonlyTyped() { $this->extractorDefinitionShouldBuildOn('test');