Completed
Push — master ( 67e977...5a3a1c )
by Rudie
02:04
created

Rules::addFieldRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kris\LaravelFormBuilder;
4
5
class Rules
6
{
7
8
    /**
9
     * @var string|null
10
     */
11
    protected $fieldName;
12
13
    /**
14
     * @var array
15
     */
16
    protected $rules;
17
18
    /**
19
     * @var array
20
     */
21
    protected $attributes;
22
23
    /**
24
     * @var array
25
     */
26
    protected $messages;
27
28
    /**
29
     * @param array $rules
30
     * @param array $attributes
31
     * @param array $messages
32
     */
33 9
    public function __construct(array $rules, array $attributes = [], array $messages = []) {
34 9
        $this->rules = $rules;
35 9
        $this->attributes = $attributes;
36 9
        $this->messages = $messages;
37 9
    }
38
39
    /**
40
     * @param string $name
41
     */
42 9
    public function setFieldName($name) {
43 9
        $this->fieldName = $name;
44
45 9
        return $this;
46
    }
47
48
    /**
49
     * @param string $fieldName
50
     */
51
    public function getFieldRules($fieldName = null) {
52
      $fieldName = $this->ensureFieldName($fieldName);
53
54
      $rules = $this->rules;
55
      return isset($rules[$fieldName]) ? $rules[$fieldName] : [];
56
    }
57
58
    /**
59
     * @param mixes $rule
60
     * @param string $fieldName
61
     */
62
    public function addFieldRule($rule, $fieldName = null) {
63
      $rules = $this->getFieldRules($fieldName);
64
      $rules[] = $rule;
65
      $this->setFieldRules($rules, $fieldName);
66
    }
67
68
    /**
69
     * @param array $rules
70
     * @param string $fieldName
71
     */
72
    public function setFieldRules(array $rules, $fieldName = null) {
73
      $fieldName = $this->ensureFieldName($fieldName);
74
      $this->rules[$fieldName] = $rules;
75
    }
76
77
    /**
78
     * @param string $fieldName
79
     */
80
    protected function ensureFieldName($fieldName) {
81
      if (!$fieldName) {
82
        if (!$this->fieldName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->fieldName of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
83
          throw new InvalidArgumentException("Field functions on non-field Rules need explicit field name");
84
        }
85
86
        $fieldName = $this->fieldName;
87
      }
88
89
      return $fieldName;
90
    }
91
92
    /**
93
     * @param array $rules
94
     * @param array $attributes
0 ignored issues
show
Bug introduced by
There is no parameter named $attributes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
95
     * @param array $messages
0 ignored issues
show
Bug introduced by
There is no parameter named $messages. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
96
     */
97 9
    public function append($rules) {
98 9
      if (is_array($rules)) {
99
        $rules = static::fromArray($rules);
100
      }
101
102 9
      $this->rules = array_replace_recursive($this->rules, $rules->getRules());
103 9
      $this->attributes = array_replace_recursive($this->attributes, $rules->getAttributes());
104 9
      $this->messages = array_replace_recursive($this->messages, $rules->getMessages());
105
106 9
      return $this;
107
    }
108
109
    /**
110
     * @return array
111
     */
112 9
    public function getRules() {
113 9
      return $this->rules;
114
    }
115
116
    /**
117
     * @return array
118
     */
119 9
    public function getAttributes() {
120 9
      return $this->attributes;
121
    }
122
123
    /**
124
     * @return array
125
     */
126 9
    public function getMessages() {
127 9
      return $this->messages;
128
    }
129
130
    /**
131
     * @param array[] $rules
132
     * @return static
133
     */
134
    static public function fromArray($rules) {
135
        if (!$rules) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rules of type array[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
136
            return new static([]);
137
        }
138
139
        $rules += [
140
            'rules' => [],
141
            'attributes' => [],
142
            'error_messages' => [],
143
        ];
144
145
        return new static($rules['rules'], $rules['attributes'], $rules['error_messages']);
146
    }
147
148
}
149