uuur86/strobj

该仓库允许您通过字符串访问对象。

v2.1.9 2024-01-05 13:47 UTC

README

Duplicated Lines (%) Maintainability Rating Reliability Rating Security Rating Quality Gate Status Vulnerabilities Bugs Technical Debt

PHP 字符串对象是一个库,它提供了一个简单直观的界面来处理 PHP 数组和对象。内置验证和过滤功能,使得访问、操作和验证数据更加容易,节省您的时间和精力。

  • 允许通过字符串访问对象
  • 允许使用预定义或自定义验证规则检查对象的值是否有效
  • 提供中间件功能来设置内存限制或其他配置
  • 提供数据过滤器来操作对象的值
  • 可以用来以简化的方式设置或获取对象和数组的值

安装

要安装此库,请运行以下 Composer 命令

composer require uuur86/strobj

用法

要开始使用 PHP 字符串对象,请在您的 PHP 文件顶部包含以下代码

use StrObj\StringObjects;
require('vendor/autoload.php');

基本用法

以下是一个示例,展示如何使用 PHP 字符串对象访问和操作 JSON 字符串中的数据

use StrObj\StringObjects;

require('vendor/autoload.php');

// String JSON data to be used
// or you can use an object/array
$persons = '{
    "persons": [
        {
            "name": "John Doe",
            "age": "twelve"
        },
        {
            "name": "Molly Doe",
            "age": "14"
        },
        {
            "name": "Lorem Doe",
            "age": "34"
        },
        {
            "name": "Ipsum Doe",
            "age": "21"
        }
    ]
}';

$test = StringObjects::instance(
    $persons,
    [
        'validation' => [
            'patterns' => [
                // Add a new pattern named 'age' which only accepts numbers
                'age' => '#^[0-9]+$#siu',
                // Add a new pattern named 'name' which only accepts letters and spaces
                'name' => '#^[a-zA-Z ]+$#siu',
            ],
            'rules' => [
                // first rule
                [
                    // path scope to be checked
                    'path' => 'persons/*/age',
                    // uses 'age' pattern
                    'pattern' => 'age',
                    // makes it required
                    'required' => true
                ],
                // second rule
                [
                    'path' => 'persons/*/name',
                    'pattern' => 'name',
                    'required' => true
                ],
            ],
        ],
        'middleware' => [
            // Sets memory limit to 3MB
            'memory_limit' => 1024 * 1024 * 3,
        ],
        // Output data filters
        'filters' => [
            // Filters all persons/*/age values
            'persons/*/age' => [
                // converts to integer
                'type' => 'int',
                // only accepts values greater than 10
                'callback' => function ($value) {
                    return $value > 10;
                }
            ],
            'persons/*/name' => [
                // converts to string (not necessary)
                'type' => 'string',
                // only accepts values which contains only letters and spaces
                'callback' => function ($value) {
                    return preg_match('#^[a-zA-Z ]+$#siu', $value);
                }
            ],
        ],
    ]
);

// False
var_dump($test->isValid('persons/0/age'));

// True
var_dump($test->isValid('persons/1/age'));

// False
var_dump($test->isValid('persons/*/age'));

// False
var_dump($test->isValid('persons'));

// Updates value of persons/0/name
$test->set('persons/0/name', 'John D.');

// Updates value of persons/0/age
$test->set('persons/0/age', 12);

// Adds a new person named "Neo Doe" with age 199
$test->set('persons/4/name', 'Neo Doe');
$test->set('persons/4/age', 199);

// Outputs "John D."
$test->get('persons/0/name');

// Outputs "12"
$test->get('persons/3/age');

// Outputs "Neo Doe"
$test->get('persons/4/name');

// Outputs "199"
$test->get('persons/4/age');

// Updates value of persons/4/age to "200"
$test->set('persons/4/age', 200);

// Outputs "200"
$test->get('persons/4/age');

开发

测试

composer test

或者

php vendor/bin/phpunit tests/TestScenarios

许可证

GPL-2.0-or-later

作者

Uğur Biçer - @uuur86

贡献

如果您想为此项目做出贡献,您可以发送拉取请求。我们期望所有贡献者遵守我们的行为准则

联系

您可以通过电子邮件联系我: contact@codeplus.dev

错误

您可以通过 GitHub 问题报告错误。

安全

如果您发现安全漏洞,请通过电子邮件报告: contact@codeplus.dev

捐赠

如果您想支持我,您可以通过 GitHub 赞助者进行捐赠: https://github.com/sponsors/uuur86

参见