Issues (36)

src/Attribute.php (1 issue)

Severity
1
<?php
2
3
namespace Larapie\Actions;
4
5
use Faker\Factory;
6
use Faker\Generator;
7
use Larapie\Actions\Attributes\Rules\GeneralRules;
8
9
class Attribute
10
{
11
    use GeneralRules;
12
13
    protected $data = [];
14
15
    protected $cast = null;
16
17 13
    final public function __construct()
18
    {
19 13
        $this->boot();
20 13
    }
21
22 13
    protected function boot()
23
    {
24 13
        $this->initializeRules();
25 13
    }
26
27 13
    protected function initializeRules()
28
    {
29 13
        if (method_exists($this, 'rules')) {
30 6
            $this->data['rules'] = $this->extractRules($this->rules());
31
        } else {
32 9
            $this->data['rules'] = [];
33
        }
34 13
    }
35
36
    /**
37
     * @return static
38
     */
39 12
    protected static function make()
40
    {
41 12
        return new static();
42
    }
43
44
45
    /**
46
     * @return static
47
     */
48 4
    public static function default($value)
49
    {
50 4
        $attribute = static::make();
51 4
        $attribute->setDefault($value);
52
53 4
        return $attribute;
54
    }
55
56
    /**
57
     * @return static
58
     */
59 9
    public static function required()
60
    {
61 9
        return static::make()->require();
62
    }
63
64
    /**
65
     * @return static
66
     */
67 9
    public function require()
68
    {
69 9
        return $this->rule('required');
70
    }
71
72
    /**
73
     * @return static
74
     */
75 4
    public function cast($cast)
76
    {
77 4
        if (is_callable($cast) || is_string($cast)) {
78 2
            $this->cast = $cast;
79
        }
80
81 4
        return $this;
82
    }
83
84
    /**
85
     * @return static
86
     */
87 1
    public static function optional()
88
    {
89 1
        return tap(static::make(), function (Attribute $attribute) {
90 1
            $attribute->setRules(
91 1
                collect($attribute->getRules())
92 1
                    ->reject(function ($value) {
93 1
                        return $value === 'required';
94 1
                    })
95 1
                    ->toArray()
96
            );
97 1
        });
98
    }
99
100
    /**
101
     * @return mixed
102
     */
103
    public static function fake()
104
    {
105
        return static::make()->factory(Factory::create());
106
    }
107
108
    public function factory(Generator $faker)
0 ignored issues
show
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

108
    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...
109
    {
110
        throw new \RuntimeException('The fake method for this attribute is not implemented.');
111
    }
112
113 13
    protected function setRules(array $rules)
114
    {
115 13
        $this->data['rules'] = $rules;
116 13
    }
117
118
    /**
119
     * @param mixed ...$rules
120
     * @return static
121
     */
122 12
    public function rule(...$rules)
123
    {
124 12
        collect($rules)->each(function ($rule) {
125 12
            $this->setRules(
126 12
                collect($this->extractRules($rule))
127 12
                    ->flatten()
128 12
                    ->merge($this->data['rules'])
129 12
                    ->unique()
130 12
                    ->toArray()
131
            );
132 12
        });
133
134 12
        return $this;
135
    }
136
137 6
    public function isNullable(): bool
138
    {
139 6
        return $this->hasRule('nullable');
140
    }
141
142 6
    public function hasRule(string $rule): bool
143
    {
144 6
        return in_array($rule, $this->getRules());
145
    }
146
147 13
    protected function extractRules($rules): array
148
    {
149 13
        if (is_array($rules)) {
150 1
            return $rules;
151 13
        } elseif (is_string($rules)) {
152 13
            return explode('|', $rules);
153 1
        } elseif (is_object($rules)) {
154 1
            return [$rules];
155
        }
156
157
        return [];
158
    }
159
160 13
    public function getRules(): array
161
    {
162 13
        return $this->data['rules'];
163
    }
164
165 5
    public function getCast()
166
    {
167 5
        return $this->cast;
168
    }
169
170 9
    public function hasDefault(): bool
171
    {
172 9
        return array_key_exists('default', $this->data);
173
    }
174
175 4
    public function getDefault()
176
    {
177 4
        return $this->data['default'] ?? null;
178
    }
179
180 4
    protected function setDefault($value)
181
    {
182 4
        $this->data['default'] = $value;
183 4
    }
184
185
    public function __toString()
186
    {
187
        return implode('|', $this->getRules());
188
    }
189
}
190