-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathArrayDataSet.php
More file actions
74 lines (65 loc) · 2.12 KB
/
ArrayDataSet.php
File metadata and controls
74 lines (65 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types=1);
namespace Yiisoft\Validator\DataSet;
use Yiisoft\Validator\DataWrapperInterface;
use Yiisoft\Validator\Helper\DataSetNormalizer;
use function array_key_exists;
/**
* A data set for storing data as an associative array, where keys are property names and values are their
* corresponding values. An example of usage:
*
* ```php
* $dataSet = new ArrayDataSet(['name' => 'John', 'age' => 18]);
* ```
*
* When using validator, there is no need to wrap your data manually. Array will be automatically wrapped with
* {@see ArrayDataSet} by {@see DataSetNormalizer} during validation.
*/
final class ArrayDataSet implements DataWrapperInterface
{
public function __construct(
/**
* @var array A mapping between property names and their values.
*/
private array $data = [],
) {}
/**
* Returns a property value by its name.
*
* Note that in case of non-existing property a default `null` value is returned. If you need to check the presence
* of property or return a different default value, use {@see hasProperty()} instead.
*
* @param string $property Property name.
*
* @return mixed Property value.
*/
public function getPropertyValue(string $property): mixed
{
return $this->data[$property] ?? null;
}
/**
* A getter for {@see $data} property. Returns the validated data as a whole in a form of array.
*
* @return array A mapping between property names and their values.
*/
public function getData(): array
{
return $this->data;
}
public function getSource(): array
{
return $this->data;
}
/**
* Whether this data set has the property with a given name. Note that this means existence only and properties
* with empty values are treated as present too.
*
* @param string $property Property name.
*
* @return bool Whether the property exists: `true` - exists and `false` - otherwise.
*/
public function hasProperty(string $property): bool
{
return array_key_exists($property, $this->data);
}
}