0 follower

Final Class Yiisoft\Validator\DataSet\ArrayDataSet

InheritanceYiisoft\Validator\DataSet\ArrayDataSet
ImplementsYiisoft\Validator\DataWrapperInterface

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:

$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 \Yiisoft\Validator\DataSet\ArrayDataSet} by {@see \Yiisoft\Validator\Helper\DataSetNormalizer} during validation.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Validator\DataSet\ArrayDataSet
getData() A getter for {@see $data} property. Returns the validated data as a whole in a form of array. Yiisoft\Validator\DataSet\ArrayDataSet
getPropertyValue() Returns a property value by its name. Yiisoft\Validator\DataSet\ArrayDataSet
getSource() Yiisoft\Validator\DataSet\ArrayDataSet
hasProperty() 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. Yiisoft\Validator\DataSet\ArrayDataSet

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( array $data = [] )
$data array

                public function __construct(
    /**
     * @var array A mapping between property names and their values.
     */
    private array $data = [],
) {}

            
getData() public method

A getter for {@see $data} property. Returns the validated data as a whole in a form of array.

public array getData ( )
return array

A mapping between property names and their values.

                public function getData(): array
{
    return $this->data;
}

            
getPropertyValue() public method

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 \Yiisoft\Validator\DataSet\hasProperty()} instead.

public mixed getPropertyValue ( string $property )
$property string

Property name.

return mixed

Property value.

                public function getPropertyValue(string $property): mixed
{
    return $this->data[$property] ?? null;
}

            
getSource() public method

public array getSource ( )

                public function getSource(): array
{
    return $this->data;
}

            
hasProperty() public method

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.

public boolean hasProperty ( string $property )
$property string

Property name.

return boolean

Whether the property exists: true - exists and false - otherwise.

                public function hasProperty(string $property): bool
{
    return array_key_exists($property, $this->data);
}