dschoenbauer/dot-notation

对于复杂的数据结构,此库允许通过点符号轻松且安全地访问数据。此库是其他点符号库的汇编,汲取了每个库的最佳特性。

1.2.1 2017-05-03 16:13 UTC

This package is not auto-updated.

Last update: 2024-09-27 23:25:22 UTC


README

Build Status Coverage Status License Downloads Version

License Downloads Version

目的

简化对大型数组结构的访问

安装

    composer require dschoenbauer/dot-notation

测试

    ./vendor/bin/phpunit tests

示例

use DSchoenbauer\DotNotation\ArrayDotNotation;

$mongoConnection = [ 
    'mongo' => [ 
        'default' => [  'user' => 'username', 'password' => 's3cr3t' ]
    ]
];
$config = new ArrayDotNotation($mongoConnection);
        --- or ---
$config = ArrayDotNotation::with($mongoConnection);

GET

// Get plain value
$user = $config->get('mongo.default.user');
/*
    $user = 'username';
*/ 

// Get array value
$mongoDefault = $config->get('mongo.default'); 
/* 
    $mongoDefault = ['user' => 'username', 'password' => 's3cr3t'];
*/

SET

$configDump = $config->set('mongo.numbers', [2, 3, 5, 7, 11])->getData();
/*
    $configDump = [
        'mongo' => [
            'default' => [  'user' => 'username', 'password' => 's3cr3t' ],
            'numbers' => [2, 3, 5, 7, 11]
        ],
        'title' => 'Dot Notation'
    ];
*/

MERGE

$configDump = $config->merge('mongo.default', ['user' => 'otherUser','active' => true])->getData();
/*
    $configDump = [
        'mongo' => [
           'default' => [  'user' => 'otherUser', 'password' => 's3cr3t','active' => true ]
        ],
        'title' => 'Dot Notation'
    ];
*/

REMOVE

$configDump = $config->remove('mongo.default.user')->getData();
/*
    $configDump = [
        'mongo' => [
           'default' => [  'password' => 's3cr3t' ]
        ],
        'title' => 'Dot Notation'
    ];
*/

符号类型

// Tired of dots? Change it.
$user = $config->setNotationType(',')->get('mongo,default,user');
/*
    $user = 'username';
*/ 

WITH

//Functional fluent fun
$user = ArrayDotNotation::with($mongoConnection)->get('mongo.default.user');
/*
    $user = 'username';
*/ 

HAS

// Validates that the dot notation path is present in the data.
$isPresent = $config->has('mongo,default,user');
/*
    $isPresent = true;
*/