Completed
Push — master ( bcf757...846461 )
by Fumio
03:51 queued 52s
created

InputSpec   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 75%

Importance

Changes 6
Bugs 3 Features 1
Metric Value
c 6
b 3
f 1
dl 0
loc 198
ccs 48
cts 64
cp 0.75
rs 10
wmc 25
lcom 2
cbo 1

13 Methods

Rating   Name   Duplication   Size   Complexity  
A attributes() 0 4 1
A rules() 0 4 1
A ruleMessages() 0 6 1
A labels() 0 6 1
A values() 0 6 1
A required() 0 4 1
A label() 0 6 1
A helptext() 0 6 1
A hasRule() 0 10 3
A fullkey() 0 4 2
B __construct() 0 27 5
A hasRuleInArray() 0 10 3
A resolveSpecReferences() 0 16 4
1
<?php
2
3
namespace LaravelPlus\Extension\Specs;
4
5
use LaravelPlus\Extension\Repository\NamespacedRepository;
6
use Symfony\Component\Translation\TranslatorInterface;
7
use InvalidArgumentException;
8
9
class InputSpec
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $namespace;
15
16
    /**
17
     * @var string
18
     */
19
    protected $path;
20
21
    /**
22
     * @var array
23
     */
24
    protected $rules = [];
25
26
    /**
27
     * @var \LaravelPlus\Extension\Specs\Translator
28
     */
29
    protected $translator;
30
31
    /**
32
     * @param \LaravelPlus\Extension\Repository\NamespacedRepository $specs
33
     * @param \Symfony\Component\Translation\TranslatorInterface $translator
34
     * @param string $path
35
     */
36 6
    public function __construct(NamespacedRepository $specs, TranslatorInterface $translator, $path)
37
    {
38 6
        $rules = $specs->get($path);
39
40 6
        if (is_null($rules)) {
41
            throw new InvalidArgumentException("spec '$path' is not found");
42
        }
43 6
        if (!is_array($rules)) {
44
            throw new InvalidArgumentException('$rules must array in path '.$path);
45
        }
46 6
        if (!$translator->has($path)) {
47
            throw new InvalidArgumentException("translate '$path' is not found");
48
        }
49
50 6
        if (strpos($path, '::') !== false) {
51
            list($namespace, $path) = explode('::', $path, 2);
52
        } else {
53 6
            $namespace = '';
54
        }
55
56 6
        $this->path = $path;
57 6
        $this->namespace = $namespace;
58 6
        $this->rules = $rules;
59 6
        $this->translator = new Translator($translator, $namespace);
60
61 6
        $this->resolveSpecReferences();
62 6
    }
63
64
    /**
65
     * @return array
66
     */
67 3
    public function attributes()
68
    {
69 3
        return array_keys($this->rules);
70
    }
71
72
    /**
73
     * @return array
74
     */
75 3
    public function rules()
76
    {
77 3
        return $this->rules;
78
    }
79
80
    /**
81
     * @return array
82
     */
83 3
    public function ruleMessages()
84
    {
85 3
        $path = $this->path.'.rules';
86
87 3
        return $this->translator->get($path, []);
88
    }
89
90
    /**
91
     * @return array
92
     */
93 3
    public function labels()
94
    {
95 3
        $path = $this->path.'.attributes';
96
97 3
        return $this->translator->get($path, []);
98
    }
99
100
    /**
101
     * @return array
102
     */
103 1
    public function values()
104
    {
105 1
        $path = $this->path.'.values';
106
107 1
        return $this->translator->get($path, []);
108
    }
109
110
    /**
111
     * @param string $name
112
     *
113
     * @return bool
114
     */
115 1
    public function required($name)
116
    {
117 1
        return $this->hasRule($this->rules[$name], 'required');
118
    }
119
120
    /**
121
     * @param string $name
122
     *
123
     * @return string
124
     */
125 1
    public function label($name)
126
    {
127 1
        $path = $this->path.'.attributes.'.$name;
128
129 1
        return $this->translator->get($path);
130
    }
131
132
    /**
133
     * @param string $name
134
     *
135
     * @return string
136
     */
137 1
    public function helptext($name)
138
    {
139 1
        $path = $this->path.'.helptexts.'.$name;
140
141 1
        return $this->translator->get($path, '');
142
    }
143
144
    /**
145
     * @param mixed  $ruleOrRules
146
     * @param string $name
147
     *
148
     * @return bool
149
     */
150 1
    protected function hasRule($ruleOrRules, $name)
151
    {
152 1
        if (is_string($ruleOrRules)) {
153 1
            return $this->hasRuleInArray(explode('|', $ruleOrRules), 'required');
154
        } elseif (is_array($ruleOrRules)) {
155
            return $this->hasRuleInArray($ruleOrRules, 'required');
156
        } else {
157
            return false;
158
        }
159
    }
160
161
    /**
162
     * @param array  $rules
163
     * @param string $name
164
     *
165
     * @return bool
166
     */
167 1
    private function hasRuleInArray(array $rules, $name)
168
    {
169 1
        foreach ($rules as $rule) {
170 1
            if ($rule == $name) {
171 1
                return true;
172
            }
173 1
        }
174
175 1
        return false;
176
    }
177
178
    /**
179
     */
180 6
    private function resolveSpecReferences()
181
    {
182 6
        foreach ($this->rules as &$value) {
183 2
            if (strpos($value, '@') !== false) {
184
                list(, $key) = explode('@', $value, 2);
185
186
                $rule = app('specs')->get($this->fullkey('vocabulary.'.$key), null);
187
188
                if (empty($rule)) {
189
                    throw new InvalidArgumentException('specs/vocabulary '.$key.' not found.');
190
                }
191
192
                $value = $rule;
193
            }
194 6
        }
195 6
    }
196
197
    /**
198
     * @param string $key
199
     *
200
     * @return string
201
     */
202
    private function fullkey($key)
203
    {
204
        return $this->namespace ? $this->namespace.'::'.$key : $key;
205
    }
206
}
207