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