Passed
Push — master ( 64ec9f...625618 )
by Arthur
03:13 queued 10s
created

Attribute::isNullable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Larapie\Actions;
4
5
use Faker\Factory;
6
use Faker\Generator;
7
use Illuminate\Support\Str;
8
9
class Attribute
10
{
11
    protected $data = [];
12
13
    protected $cast = null;
14
15 11
    final public function __construct()
16
    {
17 11
        $this->boot();
18 11
    }
19
20 11
    protected function boot()
21
    {
22 11
        $this->initializeRules();
23 11
    }
24
25 11
    protected function initializeRules()
26
    {
27 11
        if (method_exists($this, 'rules')) {
28 4
            $this->data['rules'] = $this->extractRules($this->rules());
29
        } else {
30 9
            $this->data['rules'] = [];
31
        }
32 11
    }
33
34 10
    protected static function make()
35
    {
36 10
        return new static();
37
    }
38
39 4
    public static function default($value)
40
    {
41 4
        $attribute = static::make();
42 4
        $attribute->setDefault($value);
43
44 4
        return $attribute;
45
    }
46
47 7
    public static function required()
48
    {
49 7
        return static::make()->require();
50
    }
51
52 7
    public function require()
53
    {
54 7
        $this->rule('required');
55
56 7
        return $this;
57
    }
58
59 6
    public function cast($cast)
60
    {
61 6
        if (is_callable($cast) || is_string($cast)) {
62 2
            $this->cast = $cast;
63
        }
64
65 6
        return $this;
66
    }
67
68 1
    public static function optional()
69
    {
70 1
        $attribute = static::make();
71
72 1
        $attribute->setRules(
73 1
            collect($attribute->getRules())
74
                ->reject(function ($value) {
75 1
                    return $value === 'required';
76 1
                })
77 1
                ->toArray()
78
        );
79
80 1
        return $attribute;
81
    }
82
83
    public static function fake()
84
    {
85
        return static::make()->factory(Factory::create());
86
    }
87
88
    public function factory(Generator $faker)
0 ignored issues
show
Unused Code introduced by
The parameter $faker is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

88
    public function factory(/** @scrutinizer ignore-unused */ Generator $faker)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
    {
90
        throw new \RuntimeException('The fake method for this attribute is not implemented.');
91
    }
92
93 11
    protected function setRules(array $rules)
94
    {
95 11
        $this->data['rules'] = $rules;
96 11
    }
97
98 10
    public function rule(...$rules)
99
    {
100
        collect($rules)->each(function ($rule) {
101 10
            $this->setRules(
102 10
                collect($this->extractRules($rule))
103 10
                    ->flatten()
104 10
                    ->merge($this->data['rules'])
105 10
                    ->toArray()
106
            );
107 10
        });
108
109 10
        return $this;
110
    }
111
112 1
    public function nullable()
113
    {
114 1
        $this->rule('nullable');
115
116 1
        return $this;
117
    }
118
119 7
    public function isNullable(): bool
120
    {
121 7
        return $this->hasRule('nullable');
122
    }
123
124 7
    public function hasRule(string $rule): bool
125
    {
126 7
        return in_array($rule, $this->getRules());
127
    }
128
129 11
    protected function extractRules($rules): array
130
    {
131 11
        if (is_array($rules)) {
132 1
            return $rules;
133 11
        } elseif (is_string($rules)) {
134 11
            return explode('|', $rules);
135 1
        } elseif (is_object($rules)) {
136 1
            return [$rules];
137
        }
138
139
        return [];
140
    }
141
142 11
    public function getRules()
143
    {
144 11
        return $this->data['rules'];
145
    }
146
147 6
    public function getCast()
148
    {
149 6
        return $this->cast;
150
    }
151
152 7
    public function hasDefault()
153
    {
154 7
        return array_key_exists('default', $this->data);
155
    }
156
157 4
    public function getDefault()
158
    {
159 4
        return $this->data['default'] ?? null;
160
    }
161
162 4
    protected function setDefault($value)
163
    {
164 4
        $this->data['default'] = $value;
165 4
    }
166
}
167