Skip to content

Commit ad6b57f

Browse files
Fix.
1 parent d7a9855 commit ad6b57f

File tree

8 files changed

+39
-23
lines changed

8 files changed

+39
-23
lines changed

src/AttributeAccessors/AttributeWrapper.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function setCastRule(CastRule $castRule) {
4848
}
4949

5050
public function getValue(array $target) {
51-
$value = $this->get($target);
51+
$value = $this->getSafe($target);
5252

5353
return $this->castRule && $this->castEnabled ? $this->castRule->cast($value) : $value;
5454
}
@@ -62,7 +62,7 @@ public function cast(array $target) {
6262
$target = array();
6363
}
6464

65-
$value = $this->get($target);
65+
$value = $this->getSafe($target);
6666

6767
$target[$this->attribute] = $this->castRule ? $this->castRule->cast($value) : $value;
6868

@@ -113,4 +113,8 @@ protected function get($target) {
113113

114114
return $target[$this->attribute];
115115
}
116+
117+
protected function getSafe($target) {
118+
return isset($target[$this->attribute]) ? $target[$this->attribute] : null;
119+
}
116120
}

src/CastRules/CastRuleBase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ private function executeOptions($value) {
3838
return $value;
3939
}
4040

41+
public function getParams() {
42+
return $this->params;
43+
}
44+
4145
public function cast($value) {
4246
return $this->executeOptions($value);
4347
}

src/CastRules/StringCastRule.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,31 @@ public function cast($value) {
1616
}
1717

1818
private function loadOptionsHandlers() {
19-
$params = $this->params;
19+
$self = $this;
20+
21+
$this->addOptionHandler("max_length", function($value) use ($self) {
22+
$params = $this->getParams();
2023

21-
$this->addOptionHandler("max_length", function($value) use ($params) {
2224
return substr(
2325
$value,
2426
0,
2527
isset($params["max_length"][0]) ? $params["max_length"][0] : 0
2628
);
2729
});
2830

29-
$this->addOptionHandler("rpad", function($value) use ($params) {
31+
$this->addOptionHandler("rpad", function($value) use ($self) {
32+
$params = $this->getParams();
33+
3034
return str_pad(
3135
$value,
3236
isset($params["rpad"][0]) ? $params["rpad"][0] : 0,
3337
isset($params["rpad"][1]) ? $params["rpad"][1] : "",
3438
STR_PAD_RIGHT);
3539
});
3640

37-
$this->addOptionHandler("lpad", function($value) use ($params) {
41+
$this->addOptionHandler("lpad", function($value) use ($self) {
42+
$params = $this->getParams();
43+
3844
return str_pad(
3945
$value,
4046
isset($params["lpad"][0]) ? $params["lpad"][0] : 0,

src/ValidationRules/NotInValidation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use ArrayUtils\ValidationRule;
66

77
class NotInValidation implements ValidationRule {
8-
private $allowedValues;
8+
private $allowedValues = array();
99

1010
/**
1111
* @return string
@@ -20,7 +20,7 @@ public function getIdentifier() {
2020
* @return array
2121
*/
2222
public function validate($value) {
23-
if (in_array($value, $this->allowedValues)) {
23+
if (is_scalar($value) && in_array($value, $this->allowedValues)) {
2424
return array("O campo não deve ser um destes valores " . implode(", ", $this->allowedValues));
2525
}
2626

test/AttributeAccessors/NestedAttributeTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class NestedAttributeTest extends \PHPUnit_Framework_TestCase {
1414

1515
public function testInstanceOf() {
1616
$mock = $this->getMockBuilder(AttributeAccessor::class)
17-
->setMethods(["getValue", "cast", "withoutCasting", "withCasting", "setValue"])
17+
->setMethods(["getValue", "cast", "withoutCasting", "withCasting", "setValue", "setValidateRule", "setCastRule", "validate"])
1818
->getMock();
1919

2020
$nestedAttr = new NestedAttribute($mock);
@@ -24,7 +24,7 @@ public function testInstanceOf() {
2424

2525
public function testGetValue() {
2626
$mock = $this->getMockBuilder(AttributeAccessor::class)
27-
->setMethods(["getValue", "cast", "withoutCasting", "withCasting", "setValue"])
27+
->setMethods(["getValue", "cast", "withoutCasting", "withCasting", "setValue", "setValidateRule", "setCastRule", "validate"])
2828
->getMock();
2929

3030
$mock->method("getValue")->willReturn("mockValue");
@@ -38,7 +38,7 @@ public function testGetValue() {
3838

3939
public function testCastValue() {
4040
$mock = $this->getMockBuilder(AttributeAccessor::class)
41-
->setMethods(["getValue", "cast", "withoutCasting", "withCasting", "setValue"])
41+
->setMethods(["getValue", "cast", "withoutCasting", "withCasting", "setValue", "setValidateRule", "setCastRule", "validate"])
4242
->getMock();
4343

4444
$mock->method("cast")->willReturn("mockValue");

test/CastHelperTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public function testSimpleCast() {
3333
->addRule("a.b.c5.*.d1", "string|lpad:2,0")
3434
->cast($value);
3535

36-
$this->assertEquals($ret["a"]["b"]["c1"] === "a");
37-
$this->assertEquals($ret["a"]["b"]["c2"] === 2);
38-
$this->assertEquals($ret["a"]["b"]["c3"] === 3.0);
39-
$this->assertEquals($ret["a"]["b"]["c4"][0] === 1);
40-
$this->assertEquals($ret["a"]["b"]["c4"][1] === 2);
41-
$this->assertEquals($ret["a"]["b"]["c4"][2] === 3);
42-
$this->assertEquals($ret["a"]["b"]["c5"][0]["d1"] === "09");
43-
$this->assertEquals($ret["a"]["b"]["c5"][0]["d1"] === "08");
44-
$this->assertEquals($ret["a"]["b"]["c5"][0]["d1"] === "07");
36+
$this->assertTrue($ret["a"]["b"]["c1"] === "a");
37+
$this->assertTrue($ret["a"]["b"]["c2"] === 2);
38+
$this->assertTrue($ret["a"]["b"]["c3"] === 3.0);
39+
$this->assertTrue($ret["a"]["b"]["c4"][0] === 1);
40+
$this->assertTrue($ret["a"]["b"]["c4"][1] === 2);
41+
$this->assertTrue($ret["a"]["b"]["c4"][2] === 3);
42+
$this->assertTrue($ret["a"]["b"]["c5"][0]["d1"] === "09");
43+
$this->assertTrue($ret["a"]["b"]["c5"][1]["d1"] === "08");
44+
$this->assertTrue($ret["a"]["b"]["c5"][2]["d1"] === "07");
4545
}
4646
}

test/ValidationRules/DateValidationTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function testInvalidValues() {
3737

3838
$validation->setParams(["d-m-Y"]);
3939

40-
$this->assertEmpty($validation->validate("01-31-2017"));
40+
$this->assertNotEmpty($validation->validate("01-31-2017"));
41+
$this->assertEmpty($validation->validate("31-01-2017"));
4142
}
4243
}

test/ValidationRules/NotInValidationTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ public function testValidValues() {
2525
public function testInvalidValues() {
2626
$validation = new NotInValidation();
2727

28+
$validation->setParams([1,"2", "tres"]);
2829
$this->assertNotEmpty($validation->validate("1"));
2930
$this->assertNotEmpty($validation->validate("2"));
3031
$this->assertNotEmpty($validation->validate("tres"));
31-
$this->assertNotEmpty($validation->validate(array()));
32-
$this->assertNotEmpty($validation->validate(AttributeNotExists::instance()));
32+
$this->assertEmpty($validation->validate(array()));
33+
$this->assertEmpty($validation->validate(AttributeNotExists::instance()));
3334
}
3435
}

0 commit comments

Comments
 (0)