Completed
Push — master ( 812bf9...d9cb6e )
by Andrea Marco
09:43 queued 10s
created

Dto   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 74
ccs 20
cts 20
cp 1
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromRequest() 0 5 2
A getDefaultFlags() 0 5 1
A from() 0 15 6
A fromModel() 0 3 1
A getListener() 0 3 1
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;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\CAST_PRIMITIVES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
19
use const Cerbero\Dto\IGNORE_UNKNOWN_PROPERTIES;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\IGNORE_UNKNOWN_PROPERTIES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
20
use const Cerbero\Dto\NONE;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\NONE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
21
use const Cerbero\Dto\PARTIAL;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\PARTIAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
22
23
/**
24
 * The data transfer object.
25
 *
26
 */
27
abstract class Dto extends BaseDto implements Arrayable, Jsonable
28 2
{
29
    use Macroable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by Cerbero\LaravelDto\Dto.
Loading history...
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