Skip to content

Commit 973968c

Browse files
committed
wip
1 parent 15af040 commit 973968c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+248
-427
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Attribute `OpenSoutheners\LaravelDto\Attributes\Inject` to inject container stuff
1515
- Attribute `OpenSoutheners\LaravelDto\Attributes\Authenticated` that uses base `Illuminate\Container\Attributes\Authenticated` to inject current authenticated user
1616
- Ability to register custom mappers (extending package functionality)
17+
- ObjectMapper now extracts type info from generics inside collections typed properties [#1]
18+
19+
### Changed
20+
21+
- Full refactor [#7]
1722

1823
### Removed
1924

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
},
3535
"require-dev": {
3636
"larastan/larastan": "^3.0",
37+
"laravel/pint": "^1.22",
3738
"orchestra/testbench": "^9.0 || ^10.0",
3839
"phpstan/phpstan": "^2.0",
3940
"phpunit/phpunit": "^11.0"

config/data-transfer-objects.php

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

55
/**
66
* Normalise data transfer objects property names.
7-
*
7+
*
88
* For example: user_id (sent) => user (DTO) or is_published (sent) => isPublished (DTO)
99
*/
1010
'normalise_properties' => true,
@@ -14,13 +14,13 @@
1414
* are passed to the command.
1515
*/
1616
'types_generation' => [
17-
17+
1818
'output' => null,
19-
19+
2020
'source' => null,
2121

2222
'filename' => null,
23-
23+
2424
'declarations' => false,
2525

2626
],

src/Attributes/AsType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class AsType
99
{
1010
public function __construct(public string $typeName)
1111
{
12-
//
12+
//
1313
}
14-
}
14+
}

src/Attributes/Authenticated.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
99
class Authenticated extends BaseAttribute
1010
{
11-
//
11+
//
1212
}

src/Attributes/Inject.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ class Inject implements ContextualAttribute
1111
{
1212
public function __construct(public string $value)
1313
{
14-
//
14+
//
1515
}
16-
16+
1717
/**
1818
* Resolve the currently authenticated user.
1919
*
20-
* @param self $attribute
21-
* @param \Illuminate\Contracts\Container\Container $container
2220
* @return \Illuminate\Contracts\Auth\Authenticatable|null
2321
*/
2422
public static function resolve(self $attribute, Container $container)

src/Attributes/ModelWith.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class ModelWith
99
{
1010
public function __construct(public string|array $relations, public ?string $type = null)
1111
{
12-
//
12+
//
1313
}
1414
}

src/Attributes/ResolveModel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class ResolveModel
1313
{
1414
public function __construct(
1515
public string|array|null $keyFromRouteParam = null,
16-
public string|null $morphTypeFrom = null
16+
public ?string $morphTypeFrom = null
1717
) {
1818
//
1919
}
@@ -54,7 +54,7 @@ public function getBindingAttribute(string $key, string $type, array $with)
5454

5555
protected function resolveBinding(string $model, mixed $value, mixed $field = null, array $with = [])
5656
{
57-
$modelInstance = new $model();
57+
$modelInstance = new $model;
5858

5959
return $modelInstance->resolveRouteBindingQuery($modelInstance, $value, $field)
6060
->with($with);
@@ -66,7 +66,7 @@ public function getMorphPropertyTypeKey(string $fromPropertyKey): string
6666
return Str::snake($this->morphTypeFrom);
6767
}
6868

69-
return static::getDefaultMorphKeyFrom($fromPropertyKey);
69+
return self::getDefaultMorphKeyFrom($fromPropertyKey);
7070
}
7171

7272
public function getMorphModel(string $fromPropertyKey, array $properties, array $propertyTypeClasses = []): array
@@ -90,7 +90,7 @@ public function getMorphModel(string $fromPropertyKey, array $properties, array
9090
if (count($modelModelClass) === 0 && count($propertyTypeClasses) > 0) {
9191
$modelModelClass = array_filter(
9292
$propertyTypeClasses,
93-
fn (string $class) => in_array((new $class())->getMorphClass(), $types)
93+
fn (string $class) => in_array((new $class)->getMorphClass(), $types)
9494
);
9595

9696
$modelModelClass = reset($modelModelClass);

src/Attributes/Validate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class Validate
99
{
1010
public function __construct(public string $value)
1111
{
12-
//
12+
//
1313
}
1414
}

src/Attributes/WithDefaultValue.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class WithDefaultValue
99
{
1010
public function __construct(public mixed $value)
1111
{
12-
//
12+
//
1313
}
14-
}
14+
}

src/Commands/DtoMakeCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use Illuminate\Console\GeneratorCommand;
66
use Illuminate\Support\Str;
7+
use OpenSoutheners\ExtendedLaravel\Console\Concerns\OpensGeneratedFiles;
78
use Symfony\Component\Console\Attribute\AsCommand;
89
use Symfony\Component\Console\Input\InputOption;
9-
use OpenSoutheners\ExtendedLaravel\Console\Concerns\OpensGeneratedFiles;
1010

1111
#[AsCommand(name: 'make:dto')]
1212
class DtoMakeCommand extends GeneratorCommand
@@ -138,7 +138,7 @@ protected function getProperties(string $requestClass)
138138
return '';
139139
}
140140

141-
$requestInstance = new $requestClass();
141+
$requestInstance = new $requestClass;
142142
$properties = '';
143143

144144
$requestRules = $requestInstance->rules();

src/Contracts/DataTransferObject.php

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace OpenSoutheners\LaravelDto\Contracts;
4+
5+
/**
6+
* This solely act as a tag for the class to be resolved by the Laravel DI (Container).
7+
*
8+
* So in case is used directly in a controller method this will map the request
9+
* with route data onto the object class constructor properties.
10+
*/
11+
interface RouteTransferableObject
12+
{
13+
//
14+
}

src/DataTransferObjects/MappingValue.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@
1111
final class MappingValue
1212
{
1313
public readonly ?Type $preferredType;
14-
14+
1515
/**
1616
* @var class-string|null
1717
*/
1818
public readonly ?string $preferredTypeClass;
19-
19+
2020
/**
2121
* @var Collection<\ReflectionAttribute>
2222
*/
2323
public readonly Collection $attributes;
24-
24+
2525
/**
26-
* @param class-string|null $objectClass
27-
* @param array<Type>|null $types
26+
* @param class-string|null $objectClass
27+
* @param array<Type>|null $types
2828
*/
2929
public function __construct(
3030
public readonly mixed $data,
@@ -35,9 +35,9 @@ public function __construct(
3535
public readonly ?ReflectionProperty $property = null,
3636
) {
3737
$this->preferredType = $types ? (reset($types) ?? null) : null;
38-
38+
3939
$this->preferredTypeClass = $this->preferredType ? ($this->preferredType->getClassName() ?: $objectClass) : $objectClass;
40-
40+
4141
$this->attributes = Collection::make($class ? $class->getAttributes() : []);
4242
}
4343
}

src/Enums/BuiltInType.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,42 @@ enum BuiltInType: string
77
case Object = 'object';
88
case Array = 'array';
99
case Iterable = 'iterable';
10-
10+
1111
case Integer = 'int';
1212
case Float = 'float';
13-
13+
1414
case String = 'string';
15-
15+
1616
case Boolean = 'bool';
1717
case True = 'true';
1818
case False = 'false';
19-
19+
2020
case Null = 'null';
2121
case Never = 'never';
2222
case Void = 'void';
23-
23+
2424
case Mixed = 'mixed';
25-
25+
2626
case Callable = 'callable';
2727
case Resource = 'resource';
28-
28+
2929
public static function guess($value): static
3030
{
3131
return self::tryFrom(gettype($value)) ?? self::Mixed;
3232
}
33-
33+
3434
public function assert(...$types): bool
3535
{
3636
$loops = 0;
3737
$truth = false;
38-
38+
3939
while ($truth === false && count($types) > $loops) {
4040
$type = $types[$loops];
41-
41+
4242
$truth = $type instanceof static ? $type === $this : $type === $this->value;
4343
$loops++;
4444
}
45-
45+
4646
return $truth;
4747
}
4848
}

0 commit comments

Comments
 (0)