Passed
Push — master ( 62cd3f...b14e96 )
by Sergei
02:41
created

SingleValueDataSet::getSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\DataSet;
6
7
use Yiisoft\Validator\DataWrapperInterface;
8
9
/**
10
 * A data set used for a single value of any (mixed) data type. Does not support attributes.
11
 *
12
 * Examples of usage:
13
 *
14 574
 * ```php
15
 * $dataSet = new SingleValueDataSet(5);
16
 * $dataSet = new SingleValueDataSet(2.5);
17
 * $dataSet = new SingleValueDataSet('text');
18 2
 * $dataSet = new SingleValueDataSet(null);
19
 * $dataSet = new SingleValueDataSet(false);
20 2
 * $dataSet = new SingleValueDataSet([]);
21
 * $dataSet = new SingleValueDataSet(new \stdClass());
22
 * ```
23 575
 *
24
 * When using validator, there is no need to wrap data manually. Such types be automatically wrapped with
25 575
 * {@see SingleValueDataSet} by {@see DataSetNormalizer} during validation.
26
 *
27
 * For arrays and objects {@see ArrayDataSet} and {@see ObjectDataSet} can be used accordingly.
28 2
 */
29
final class SingleValueDataSet implements DataWrapperInterface
30 2
{
31
    public function __construct(
32
        /**
33
         * @var mixed Single value of any (mixed) data type.
34
         */
35
        private mixed $value,
36
    ) {
37
    }
38
39
    /**
40
     * Returns an attribute value by its name. {@see SingleValueDataSet} does not support attributes so `null` is always
41
     * returned regardless of the attribute name.
42
     *
43
     * @param string $attribute Attribute name.
44
     *
45
     * @return mixed `null` value indicating that attributes are not supported.
46
     */
47
    public function getAttributeValue(string $attribute): mixed
48
    {
49
        return null;
50
    }
51
52
    /**
53
     * Returns the validated data as array.
54
     */
55
    public function getData(): ?array
56
    {
57
        return null;
58
    }
59
60
    public function getSource(): mixed
61
    {
62
        return $this->value;
63
    }
64
65
    /**
66
     * Whether this data set has the attribute with a given name.
67
     *
68
     * @param string $attribute Attribute name.
69
     *
70
     * @return bool `false` value indicating that attributes are not supported.
71
     */
72
    public function hasAttribute(string $attribute): bool
73
    {
74
        return false;
75
    }
76
}
77