pecee / dot-notation
为PHP提供类似JSON语法的点表示法,以便轻松创建和映射数组。
2.2.0
2017-12-17 02:36 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2024-08-25 12:00:55 UTC
README
为PHP提供类似JSON语法的点表示法,以便轻松创建和映射数组。
安装
在终端中运行以下命令,将pecee/dot-notation库的最新版本添加到您的项目中。
composer require pecee/dot-notation
示例
此示例从基本点格式化的字符串返回一个数组,类似于JSON语法
$d = new \Pecee\DotNotation(['one' => ['two' => 2]]); $d->set('one.two', 3); echo $d->getValues();
输出
array ('one' => ['two' => 3]);
轻松映射一个数组到另一个数组
class CustomerMapper { protected $customer; public function __construct(array $values) { // Map fields (from -> to) $this->customer = $this->map($values, [ 'customerId' => 'id', 'relationshipStatus' => 'relationship_status', 'customerGender' => 'gender', 'meta.name' => 'name', ]); } protected function map(array $input, array $mapping) { $output = array(); foreach($mapping as $before => $after) { $map = new DotNotation($input); $value = $map->get($before); $map = new DotNotation($output); $map->set($after, $value); $output = $map->getValues(); } return $output; } protected function getCustomer() { return $this->customer; } } // Example: $mapper = new CustomerMapper([ 'customerId' => 123456, 'relationshipStatus' => 'single', 'customerGender' => 'male', 'meta' => ['name' => 'Peter'] ]); $customer = $mapper->getCustomer();
输出
array( 'id' => 123456, 'relationship_status' => 'single', 'gender' => 'male', 'name' => 'Peter', );
鸣谢
elfet
MIT许可(MIT)
版权所有 (c) 2016 Simon Sessingø / pecee
在此特此授予任何获得此软件及其相关文档副本(“软件”)的人免费权利,在不受限制的情况下处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件的副本,并允许向提供软件的人提供这样做,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或实质性部分中。
软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途的适用性和非侵权性保证。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他责任负责,无论这些责任是基于合同、侵权或其他方式,无论这些责任是否源于、因之产生或与此软件的使用或其他方式有关。