| 1 |
<?php |
| 2 |
|
| 3 |
namespace Rakit\Validation; |
| 4 |
|
| 5 |
class Attribute |
| 6 |
{ |
| 7 |
|
| 8 |
/** @var array */ |
| 9 |
protected $rules = []; |
| 10 |
|
| 11 |
/** @var string */ |
| 12 |
protected $key; |
| 13 |
|
| 14 |
/** @var string|null */ |
| 15 |
protected $alias; |
| 16 |
|
| 17 |
/** @var \Rakit\Validation\Validation */ |
| 18 |
protected $validation; |
| 19 |
|
| 20 |
/** @var bool */ |
| 21 |
protected $required = false; |
| 22 |
|
| 23 |
/** @var \Rakit\Validation\Validation|null */ |
| 24 |
protected $primaryAttribute = null; |
| 25 |
|
| 26 |
/** @var array */ |
| 27 |
protected $otherAttributes = []; |
| 28 |
|
| 29 |
/** @var array */ |
| 30 |
protected $keyIndexes = []; |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructor |
| 34 |
* |
| 35 |
* @param \Rakit\Validation\Validation $validation |
| 36 |
* @param string $key |
| 37 |
* @param string|null $alias |
| 38 |
* @param array $rules |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function __construct( |
| 42 |
Validation $validation, |
| 43 |
string $key, |
| 44 |
$alias = null, |
| 45 |
array $rules = [] |
| 46 |
) { |
| 47 |
$this->validation = $validation; |
| 48 |
$this->alias = $alias; |
| 49 |
$this->key = $key; |
| 50 |
foreach ($rules as $rule) { |
| 51 |
$this->addRule($rule); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Set the primary attribute |
| 57 |
* |
| 58 |
* @param \Rakit\Validation\Attribute $primaryAttribute |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function setPrimaryAttribute(Attribute $primaryAttribute) |
| 62 |
{ |
| 63 |
$this->primaryAttribute = $primaryAttribute; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Set key indexes |
| 68 |
* |
| 69 |
* @param array $keyIndexes |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public function setKeyIndexes(array $keyIndexes) |
| 73 |
{ |
| 74 |
$this->keyIndexes = $keyIndexes; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get primary attributes |
| 79 |
* |
| 80 |
* @return \Rakit\Validation\Attribute|null |
| 81 |
*/ |
| 82 |
public function getPrimaryAttribute() |
| 83 |
{ |
| 84 |
return $this->primaryAttribute; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Set other attributes |
| 89 |
* |
| 90 |
* @param array $otherAttributes |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function setOtherAttributes(array $otherAttributes) |
| 94 |
{ |
| 95 |
$this->otherAttributes = []; |
| 96 |
foreach ($otherAttributes as $otherAttribute) { |
| 97 |
$this->addOtherAttribute($otherAttribute); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Add other attributes |
| 103 |
* |
| 104 |
* @param \Rakit\Validation\Attribute $otherAttribute |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
public function addOtherAttribute(Attribute $otherAttribute) |
| 108 |
{ |
| 109 |
$this->otherAttributes[] = $otherAttribute; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get other attributes |
| 114 |
* |
| 115 |
* @return array |
| 116 |
*/ |
| 117 |
public function getOtherAttributes(): array |
| 118 |
{ |
| 119 |
return $this->otherAttributes; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Add rule |
| 124 |
* |
| 125 |
* @param \Rakit\Validation\Rule $rule |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
public function addRule(Rule $rule) |
| 129 |
{ |
| 130 |
$rule->setAttribute($this); |
| 131 |
$rule->setValidation($this->validation); |
| 132 |
$this->rules[$rule->getKey()] = $rule; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get rule |
| 137 |
* |
| 138 |
* @param string $ruleKey |
| 139 |
* @return void |
| 140 |
*/ |
| 141 |
public function getRule(string $ruleKey) |
| 142 |
{ |
| 143 |
return $this->hasRule($ruleKey)? $this->rules[$ruleKey] : null; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Get rules |
| 148 |
* |
| 149 |
* @return array |
| 150 |
*/ |
| 151 |
public function getRules(): array |
| 152 |
{ |
| 153 |
return $this->rules; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Check the $ruleKey has in the rule |
| 158 |
* |
| 159 |
* @param string $ruleKey |
| 160 |
* @return bool |
| 161 |
*/ |
| 162 |
public function hasRule(string $ruleKey): bool |
| 163 |
{ |
| 164 |
return isset($this->rules[$ruleKey]); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Set required |
| 169 |
* |
| 170 |
* @param boolean $required |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
public function setRequired(bool $required) |
| 174 |
{ |
| 175 |
$this->required = $required; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Set rule is required |
| 180 |
* |
| 181 |
* @return boolean |
| 182 |
*/ |
| 183 |
public function isRequired(): bool |
| 184 |
{ |
| 185 |
return $this->required; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Get key |
| 190 |
* |
| 191 |
* @return string |
| 192 |
*/ |
| 193 |
public function getKey(): string |
| 194 |
{ |
| 195 |
return $this->key; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Get key indexes |
| 200 |
* |
| 201 |
* @return array |
| 202 |
*/ |
| 203 |
public function getKeyIndexes(): array |
| 204 |
{ |
| 205 |
return $this->keyIndexes; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get value |
| 210 |
* |
| 211 |
* @param string|null $key |
| 212 |
* @return mixed |
| 213 |
*/ |
| 214 |
public function getValue(string $key = null) |
| 215 |
{ |
| 216 |
if ($key && $this->isArrayAttribute()) { |
| 217 |
$key = $this->resolveSiblingKey($key); |
| 218 |
} |
| 219 |
|
| 220 |
if (!$key) { |
| 221 |
$key = $this->getKey(); |
| 222 |
} |
| 223 |
|
| 224 |
return $this->validation->getValue($key); |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Get that is array attribute |
| 229 |
* |
| 230 |
* @return boolean |
| 231 |
*/ |
| 232 |
public function isArrayAttribute(): bool |
| 233 |
{ |
| 234 |
return count($this->getKeyIndexes()) > 0; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Check this attribute is using dot notation |
| 239 |
* |
| 240 |
* @return boolean |
| 241 |
*/ |
| 242 |
public function isUsingDotNotation(): bool |
| 243 |
{ |
| 244 |
return strpos($this->getKey(), '.') !== false; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Resolve sibling key |
| 249 |
* |
| 250 |
* @param string $key |
| 251 |
* @return string |
| 252 |
*/ |
| 253 |
public function resolveSiblingKey(string $key): string |
| 254 |
{ |
| 255 |
$indexes = $this->getKeyIndexes(); |
| 256 |
$keys = explode("*", $key); |
| 257 |
$countAsterisks = count($keys) - 1; |
| 258 |
if (count($indexes) < $countAsterisks) { |
| 259 |
$indexes = array_merge($indexes, array_fill(0, $countAsterisks - count($indexes), "*")); |
| 260 |
} |
| 261 |
$args = array_merge([str_replace("*", "%s", $key)], $indexes); |
| 262 |
return call_user_func_array('sprintf', $args); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Get humanize key |
| 267 |
* |
| 268 |
* @return string |
| 269 |
*/ |
| 270 |
public function getHumanizedKey() |
| 271 |
{ |
| 272 |
$primaryAttribute = $this->getPrimaryAttribute(); |
| 273 |
$key = str_replace('_', ' ', $this->key); |
| 274 |
|
| 275 |
// Resolve key from array validation |
| 276 |
if ($primaryAttribute) { |
| 277 |
$split = explode('.', $key); |
| 278 |
$key = implode(' ', array_map(function ($word) { |
| 279 |
if (is_numeric($word)) { |
| 280 |
$word = $word + 1; |
| 281 |
} |
| 282 |
return Helper::snakeCase($word, ' '); |
| 283 |
}, $split)); |
| 284 |
} |
| 285 |
|
| 286 |
return ucfirst($key); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Set alias |
| 291 |
* |
| 292 |
* @param string $alias |
| 293 |
* @return void |
| 294 |
*/ |
| 295 |
public function setAlias(string $alias) |
| 296 |
{ |
| 297 |
$this->alias = $alias; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Get alias |
| 302 |
* |
| 303 |
* @return string|null |
| 304 |
*/ |
| 305 |
public function getAlias() |
| 306 |
{ |
| 307 |
return $this->alias; |
| 308 |
} |
| 309 |
} |
| 310 |
|