Skip to content

Commit a447e8d

Browse files
Merge pull request #1048 from it4need/main
can get the data class for a data collection by annotation with unicode characters
2 parents 261a2e5 + 3b07f59 commit a447e8d

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

src/Support/Annotations/DataIterableAnnotationReader.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ protected function get(
5050
$comment = str_replace('?', '', $comment);
5151

5252
$kindPattern = '(?:@property|@var|@param)\s*';
53-
$fqsenPattern = '[\\\\a-z0-9_\|]+';
54-
$typesPattern = '[\\\\a-z0-9_\\|\\[\\]]+';
53+
$fqsenPattern = '[\\\\\\p{L}0-9_\|]+';
54+
$typesPattern = '[\\\\\\p{L}0-9_\\|\\[\\]]+';
5555
$keyPattern = '(?<key>int|string|int\|string|string\|int|array-key)';
56-
$parameterPattern = '\s*\$?(?<parameter>[a-z0-9_]+)?';
56+
$parameterPattern = '\s*\$?(?<parameter>[\\p{L}0-9_]+)?';
5757

5858
preg_match_all(
59-
"/{$kindPattern}(?<types>{$typesPattern}){$parameterPattern}/i",
59+
"/{$kindPattern}(?<types>{$typesPattern}){$parameterPattern}/ui",
6060
$comment,
6161
$arrayMatches,
6262
);
6363

6464
preg_match_all(
65-
"/{$kindPattern}(?<collectionClass>{$fqsenPattern})<(?:{$keyPattern}\s*?,\s*?)?(?<dataClass>{$fqsenPattern})>(?:{$typesPattern})*{$parameterPattern}/i",
65+
"/{$kindPattern}(?<collectionClass>{$fqsenPattern})<(?:{$keyPattern}\s*?,\s*?)?(?<dataClass>{$fqsenPattern})>(?:{$typesPattern})*{$parameterPattern}/ui",
6666
$comment,
6767
$collectionMatches,
6868
);

tests/Fakes/CollectionDataAnnotationsData.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* @property \Spatie\LaravelData\Tests\Fakes\SimpleData[] $propertyR
1616
* @property array<SimpleData> $propertyS
1717
* @property \Illuminate\Support\Collection<\Spatie\LaravelData\Tests\Fakes\SimpleData>|null $propertyT
18+
* @property \Illuminate\Support\Collection<\Spatie\LaravelData\Tests\Fakes\SimpleDataWithUnicodeCharséÄöü>|null $propertyW
1819
*/
1920
class CollectionDataAnnotationsData
2021
{
@@ -73,6 +74,11 @@ class CollectionDataAnnotationsData
7374
/** @var \Illuminate\Support\Collection<\Spatie\LaravelData\Tests\Fakes\SimpleData>|null */
7475
public ?array $propertyU;
7576

77+
/** @var \Illuminate\Support\Collection<\Spatie\LaravelData\Tests\Fakes\SimpleDataWithUnicodeCharséÄöü>|null */
78+
public ?array $propertyV;
79+
80+
public ?array $propertyW;
81+
7682
/**
7783
* @param \Spatie\LaravelData\Tests\Fakes\SimpleData[]|null $paramA
7884
* @param null|\Spatie\LaravelData\Tests\Fakes\SimpleData[] $paramB
@@ -85,6 +91,10 @@ class CollectionDataAnnotationsData
8591
* @param array<int,SimpleData> $paramJ
8692
* @param array<int, SimpleData> $paramI
8793
* @param \Spatie\LaravelData\DataCollection<\Spatie\LaravelData\Tests\Fakes\SimpleData>|null $paramK
94+
* @param \Spatie\LaravelData\DataCollection<\Spatie\LaravelData\Tests\Fakes\SimpleDataWithUnicodeCharséÄöü>|null $paramL
95+
* @param Collection<\Spatie\LaravelData\Tests\Fakes\SimpleDataWithUnicodeCharséÄöü>|null $paramM
96+
* @param array<\Spatie\LaravelData\Tests\Fakes\SimpleDataWithUnicodeCharséÄöü>|null $paramN
97+
* @param \Spatie\LaravelData\Tests\Fakes\SimpleDataWithUnicodeCharséÄöü[]|null $paramO
8898
*/
8999
public function method(
90100
array $paramA,
@@ -97,6 +107,10 @@ public function method(
97107
array $paramJ,
98108
array $paramI,
99109
?array $paramK,
110+
?DataCollection $paramL,
111+
?Collection $paramM,
112+
?array $paramN,
113+
?array $paramO
100114
) {
101115

102116
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Spatie\LaravelData\Tests\Fakes;
4+
5+
use Spatie\LaravelData\Data;
6+
7+
class SimpleDataWithUnicodeCharséÄöü extends Data
8+
{
9+
public function __construct(
10+
public string $string
11+
) {
12+
}
13+
14+
public static function fromString(string $string): self
15+
{
16+
return new self($string);
17+
}
18+
19+
public function toUserDefinedToArray(): array
20+
{
21+
return [
22+
'string' => $this->string,
23+
];
24+
}
25+
}

tests/Support/Annotations/DataIterableAnnotationReaderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Spatie\LaravelData\Tests\Fakes\Enums\DummyBackedEnum;
99
use Spatie\LaravelData\Tests\Fakes\Error;
1010
use Spatie\LaravelData\Tests\Fakes\SimpleData;
11+
use Spatie\LaravelData\Tests\Fakes\SimpleDataWithUnicodeCharséÄöü;
1112

1213
it(
1314
'can get the data class for a data collection by annotation',
@@ -42,6 +43,11 @@ function (string $property, ?DataIterableAnnotation $expected) {
4243
new DataIterableAnnotation(SimpleData::class, isData: true), // expected
4344
];
4445

46+
yield 'propertyE' => [
47+
'propertyE', // property
48+
new DataIterableAnnotation(SimpleData::class, isData: true), // expected
49+
];
50+
4551
yield 'propertyF' => [
4652
'propertyF', // property
4753
new DataIterableAnnotation(SimpleData::class, isData: true), // expected
@@ -86,6 +92,11 @@ function (string $property, ?DataIterableAnnotation $expected) {
8692
'propertyU', // property
8793
new DataIterableAnnotation(SimpleData::class, isData: true), // expected
8894
];
95+
96+
yield 'propertyV' => [
97+
'propertyV', // property
98+
new DataIterableAnnotation(SimpleDataWithUnicodeCharséÄöü::class, isData: true), // expected
99+
];
89100
});
90101

91102
it('can get the data class for a data collection by class annotation', function () {
@@ -99,6 +110,7 @@ function (string $property, ?DataIterableAnnotation $expected) {
99110
'propertyR' => new DataIterableAnnotation(SimpleData::class, isData: true, property: 'propertyR'),
100111
'propertyS' => new DataIterableAnnotation(SimpleData::class, isData: true, property: 'propertyS'),
101112
'propertyT' => new DataIterableAnnotation(SimpleData::class, isData: true, property: 'propertyT'),
113+
'propertyW' => new DataIterableAnnotation(SimpleDataWithUnicodeCharséÄöü::class, isData: true, property: 'propertyW'),
102114
]);
103115
});
104116

@@ -117,6 +129,10 @@ function (string $property, ?DataIterableAnnotation $expected) {
117129
'paramJ' => new DataIterableAnnotation(SimpleData::class, isData: true, keyType: 'int', property: 'paramJ'),
118130
'paramI' => new DataIterableAnnotation(SimpleData::class, isData: true, keyType: 'int', property: 'paramI'),
119131
'paramK' => new DataIterableAnnotation(SimpleData::class, isData: true, property: 'paramK'),
132+
'paramL' => new DataIterableAnnotation(SimpleDataWithUnicodeCharséÄöü::class, isData: true, property: 'paramL'),
133+
'paramM' => new DataIterableAnnotation(SimpleDataWithUnicodeCharséÄöü::class, isData: true, property: 'paramM'),
134+
'paramN' => new DataIterableAnnotation(SimpleDataWithUnicodeCharséÄöü::class, isData: true, property: 'paramN'),
135+
'paramO' => new DataIterableAnnotation(SimpleDataWithUnicodeCharséÄöü::class, isData: true, property: 'paramO'),
120136
]);
121137
});
122138

0 commit comments

Comments
 (0)