| Total Complexity | 11 |
| Total Lines | 59 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 8 | abstract class AbstractResourceRuleSet implements ResourceRuleSetContract |
||
| 9 | { |
||
| 10 | public function rules(?string $key = null) : array |
||
| 11 | { |
||
| 12 | return $this->getRules($this->provideRules(), $key); |
||
| 13 | } |
||
| 14 | |||
| 15 | public function creationRules(?string $key = null) : array |
||
| 16 | { |
||
| 17 | return $this->getRules(array_merge_recursive( |
||
| 18 | $this->provideCreationRules(), |
||
| 19 | $this->provideRules(), |
||
| 20 | ), $key); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function updateRules(?string $key = null) : array |
||
| 24 | { |
||
| 25 | return $this->getRules(array_merge_recursive( |
||
| 26 | $this->provideUpdateRules(), |
||
| 27 | $this->provideRules(), |
||
| 28 | ), $key); |
||
| 29 | } |
||
| 30 | |||
| 31 | protected function getRules(array $rules, ?string $key) : array |
||
| 32 | { |
||
| 33 | if (null === $key) { |
||
| 34 | return $rules; |
||
| 35 | } |
||
| 36 | |||
| 37 | return $this->getRulesForKey($rules, $key); |
||
| 38 | } |
||
| 39 | |||
| 40 | protected function getRulesForKey(array $rules, string $key) : array |
||
| 41 | { |
||
| 42 | $this->guardAgainstInvalidKey($key, $rules); |
||
| 43 | |||
| 44 | return $rules[$key]; |
||
| 45 | } |
||
| 46 | |||
| 47 | protected function guardAgainstInvalidKey(string $key, array $rules) |
||
| 48 | { |
||
| 49 | if (! array_key_exists($key, $rules)) { |
||
| 50 | throw new OutOfBoundsException("invalid key, '{$key}'"); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | protected function provideRules() : array |
||
| 55 | { |
||
| 56 | return []; |
||
| 57 | } |
||
| 58 | |||
| 59 | protected function provideCreationRules() : array |
||
| 60 | { |
||
| 61 | return []; |
||
| 62 | } |
||
| 63 | |||
| 64 | protected function provideUpdateRules() : array |
||
| 67 | } |
||
| 68 | } |
||
| 69 |