Skip to content

Commit 6dccadd

Browse files
authored
Fix #173: Do not throw MissingAttributeException (#285)
1 parent 1b5bc01 commit 6dccadd

7 files changed

Lines changed: 29 additions & 39 deletions

File tree

src/DataSet/ArrayDataTrait.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,16 @@
44

55
namespace Yiisoft\Validator\DataSet;
66

7-
use Yiisoft\Validator\Exception\MissingAttributeException;
8-
97
/**
108
* @internal
119
*/
1210
trait ArrayDataTrait
1311
{
1412
private array $data;
1513

16-
public function getAttributeValue(string $attribute)
14+
public function getAttributeValue(string $attribute): mixed
1715
{
18-
if (!isset($this->data[$attribute])) {
19-
throw new MissingAttributeException("There is no \"$attribute\" key in the array.");
20-
}
21-
22-
return $this->data[$attribute];
16+
return $this->data[$attribute] ?? null;
2317
}
2418

2519
public function getData(): array

src/DataSet/ScalarDataSet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __construct($value)
1515
$this->value = $value;
1616
}
1717

18-
public function getAttributeValue(string $attribute)
18+
public function getAttributeValue(string $attribute): mixed
1919
{
2020
return $this->value;
2121
}

src/DataSetInterface.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace Yiisoft\Validator;
66

7-
use Yiisoft\Validator\Exception\MissingAttributeException;
8-
97
/**
108
* DataSetInterface represents a key-value data set.
119
*/
@@ -14,11 +12,9 @@ interface DataSetInterface
1412
/**
1513
* Get specified attribute value.
1614
*
17-
* @throws MissingAttributeException If there is no such value.
18-
*
1915
* @return mixed
2016
*/
21-
public function getAttributeValue(string $attribute);
17+
public function getAttributeValue(string $attribute): mixed;
2218

2319
public function getData(): mixed;
2420
}

src/Exception/MissingAttributeException.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/Stub/DataSet.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Yiisoft\Validator\Tests\Stub;
66

77
use Yiisoft\Validator\DataSetInterface;
8-
use Yiisoft\Validator\Exception\MissingAttributeException;
98

109
final class DataSet implements DataSetInterface
1110
{
@@ -16,13 +15,9 @@ public function __construct(array $data = [])
1615
$this->data = $data;
1716
}
1817

19-
public function getAttributeValue(string $attribute)
18+
public function getAttributeValue(string $attribute): mixed
2019
{
21-
if (!isset($this->data[$attribute])) {
22-
throw new MissingAttributeException("There is no \"$attribute\" attribute in the class.");
23-
}
24-
25-
return $this->data[$attribute];
20+
return $this->data[$attribute] ?? null;
2621
}
2722

2823
public function getData(): mixed

tests/Stub/RulesProvidedDataSet.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Yiisoft\Validator\Tests\Stub;
66

77
use Yiisoft\Validator\DataSetInterface;
8-
use Yiisoft\Validator\Exception\MissingAttributeException;
98
use Yiisoft\Validator\RulesProviderInterface;
109

1110
final class RulesProvidedDataSet implements RulesProviderInterface, DataSetInterface
@@ -19,13 +18,9 @@ public function __construct(array $data, array $rules)
1918
$this->rules = $rules;
2019
}
2120

22-
public function getAttributeValue(string $attribute)
21+
public function getAttributeValue(string $attribute): mixed
2322
{
24-
if (!isset($this->data[$attribute])) {
25-
throw new MissingAttributeException("There is no \"$attribute\" attribute in the class.");
26-
}
27-
28-
return $this->data[$attribute];
23+
return $this->data[$attribute] ?? null;
2924
}
3025

3126
public function getRules(): iterable

tests/ValidatorTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use PHPUnit\Framework\TestCase;
88
use stdClass;
9+
use Yiisoft\Validator\DataSet\ArrayDataSet;
10+
use Yiisoft\Validator\Error;
911
use Yiisoft\Validator\Exception\RuleHandlerInterfaceNotImplementedException;
1012
use Yiisoft\Validator\Exception\RuleHandlerNotFoundException;
1113
use Yiisoft\Validator\Result;
@@ -143,4 +145,23 @@ public function getHandlerClassName(): string
143145
],
144146
]);
145147
}
148+
149+
/**
150+
* @link https://github.com/yiisoft/validator/issues/173
151+
*/
152+
public function testMissingRequiredAttribute(): void
153+
{
154+
$validator = FakeValidatorFactory::make();
155+
$dataSet = new ArrayDataSet([
156+
'merchantIdd' => 1,
157+
]);
158+
$rules = [
159+
'merchantId' => [new Required(), new Number(asInteger: true)],
160+
];
161+
$result = $validator->validate($dataSet, $rules);
162+
$this->assertEquals([
163+
new Error('Value cannot be blank.', ['merchantId']),
164+
new Error('Value must be an integer.', ['merchantId']),
165+
], $result->getErrors());
166+
}
146167
}

0 commit comments

Comments
 (0)