Skip to content

Commit de3386e

Browse files
committed
Replace includeFilter with exclude filter
1 parent 90bd20b commit de3386e

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/Language/Dart/DartClassFactoryGenerator.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Riverwaysoft\DtoConverter\Dto\DtoEnumProperty;
66
use Riverwaysoft\DtoConverter\Dto\DtoList;
77
use Riverwaysoft\DtoConverter\Dto\DtoType;
8+
use Riverwaysoft\DtoConverter\Dto\PhpType\PhpBaseType;
89
use Riverwaysoft\DtoConverter\Dto\PhpType\PhpListType;
910
use Riverwaysoft\DtoConverter\Dto\PhpType\PhpTypeInterface;
1011
use Riverwaysoft\DtoConverter\Dto\PhpType\PhpUnionType;
@@ -13,7 +14,7 @@
1314

1415
class DartClassFactoryGenerator
1516
{
16-
public function __construct(private string|null $includePattern = null)
17+
public function __construct(private string|null $excludePattern = null)
1718
{
1819

1920
}
@@ -24,15 +25,15 @@ public function generateClassFactory(DtoType $dto, DtoList $dtoList): string
2425
return '';
2526
}
2627

27-
if ($this->includePattern && !preg_match(pattern: $this->includePattern, subject: $dto->getName())) {
28-
return '';
28+
if ($this->excludePattern && preg_match(pattern: $this->excludePattern, subject: $dto->getName())) {
29+
return '';
2930
}
3031

3132
$factoryProperties = '';
3233

3334
foreach ($dto->getProperties() as $property) {
3435
Assert::false($property instanceof DtoEnumProperty, "Dart factories only work in a class context, not enum");
35-
$propertyValue = $this->resolveFactoryProperty($property->getName(), $property->getType(), $dtoList);
36+
$propertyValue = $this->resolveFactoryProperty($property->getName(), $property->getType(), $dto, $dtoList);
3637
$factoryProperties .= sprintf(" %s: %s,\n", $property->getName(), $propertyValue);
3738
}
3839

@@ -45,19 +46,36 @@ public function generateClassFactory(DtoType $dto, DtoList $dtoList): string
4546
);
4647
}
4748

48-
private function resolveFactoryProperty(string $propertyName, PhpTypeInterface $type, DtoList $dtoList): string
49+
private function resolveFactoryProperty(string $propertyName, PhpTypeInterface $type, DtoType $dto, DtoList $dtoList): string
4950
{
5051
if ($type instanceof PhpUnionType && $type->isNullable()) {
51-
return sprintf("json['{$propertyName}'] != null ? %s : null", $this->resolveFactoryProperty($propertyName, $type->getFirstNotNullType(), $dtoList));
52+
return sprintf("json['{$propertyName}'] != null ? %s : null", $this->resolveFactoryProperty($propertyName, $type->getFirstNotNullType(), $dto, $dtoList));
5253
}
5354

5455
if ($type instanceof PhpListType) {
5556
$collectionType = $type->getType();
56-
if (!($collectionType instanceof PhpUnknownType)) {
57-
throw new \Exception('Only class instance can be converted to collection');
57+
58+
if ($collectionType instanceof PhpUnknownType) {
59+
$collectionInnerType = $collectionType->getName();
60+
return sprintf("List<%s>.from(json['%s'].map((e) => %s.fromJson(e)))", $collectionInnerType, $propertyName, $collectionInnerType);
61+
}
62+
63+
if ($collectionType instanceof PhpBaseType) {
64+
$dartType = match (true) {
65+
$collectionType->equalsTo(PhpBaseType::int()) => 'int',
66+
$collectionType->equalsTo(PhpBaseType::float()) => 'double',
67+
$collectionType->equalsTo(PhpBaseType::string()) => 'String',
68+
$collectionType->equalsTo(PhpBaseType::bool()) => 'bool',
69+
$collectionType->equalsTo(PhpBaseType::mixed()), $collectionType->equalsTo(PhpBaseType::iterable()), $collectionType->equalsTo(PhpBaseType::array()) => 'Object',
70+
$collectionType->equalsTo(PhpBaseType::null()) => 'null',
71+
$collectionType->equalsTo(PhpBaseType::self()) => $dto->getName(),
72+
default => throw new \Exception(sprintf("Unknown base PHP type: %s", $type->jsonSerialize()))
73+
};
74+
75+
return sprintf("List<%s>.from(json['%s'])", $dartType, $propertyName);
5876
}
59-
$class = $collectionType->getName();
60-
return sprintf("List<%s>.from(json['%s'].map((e) => %s.fromJson(e)))", $class, $propertyName, $class);
77+
78+
throw new \Exception(sprintf("Only PHP base types and class instance can be converted to collection. Property: %s#%s", $dto->getName(), $propertyName));
6179
}
6280

6381
if ($type instanceof PhpUnknownType) {

src/Language/Dart/DartEquitableGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class DartEquitableGenerator
99
{
10-
public function __construct(private string|null $includePattern = null)
10+
public function __construct(private string|null $excludePattern = null)
1111
{
1212

1313
}
@@ -23,7 +23,7 @@ public function generateEquitableHeader(DtoType $dto): string
2323

2424
private function doesntMatch(DtoType $dto): bool
2525
{
26-
return $this->includePattern && !preg_match(pattern: $this->includePattern, subject: $dto->getName());
26+
return $this->excludePattern && preg_match(pattern: $this->excludePattern, subject: $dto->getName());
2727
}
2828

2929
public function generateEquitableId(DtoType $dto): string

tests/DartGeneratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ class UserCreateInput
147147
"// THE FILE WAS AUTOGENERATED USING DTO-CONVERTER. PLEASE DO NOT EDIT IT!\nimport 'package:equatable/equatable.dart';\n\n",
148148
),
149149
]),
150-
new DartClassFactoryGenerator('/[^Input]$/'),
151-
new DartEquitableGenerator('/[^Input]$/'),
150+
new DartClassFactoryGenerator('/Input$/'),
151+
new DartEquitableGenerator('/Input$/'),
152152
))->generate($normalized);
153153

154154
$this->assertCount(1, $results);

0 commit comments

Comments
 (0)