1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\LaravelDto; |
4
|
|
|
|
5
|
|
|
use Cerbero\Dto\Dto as BaseDto; |
6
|
|
|
use Cerbero\Dto\Manipulators\Listener as BaseListener; |
7
|
|
|
use Cerbero\LaravelDto\Manipulators\Listener; |
8
|
|
|
use Illuminate\Container\Container; |
9
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
10
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
12
|
|
|
use Illuminate\Http\Request; |
13
|
|
|
use Illuminate\Support\Enumerable; |
14
|
|
|
use Illuminate\Support\Traits\Macroable; |
15
|
|
|
use JsonSerializable; |
16
|
|
|
use Traversable; |
17
|
|
|
|
18
|
|
|
use const Cerbero\Dto\CAST_PRIMITIVES; |
|
|
|
|
19
|
|
|
use const Cerbero\Dto\IGNORE_UNKNOWN_PROPERTIES; |
|
|
|
|
20
|
|
|
use const Cerbero\Dto\NONE; |
|
|
|
|
21
|
|
|
use const Cerbero\Dto\PARTIAL; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The data transfer object. |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
abstract class Dto extends BaseDto implements Arrayable, Jsonable |
28
|
2 |
|
{ |
29
|
|
|
use Macroable; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Retrieve an instance of DTO from the given or current request |
33
|
|
|
* |
34
|
|
|
* @param Request|null $request |
35
|
|
|
* @param int $flags |
36
|
|
|
* @return self |
37
|
3 |
|
*/ |
38
|
|
|
public static function fromRequest(Request $request = null, int $flags = NONE): self |
39
|
3 |
|
{ |
40
|
|
|
$request = $request ?: Request::capture(); |
41
|
3 |
|
|
42
|
|
|
return static::make($request->all(), $flags | PARTIAL | IGNORE_UNKNOWN_PROPERTIES); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Retrieve an instance of DTO from the request |
47
|
|
|
* |
48
|
|
|
* @param Model $model |
49
|
|
|
* @param int $flags |
50
|
|
|
* @return self |
51
|
3 |
|
*/ |
52
|
|
|
public static function fromModel(Model $model, int $flags = NONE): self |
53
|
3 |
|
{ |
54
|
|
|
return static::make($model->toArray(), $flags | CAST_PRIMITIVES | PARTIAL | IGNORE_UNKNOWN_PROPERTIES); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Retrieve an instance of DTO from the given source |
59
|
|
|
* |
60
|
|
|
* @param mixed $source |
61
|
|
|
* @param int $flags |
62
|
|
|
* @return self |
63
|
3 |
|
*/ |
64
|
|
|
public static function from($source, int $flags = NONE): self |
65
|
3 |
|
{ |
66
|
3 |
|
if ($source instanceof Enumerable) { |
67
|
3 |
|
$source = $source->all(); |
68
|
3 |
|
} elseif ($source instanceof Arrayable) { |
69
|
3 |
|
$source = $source->toArray(); |
70
|
3 |
|
} elseif ($source instanceof Jsonable) { |
71
|
3 |
|
$source = json_decode($source->toJson(), true); |
72
|
3 |
|
} elseif ($source instanceof JsonSerializable) { |
73
|
3 |
|
$source = $source->jsonSerialize(); |
74
|
3 |
|
} elseif ($source instanceof Traversable) { |
75
|
|
|
$source = iterator_to_array($source); |
76
|
|
|
} |
77
|
3 |
|
|
78
|
|
|
return static::make((array) $source, $flags); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Retrieve the default flags |
83
|
|
|
* |
84
|
|
|
* @return int |
85
|
24 |
|
*/ |
86
|
|
|
public static function getDefaultFlags(): int |
87
|
24 |
|
{ |
88
|
|
|
$config = Container::getInstance()->make('config'); |
89
|
|
|
|
90
|
|
|
return $config['dto.flags'] | static::$defaultFlags; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Retrieve the listener instance |
95
|
|
|
* |
96
|
|
|
* @return BaseListener |
97
|
|
|
*/ |
98
|
|
|
protected function getListener(): BaseListener |
99
|
|
|
{ |
100
|
|
|
return Listener::instance(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|