vertigolabs / data-aware
一个库,用于为您的对象提供有用的输入数据处理
Requires
- php: >=8.0.0
- symfony/property-access: ^v7.0.0
Requires (Dev)
- phpunit/phpunit: 11.1.3
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2024-09-16 14:07:08 UTC
README
DataAware提供了一种独特且一致的方法来动态处理数据。
依赖项
- 使用Symfony的PropertyAccess组件。您可以通过Composer安装它,使用
composer require symfony/property-access
。
功能
- 检索特定的数据项或完整的数据集。
- 设置和合并数据到类中。
- 检查数据项是否存在。
- 将默认值应用于缺失的数据。
- 将数据键规范化为驼峰格式。
方法
setData($data, $normalize = true, $applyDefaults = true)
设置类的数据。此方法接受三个参数
参数
$data
: 类要管理的数据。它应为一个数组。
$normalize
(可选): 这是一个布尔参数,表示是否应将$data
数组中的键规范化为驼峰格式。默认值为true
,这意味着除非指定其他方式,否则键将默认进行规范化。
$applyDefaults
(可选): 这也是一个布尔参数。如果设置为true
,则方法将应用默认值,对于$data
数组中未设置的所有键。此参数的默认值也是true
。
此方法在考虑提供的规范化和默认参数的情况下设置类的数据。
mergeData($data, $normalize = true, $applyDefaults = true)
此方法将新数据合并到现有数据集中。
参数
$data
: 要合并到现有数据集中的新数据。它应该是一个数组。
$normalize
(可选): 这是一个布尔参数,指定是否应将$data
数组中的键规范化为驼峰格式。默认值为true
,这意味着除非指定其他方式,否则键将默认进行规范化。
$applyDefaults
(可选): 这也是一个布尔参数。如果设置为true
,则方法将应用默认值,对于$data
数组中未设置的所有键。此参数的默认值也是true
。
将新数据合并到现有数据集中。此方法接受与setData()
相同的参数。
getData($key = null, $default = null)
检索特定、所有或部分处理后的数据。此方法接受一个可选参数
参数
$key
(可选): 这是一个参数,接受要检索的特定数据项的键。如果没有提供或为null,则方法将检索所有数据。
$default
(可选): 这是一个参数,允许您提供默认值,如果指定的键在数据中不存在,则返回此默认值。此参数默认为null
。
getRawData($key = null, $default = null)
检索特定、所有或部分原始(未处理)数据。此方法接受两个可选参数
参数
$key
(可选): 这是一个参数,接受从原始数据集中检索的特定数据项的键。如果没有提供或为null,则方法将检索所有原始数据。
$default
(可选):这是一个允许您提供默认值的参数,如果指定的键在原始数据中不存在,则会返回此默认值。此参数默认为null
。
hasData($key)
检查数据集中是否存在数据项。此方法需要一个参数
参数
$key
:这是您想要检查的具体数据项的键。它应该是字符串值。如果该键存在于处理后的数据集中,则方法返回true
。否则,它返回false
。
hasRawData($key)
此方法检查原始数据集中是否存在数据项。它需要一个参数
参数
$key
:这是一个表示您想要在原始数据集中验证其存在的特定数据项键的字符串值。如果该键存在于原始数据集中,则方法返回true
。否则,它返回false
。
异常
当尝试访问不存在的键且未提供默认值时,会抛出DataNotFoundNoDefaultException
异常。
如何使用
以下是一个如何在类中使用此特性的简单示例
use VertigoLabs\DataAware\DataAwareTrait; use VertigoLabs\DataAware\DataAwareInterface; class EmployeeProfile implements DataAwareInterface { use DataAwareTrait; public function __construct(array $data) { $defaults = [ 'firstName' => 'Not provided', 'lastName' => 'Not provided', 'email' => 'Not provided' ]; // Merging incoming data with defaults $data = array_merge($defaults, $data); $this->setData($data); } public function displayProfile() { foreach ($this->getData() as $key => $value) { echo ucfirst($key) . ": " . $value . "\n"; } } } // Instantiate an EmployeeProfile with some initial data $employee = new EmployeeProfile([ 'firstName' => 'John', 'email' => 'john.doe@example.com' ]); // Display the profile $employee->displayProfile(); // Merge new data into the profile $employee->mergeData([ 'lastName' => 'Doe', 'position' => 'Software Developer', 'address'=>[ 'street'=>'123 Main St', 'city'=>'Anytown', 'state'=>'NY','zip'=>'12345' ] ]); // Display the updated profile $employee->displayProfile(); // Retrieve a specific piece of data echo "First Name: " . $employee->getData('firstName') . "\n"; // Check for the existence of a specific piece of data echo "Does the profile have a position? " . ($employee->hasData('position') ? 'Yes' : 'No') . "\n"; // Use dot notation to retieve data from nested arrays echo "City: " . $employee->getData('address.city') . "\n"; // Retrieve an an array of a subset of data $name = $employee->getData(['firstName', 'lastName']); // Display the subset of data echo "Name: " . $name['firstName'] . " " . $name['lastName'] . "\n"; // Create an array of a subset of data with custom key names $address = $employee->getData([ 'line1' => 'address.street', 'locality' => 'address.city', 'region' => 'address.state', 'postalCode' => 'address.zip' ]); // Display the custom subset of data echo "Address: " . $address['line1'] . "\n"; echo "City: " . $address['locality'] . "\n"; echo "State: " . $address['region'] . "\n"; echo "Zip: " . $address['postalCode'] . "\n";