Skip to content

Commit 13b532f

Browse files
Ajustes.
1 parent f569520 commit 13b532f

10 files changed

Lines changed: 105 additions & 28 deletions

src/AttributeAccessors/ArrayAttributeWrapper.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ protected function appendAttrToFailMessages($index, $failMsgs) {
7474
* @return boolean
7575
*/
7676
public function validate($target) {
77-
$ret = array();
77+
if (!is_array($target)) {
78+
return $this->validateRule->validate(AttributeNotExists::instance());
79+
}
7880

79-
$total = count($target);
81+
$ret = array();
8082

81-
for ($i = 0; $i < $total; $i++) {
82-
$ret = array_merge($ret, $this->appendAttrToFailMessages($i, $this->validateRule->validate(isset($target[$i]) ? $target[$i] : null)));
83+
foreach ($target as $key => $value) {
84+
$ret = array_merge($ret, $this->appendAttrToFailMessages($key, $this->validateRule->validate($value)));
8385
}
8486

8587
return $ret;

src/AttributeAccessors/ArrayNestedAttributeWrapper.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,14 @@ protected function appendAttrToFailMessages($index, $failMsgs) {
6161
}
6262

6363
public function validate($target) {
64-
if ($target == null) {
65-
return parent::validate(null);
64+
if (!is_array($target)) {
65+
return parent::validate(AttributeNotExists::instance());
6666
}
6767

6868
$ret = array();
6969

70-
$total = count($target);
71-
72-
for ($i = 0; $i < $total; $i++) {
73-
$ret = array_merge($ret, $this->appendAttrToFailMessages($i, parent::validate($target[$i])));
70+
foreach ($target as $key => $value) {
71+
$ret = array_merge($ret, $this->appendAttrToFailMessages($key, parent::validate($value)));
7472
}
7573

7674
return $ret;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace ArrayUtils\AttributeAccessors;
4+
5+
class AttributeNotExists {
6+
private static $instance;
7+
8+
public static final function instance() {
9+
if (!static::$instance) {
10+
static::$instance = new AttributeNotExists();
11+
}
12+
13+
return static::$instance;
14+
}
15+
}

src/AttributeAccessors/AttributeWrapper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ public function validate($target) {
107107
* @return mixed|null
108108
*/
109109
protected function get($target) {
110-
return isset($target[$this->attribute]) ? $target[$this->attribute] : null;
110+
if (!array_key_exists($this->attribute, $target)) {
111+
return AttributeNotExists::instance();
112+
}
113+
114+
return $target[$this->attribute];
111115
}
112116
}

src/AttributeAccessors/NestedAttribute.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public function setValue(array &$target, $newValue) {
2222
$this->attributeAccessor->setValue($target, $newValue);
2323
}
2424

25-
2625
public function cast(array $target) {
2726
return $this->attributeAccessor->cast($target);
2827
}

src/AttributeAccessors/NestedAttributeWrapper.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct($attribute, AttributeAccessor $attributeAccessor) {
1616
}
1717

1818
public function getValue(array $target) {
19-
if (!isset($target[$this->attribute])) {
19+
if (!array_key_exists($this->attribute, $target)) {
2020
return null;
2121
}
2222

@@ -41,8 +41,21 @@ public function cast(array $target) {
4141
return $target;
4242
}
4343

44+
/**
45+
* @param array $target
46+
*
47+
* @return mixed|null
48+
*/
49+
protected function get($target) {
50+
if (!array_key_exists($this->attribute, $target)) {
51+
return AttributeNotExists::instance();
52+
}
53+
54+
return $target[$this->attribute];
55+
}
56+
4457
public function validate($target) {
45-
$ret = parent::validate(isset($target[$this->attribute]) ? $target[$this->attribute] : null);
58+
$ret = parent::validate($this->get($target));
4659

4760
foreach ($ret as &$failMsg) {
4861
$failMsg = $this->attribute . "." . $failMsg;

src/ValidationRules/DateValidation.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ private function validateWithFormat($value) {
3030
* @return array
3131
*/
3232
public function validate($value) {
33+
if (!is_string($value) || !is_numeric($value)) {
34+
return array("O campo deve ser um valor numérico ou string.");
35+
}
3336
if ($this->format) {
3437
return $this->validateWithFormat($value);
3538
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace ArrayUtils\ValidationRules;
4+
5+
use ArrayUtils\AttributeAccessors\AttributeNotExists;
6+
use ArrayUtils\ValidationRule;
7+
8+
class IsPresentValidation implements ValidationRule {
9+
10+
/**
11+
* @return string
12+
*/
13+
public function getIdentifier() {
14+
return "present";
15+
}
16+
17+
/**
18+
* @param $value
19+
*
20+
* @return array
21+
*/
22+
public function validate($value) {
23+
if ($value instanceof AttributeNotExists) {
24+
return array("O campo deve estar presente.");
25+
}
26+
27+
return array();
28+
}
29+
30+
/**
31+
* @param mixed $params
32+
*/
33+
public function setParams($params) {
34+
}
35+
}

src/ValidationRules/ValidationRuleFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public static function load() {
2222
new DateValidation(),
2323
new InValidation(),
2424
new NotInValidation(),
25-
new StringValidation()
25+
new StringValidation(),
26+
new IsPresentValidation(),
27+
new BooleanValidation()
2628
);
2729

2830
foreach ($classes as $instance) {

test/ValidatorTest.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,31 @@ public function testValidator() {
1313
//->addRule('c', 'required')
1414
//->addRule('a.b.c', 'required')
1515
//->addRule('d.e', 'required')
16-
->addRule('f.x.*', 'numeric')
17-
->addRule('w.x.*.a', 'required|numeric|max:5')
18-
->addRule('y.*.a', 'required|numeric|max:5')
16+
//->addRule('f.x.*', 'numeric')
17+
//->addRule('w.x.*.a', 'required|numeric|max:5')
18+
//->addRule('y.*.a', 'required|numeric|max:5')
19+
//->addRule('*', 'required|numeric|max:5')
20+
->addRule("a.b.*.c", 'present|required')
1921
;
2022

2123
$valores = [];
2224

23-
for ($i = 0; $i < 5; $i++) {
24-
$valores[] = ["a" => $i];
25+
for ($i = 0; $i < 10; $i++) {
26+
//$valores[] = ["a" => $i];
27+
$valores[] = $i;
2528
}
2629

27-
$ret = $validator->validate([
28-
'c' => 10,
29-
'a' => ['b' => 1],
30-
'd' => ['e' => []],
31-
'f' => ['x' => [1, 2, 3, '23x']],
32-
'w' => ['x' => [["a" => "1"], ["a" => "1"], ["a" => "20"]]],
33-
'y' => $valores
34-
]);
30+
//$ret = $validator->validate([
31+
// 'c' => 10,
32+
// 'a' => ['b' => 1],
33+
// 'd' => ['e' => []],
34+
// 'f' => ['x' => [1, 2, 3, '23x']],
35+
// 'w' => ['x' => [["a" => "1"], ["a" => "1"], ["a" => "20"]]],
36+
// 'y' => $valores
37+
//]);
38+
//$ret = $validator->validate($valores);
39+
40+
$ret = $validator->validate(["a" => ["b" => [["c" => null]]]]);
3541

3642
print_r($ret);
3743

0 commit comments

Comments
 (0)