Skip to content

Commit d759d4c

Browse files
authored
Merge pull request #1 from Shureban/v0.1.0
prepare to array of types
2 parents 0f580d1 + 5f31f22 commit d759d4c

File tree

9 files changed

+67
-4
lines changed

9 files changed

+67
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818
],
1919
"minimum-stability": "dev",
20-
"version": "0.0.6",
20+
"version": "0.1.0",
2121
"autoload": {
2222
"psr-4": {
2323
"Shureban\\LaravelObjectMapper\\": "src/"

src/ClassExtraInformation.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public function __construct(ReflectionClass $class)
2525
*/
2626
public function getFullObjectUseNamespace(string $objectName): ?string
2727
{
28+
if (class_exists($objectName)) {
29+
return $objectName;
30+
}
31+
2832
if (preg_match_all(self::UseRegex, $this->getClassFileContent(), $useRegexResult)) {
2933
foreach (current($useRegexResult) as $useLine) {
3034
if (preg_match(self::UseNamespaceRegex, $useLine, $lineRegexResult) === 0) {

src/PhpDoc.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
class PhpDoc
66
{
77
private const PropertyNameRegex = '/var(.*)?\$(?<name>\w+)/';
8-
private const TypeNameRegex = '/var\s(?<type>[\\a-zA-Z0-9]+)\s\$?/U';
8+
private const TypeNameRegex = '/var\s(?<type>[\\a-zA-Z0-9]+)([\[\]]+)?\s\$?/U';
9+
private const ArrayOfRegex = '/var .*\[\] \$?/';
910

1011
private string $phpDoc;
1112

@@ -40,4 +41,12 @@ public function getPropertyType(): mixed
4041

4142
return null;
4243
}
44+
45+
/**
46+
* @return bool
47+
*/
48+
public function isArrayOf(): bool
49+
{
50+
return (bool)preg_match(self::ArrayOfRegex, $this->phpDoc);
51+
}
4352
}

src/Property.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Shureban\LaravelObjectMapper;
44

5+
use ReflectionException;
56
use ReflectionProperty;
67
use Shureban\LaravelObjectMapper\Types\Factory;
78
use Shureban\LaravelObjectMapper\Types\Type;
@@ -14,6 +15,8 @@ class Property
1415

1516
/**
1617
* @param ReflectionProperty $property
18+
*
19+
* @throws ReflectionException
1720
*/
1821
public function __construct(ReflectionProperty $property)
1922
{

src/Types/Custom/ArrayOfType.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Shureban\LaravelObjectMapper\Types\Custom;
4+
5+
use Shureban\LaravelObjectMapper\ObjectMapper;
6+
use Shureban\LaravelObjectMapper\Types\SimpleTypes\ObjectType;
7+
8+
class ArrayOfType extends ObjectType
9+
{
10+
private object $type;
11+
12+
public function __construct(object $type)
13+
{
14+
$this->type = $type;
15+
}
16+
17+
/**
18+
* @param mixed $value
19+
*
20+
* @return object
21+
*/
22+
public function convert(mixed $value): object
23+
{
24+
return (new ObjectMapper($this->type))->mapFromArray($value);
25+
}
26+
}

src/Types/CustomType.php renamed to src/Types/Custom/CustomType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Shureban\LaravelObjectMapper\Types;
3+
namespace Shureban\LaravelObjectMapper\Types\Custom;
44

55
use Shureban\LaravelObjectMapper\ObjectMapper;
66
use Shureban\LaravelObjectMapper\Types\SimpleTypes\ObjectType;

src/Types/Factory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Shureban\LaravelObjectMapper\Types\BoxTypes\CarbonType;
1515
use Shureban\LaravelObjectMapper\Types\BoxTypes\CollectionType;
1616
use Shureban\LaravelObjectMapper\Types\BoxTypes\DateTimeType;
17+
use Shureban\LaravelObjectMapper\Types\Custom\CustomType;
1718
use Shureban\LaravelObjectMapper\Types\SimpleTypes\ArrayType;
1819
use Shureban\LaravelObjectMapper\Types\SimpleTypes\BoolType;
1920
use Shureban\LaravelObjectMapper\Types\SimpleTypes\FloatType;
@@ -52,6 +53,10 @@ public static function make(ReflectionProperty $property): Type
5253
return $simpleType;
5354
}
5455

56+
if ($phpDoc->isArrayOf()) {
57+
dd(12);
58+
}
59+
5560
$boxType = match ($type) {
5661
DateTime::class => new DateTimeType(),
5762
CarbonSupport::class, Carbon::class, 'Carbon' => new CarbonType(),

tests/Unit/ClassExtraInformationTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public function test_getFullObjectUseNamespace()
3737
'Tests\TestCase',
3838
(new ClassExtraInformation($class))->getFullObjectUseNamespace('TestCase')
3939
);
40-
$this->assertNull(
40+
$this->assertEquals(
41+
'ReflectionClass',
4142
(new ClassExtraInformation($class))->getFullObjectUseNamespace('ReflectionClass')
4243
);
4344
}

tests/Unit/PhpDocTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public function test_getPropertyName()
2323

2424
public function test_getPropertyType()
2525
{
26+
$this->assertEquals('string', (new PhpDoc('/** @var string[] */'))->getPropertyType());
27+
$this->assertEquals('string', (new PhpDoc('/** @var string[][] */'))->getPropertyType());
28+
$this->assertEquals('string', (new PhpDoc('/** @var string[][][] */'))->getPropertyType());
2629
$this->assertEquals('string', (new PhpDoc('/** @var string */'))->getPropertyType());
2730
$this->assertEquals('string', (new PhpDoc('/** @var string $variable */'))->getPropertyType());
2831
$this->assertEquals('SomeClass', (new PhpDoc('/** @var SomeClass $variable */'))->getPropertyType());
@@ -34,5 +37,17 @@ public function test_getPropertyType()
3437
*/
3538
DOC
3639
))->getPropertyType());
40+
41+
42+
// $this->assertEquals('string', (new PhpDoc('/** @var string[] */'))->getPropertyType());
43+
}
44+
45+
public function test_isArrayOf()
46+
{
47+
$this->assertFalse((new PhpDoc('/** @var string */'))->isArrayOf());
48+
$this->assertTrue((new PhpDoc('/** @var string[] */'))->isArrayOf());
49+
$this->assertTrue((new PhpDoc('/** @var string[][] */'))->isArrayOf());
50+
$this->assertTrue((new PhpDoc('/** @var string[][] */'))->isArrayOf());
51+
$this->assertTrue((new PhpDoc('/** @var string[] $variable */'))->isArrayOf());
3752
}
3853
}

0 commit comments

Comments
 (0)