Skip to content

Commit 8f978ff

Browse files
authored
Merge pull request #665 from ukeloop/refactor/rules_class
Refactored the `Rules` Class
2 parents a588eb8 + b6e6022 commit 8f978ff

File tree

1 file changed

+62
-46
lines changed

1 file changed

+62
-46
lines changed

src/Kris/LaravelFormBuilder/Rules.php

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Kris\LaravelFormBuilder;
44

5+
use InvalidArgumentException;
6+
57
class Rules
68
{
7-
89
/**
910
* @var string|null
1011
*/
@@ -30,108 +31,124 @@ class Rules
3031
* @param array $attributes
3132
* @param array $messages
3233
*/
33-
public function __construct(array $rules, array $attributes = [], array $messages = []) {
34+
public function __construct(array $rules, array $attributes = [], array $messages = [])
35+
{
3436
$this->rules = $rules;
3537
$this->attributes = $attributes;
3638
$this->messages = $messages;
3739
}
3840

3941
/**
4042
* @param string $name
43+
* @return $this
4144
*/
42-
public function setFieldName($name) {
45+
public function setFieldName($name)
46+
{
4347
$this->fieldName = $name;
4448

4549
return $this;
4650
}
4751

4852
/**
49-
* @param string $fieldName
53+
* @param string|null $fieldName
54+
* @return array|mixed
5055
*/
51-
public function getFieldRules($fieldName = null) {
52-
$fieldName = $this->ensureFieldName($fieldName);
56+
public function getFieldRules($fieldName = null)
57+
{
58+
$fieldName = $this->ensureFieldName($fieldName);
5359

54-
$rules = $this->rules;
55-
return isset($rules[$fieldName]) ? $rules[$fieldName] : [];
60+
$rules = $this->rules;
61+
return $rules[$fieldName] ?? [];
5662
}
5763

5864
/**
59-
* @param mixes $rule
60-
* @param string $fieldName
65+
* @param mixed $rule
66+
* @param string|null $fieldName
67+
* @return void
6168
*/
62-
public function addFieldRule($rule, $fieldName = null) {
63-
$rules = $this->getFieldRules($fieldName);
64-
$rules[] = $rule;
65-
$this->setFieldRules($rules, $fieldName);
69+
public function addFieldRule($rule, $fieldName = null)
70+
{
71+
$rules = $this->getFieldRules($fieldName);
72+
$rules[] = $rule;
73+
$this->setFieldRules($rules, $fieldName);
6674
}
6775

6876
/**
6977
* @param array $rules
70-
* @param string $fieldName
78+
* @param string|null $fieldName
79+
* @return void
7180
*/
72-
public function setFieldRules(array $rules, $fieldName = null) {
73-
$fieldName = $this->ensureFieldName($fieldName);
74-
$this->rules[$fieldName] = $rules;
81+
public function setFieldRules(array $rules, $fieldName = null)
82+
{
83+
$fieldName = $this->ensureFieldName($fieldName);
84+
$this->rules[$fieldName] = $rules;
7585
}
7686

7787
/**
78-
* @param string $fieldName
88+
* @param string|null $fieldName
89+
* @return string|null
90+
* @throws InvalidArgumentException
7991
*/
80-
protected function ensureFieldName($fieldName) {
81-
if (!$fieldName) {
82-
if (!$this->fieldName) {
83-
throw new InvalidArgumentException("Field functions on non-field Rules need explicit field name");
92+
protected function ensureFieldName($fieldName)
93+
{
94+
if (!$fieldName) {
95+
if (!$this->fieldName) {
96+
throw new InvalidArgumentException("Field functions on non-field Rules need explicit field name");
97+
}
98+
99+
$fieldName = $this->fieldName;
84100
}
85101

86-
$fieldName = $this->fieldName;
87-
}
88-
89-
return $fieldName;
102+
return $fieldName;
90103
}
91104

92105
/**
93-
* @param array $rules
94-
* @param array $attributes
95-
* @param array $messages
106+
* @param array|static $rules
107+
* @return $this
96108
*/
97-
public function append($rules) {
98-
if (is_array($rules)) {
99-
$rules = static::fromArray($rules);
100-
}
109+
public function append($rules)
110+
{
111+
if (is_array($rules)) {
112+
$rules = static::fromArray($rules);
113+
}
101114

102-
$this->rules = array_replace_recursive($this->rules, $rules->getRules());
103-
$this->attributes = array_replace_recursive($this->attributes, $rules->getAttributes());
104-
$this->messages = array_replace_recursive($this->messages, $rules->getMessages());
115+
$this->rules = array_replace_recursive($this->rules, $rules->getRules());
116+
$this->attributes = array_replace_recursive($this->attributes, $rules->getAttributes());
117+
$this->messages = array_replace_recursive($this->messages, $rules->getMessages());
105118

106-
return $this;
119+
return $this;
107120
}
108121

109122
/**
110123
* @return array
111124
*/
112-
public function getRules() {
113-
return $this->rules;
125+
public function getRules()
126+
{
127+
return $this->rules;
114128
}
115129

116130
/**
117131
* @return array
118132
*/
119-
public function getAttributes() {
120-
return $this->attributes;
133+
public function getAttributes()
134+
{
135+
return $this->attributes;
121136
}
122137

123138
/**
124139
* @return array
125140
*/
126-
public function getMessages() {
127-
return $this->messages;
141+
public function getMessages()
142+
{
143+
return $this->messages;
128144
}
129145

130146
/**
131147
* @param array[] $rules
132148
* @return static
133149
*/
134-
static public function fromArray($rules) {
150+
public static function fromArray($rules)
151+
{
135152
if (!$rules) {
136153
return new static([]);
137154
}
@@ -144,5 +161,4 @@ static public function fromArray($rules) {
144161

145162
return new static($rules['rules'], $rules['attributes'], $rules['error_messages']);
146163
}
147-
148164
}

0 commit comments

Comments
 (0)