Skip to content

Commit b14e96c

Browse files
vjikrector-botStyleCIBotsamdark
authored
Add DataWrapperInteface (#464)
* base implementation * improve * const * [ci-review] Apply changes from Rector action. * `DataWrapperInterface` * Apply fixes from StyleCI * tests * improve * rename * Apply fixes from StyleCI * docs * Apply fixes from StyleCI * rename const * Apply fixes from StyleCI * fix count * more test * [ci-review] Apply changes from Rector action. * more test * Apply fixes from StyleCI * Add DataWrapperInterface docs * Fix merge issue * fix Co-authored-by: rector-bot <[email protected]> Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Alexander Makarov <[email protected]>
1 parent 62cd3fa commit b14e96c

25 files changed

Lines changed: 153 additions & 101 deletions

src/DataSet/ArrayDataSet.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
namespace Yiisoft\Validator\DataSet;
66

7-
use Yiisoft\Validator\DataSetInterface;
8-
7+
use Yiisoft\Validator\DataWrapperInterface;
98
use Yiisoft\Validator\Helper\DataSetNormalizer;
109

1110
use function array_key_exists;
@@ -21,7 +20,7 @@
2120
* When using validator, there is no need to wrap your data manually. Array will be automatically wrapped with
2221
* {@see ArrayDataSet} by {@see DataSetNormalizer} during validation.
2322
*/
24-
final class ArrayDataSet implements DataSetInterface
23+
final class ArrayDataSet implements DataWrapperInterface
2524
{
2625
public function __construct(
2726
/**
@@ -56,6 +55,11 @@ public function getData(): array
5655
return $this->data;
5756
}
5857

58+
public function getSource(): array
59+
{
60+
return $this->data;
61+
}
62+
5963
/**
6064
* Whether this data set has the attribute with a given name. Note that this means existence only and attributes
6165
* with empty values are treated as present too.

src/DataSet/ObjectDataSet.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Yiisoft\Validator\AttributeTranslatorInterface;
99
use Yiisoft\Validator\AttributeTranslatorProviderInterface;
1010
use Yiisoft\Validator\DataSetInterface;
11+
use Yiisoft\Validator\DataWrapperInterface;
1112
use Yiisoft\Validator\Helper\ObjectParser;
1213
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider;
1314
use Yiisoft\Validator\RulesProviderInterface;
@@ -142,7 +143,7 @@
142143
*
143144
* @link https://www.php.net/manual/en/language.attributes.overview.php
144145
*/
145-
final class ObjectDataSet implements RulesProviderInterface, DataSetInterface, AttributeTranslatorProviderInterface
146+
final class ObjectDataSet implements RulesProviderInterface, DataWrapperInterface, AttributeTranslatorProviderInterface
146147
{
147148
/**
148149
* @var bool Whether an {@see $object} provided a data set by implementing {@see DataSetInterface}.
@@ -259,12 +260,13 @@ public function hasAttribute(string $attribute): bool
259260
}
260261

261262
/**
262-
* Returns the validated data as a whole.
263+
* Returns the validated data as array.
263264
*
264-
* @return mixed Validated data, has mixed type if it was provided via {@see DataSetInterface::getData()}
265-
* implementation, otherwise it's always an associative array - a mapping between property names and their values.
265+
* @return array|null Result of object {@see DataSetInterface::getData()} method, if it was implemented
266+
* {@see DataSetInterface}, otherwise returns the validated data as an associative array - a mapping between
267+
* property names and their values.
266268
*/
267-
public function getData(): mixed
269+
public function getData(): ?array
268270
{
269271
if ($this->dataSetProvided) {
270272
/** @var DataSetInterface $object */
@@ -275,6 +277,11 @@ public function getData(): mixed
275277
return $this->parser->getData();
276278
}
277279

280+
public function getSource(): object
281+
{
282+
return $this->object;
283+
}
284+
278285
/**
279286
* An optional attribute names translator. It's taken from the {@see $object} when
280287
* {@see AttributeTranslatorProviderInterface} is implemented. In case of it's missing, a `null` value is returned.

src/DataSet/SingleValueDataSet.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Yiisoft\Validator\DataSet;
66

7-
use Yiisoft\Validator\DataSetInterface;
7+
use Yiisoft\Validator\DataWrapperInterface;
88

99
/**
1010
* A data set used for a single value of any (mixed) data type. Does not support attributes.
@@ -26,7 +26,7 @@
2626
*
2727
* For arrays and objects {@see ArrayDataSet} and {@see ObjectDataSet} can be used accordingly.
2828
*/
29-
final class SingleValueDataSet implements DataSetInterface
29+
final class SingleValueDataSet implements DataWrapperInterface
3030
{
3131
public function __construct(
3232
/**
@@ -50,12 +50,14 @@ public function getAttributeValue(string $attribute): mixed
5050
}
5151

5252
/**
53-
* A getter for {@see $data} property. Returns the validated data as a whole. In this case the single value itself
54-
* is returned as a data because it can not be decoupled.
55-
*
56-
* @return mixed Single value of any (mixed) data type.
53+
* Returns the validated data as array.
5754
*/
58-
public function getData(): mixed
55+
public function getData(): ?array
56+
{
57+
return null;
58+
}
59+
60+
public function getSource(): mixed
5961
{
6062
return $this->value;
6163
}

src/DataSetInterface.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ interface DataSetInterface
1919
public function getAttributeValue(string $attribute): mixed;
2020

2121
/**
22-
* Returns the validated data as a whole.
22+
* Returns the validated data as an associative array, where keys are attribute names and values are their
23+
* corresponding values. `null` means that implementation does not support getting an array of attributes.
2324
*
24-
* @return mixed Validated data.
25+
* @return array|null Validated data as array of attributes or `null` when does not support this.
2526
*/
26-
public function getData(): mixed;
27+
public function getData(): ?array;
2728

2829
/**
2930
* Whether a data set has the attribute with a given name. Note that this means existence only and attributes with

src/DataWrapperInterface.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Validator;
6+
7+
/**
8+
* Data wrapper interface provides access to raw data behind a data set.
9+
*
10+
* @internal
11+
*/
12+
interface DataWrapperInterface extends DataSetInterface
13+
{
14+
/**
15+
* Get raw data that is wrapped.
16+
*
17+
* @return mixed Raw data.
18+
*/
19+
public function getSource(): mixed;
20+
}

src/Rule/AtLeastHandler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public function validate(mixed $value, object $rule, ValidationContext $context)
2727
throw new UnexpectedRuleException(AtLeast::class, $rule);
2828
}
2929

30+
/** @var mixed $value */
31+
$value = $context->getParameter(ValidationContext::PARAMETER_VALUE_AS_ARRAY) ?? $value;
32+
3033
$result = new Result();
3134

3235
if (!is_array($value) && !is_object($value)) {

src/Rule/Callback.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ final class Callback implements
3636
use SkipOnErrorTrait;
3737
use WhenTrait;
3838

39-
/**
40-
* @var object|null The object being validated. `null` if PHP attributes aren't used.
41-
*/
42-
private ?object $validatedObject = null;
43-
4439
/**
4540
* @param callable|null $callback Callable with the `function ($value, $rule, $context): Result` signature that
4641
* performs the validation. Mutually exclusive with {@see $method}.
@@ -102,24 +97,8 @@ public function getMethod(): string|null
10297
return $this->method;
10398
}
10499

105-
/**
106-
* Get object being validated.
107-
*
108-
* @return object|null Object being validated. Null if PHP attributes aren't used.
109-
*
110-
* @see $validatedObject
111-
*/
112-
public function getValidatedObject(): ?object
113-
{
114-
return $this->validatedObject;
115-
}
116-
117100
public function afterInitAttribute(object $object, int $target): void
118101
{
119-
if ($target === Attribute::TARGET_CLASS) {
120-
$this->validatedObject = $object;
121-
}
122-
123102
if ($this->method === null) {
124103
return;
125104
}

src/Rule/CallbackHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function validate(mixed $value, object $rule, ValidationContext $context)
3232
throw new InvalidArgumentException('Using method outside of attribute scope is prohibited.');
3333
}
3434

35-
$result = $callback($rule->getValidatedObject() ?? $value, $rule, $context);
35+
$result = $callback($value, $rule, $context);
3636
if (!$result instanceof Result) {
3737
throw new InvalidCallbackReturnTypeException($result);
3838
}

src/Rule/Count.php

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Attribute;
88
use Closure;
99
use Countable;
10-
use Yiisoft\Validator\AfterInitAttributeEventInterface;
1110
use Yiisoft\Validator\LimitInterface;
1211
use Yiisoft\Validator\Rule\Trait\LimitTrait;
1312
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
@@ -32,19 +31,13 @@ final class Count implements
3231
SkipOnErrorInterface,
3332
WhenInterface,
3433
SkipOnEmptyInterface,
35-
LimitInterface,
36-
AfterInitAttributeEventInterface
34+
LimitInterface
3735
{
3836
use LimitTrait;
3937
use SkipOnEmptyTrait;
4038
use SkipOnErrorTrait;
4139
use WhenTrait;
4240

43-
/**
44-
* @var object|null Object being validated.
45-
*/
46-
private ?object $objectValidated = null;
47-
4841
/**
4942
* @param int|null $exactly Exact number of items. `null` means no strict comparison. Mutually exclusive with
5043
* {@see $min} and {@see $max}.
@@ -130,18 +123,6 @@ public function getIncorrectInputMessage(): string
130123
return $this->incorrectInputMessage;
131124
}
132125

133-
/**
134-
* Get object being validated.
135-
*
136-
* @return object|null Object being validated.
137-
*
138-
* @see $objectValidated
139-
*/
140-
public function getObjectValidated(): ?object
141-
{
142-
return $this->objectValidated;
143-
}
144-
145126
public function getOptions(): array
146127
{
147128
return array_merge($this->getLimitOptions(), [
@@ -158,11 +139,4 @@ public function getHandler(): string
158139
{
159140
return CountHandler::class;
160141
}
161-
162-
public function afterInitAttribute(object $object, int $target): void
163-
{
164-
if ($target === Attribute::TARGET_CLASS) {
165-
$this->objectValidated = $object;
166-
}
167-
}
168142
}

src/Rule/CountHandler.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public function validate(mixed $value, object $rule, ValidationContext $context)
3232
$result = new Result();
3333

3434
/** @var mixed $value */
35-
$value = $rule->getObjectValidated() ?? $value;
3635
if (!is_countable($value)) {
3736
$result->addError($rule->getIncorrectInputMessage(), [
3837
'attribute' => $context->getTranslatedAttribute(),

0 commit comments

Comments
 (0)