ryantxr/jsonmodel

一个使用JSON作为模型类的类。

1.0.0 2024-07-03 19:33 UTC

This package is auto-updated.

Last update: 2024-09-03 20:46:25 UTC


README

概述

模型类提供了一个JSON包装器,用于使用点符号和数组语法解析、操作和导出JSON数据。它支持嵌套属性和数组,并对键访问进行验证,以确保方括号内只允许使用数字索引。安装

要使用此类,请确保您已安装PHP 7.4或任何版本的PHP 8。您可以通过Composer将此类包含到您的项目中。Composer配置

通过Composer安装

composer require ryantxr/jsonmodel

用法

类实例化

您可以使用可选的JSON字符串实例化Model类

use Ryantxr\JsonModel\Model;

$jsonInput = '
{
    "foo" : {
        "bar" : {
            "baz": [
                {"a": 100},
                {"b": 200},
                {"c": 300}
            ],
            "fiz" : {
                "x": "xyzzy", 
                "y": "yyzzy",
                "z": "zyzzy"
            },
            "buzz": [
                91, 92, 93, 94, 95
            ]
        }
    }
}';

$model = new Model($jsonInput);

$model = new \Ryantxr\Json\Model($jsonInput);

方法

get(string $var): mixed

使用点符号和数组语法检索属性的值。

示例

$value = $model->get('foo.bar.baz[0].a'); // Returns 100

抛出

InvalidArgumentException if the key format is invalid.

set(string $var, $value): void

使用点符号和数组语法设置属性的值。

示例

$model->set('foo.bar.buzz[1]', '1001');

抛出

InvalidArgumentException if the key format is invalid.

isset(string $var): bool

使用点符号和数组语法检查变量是否存在。

示例

$exists = $model->isset('foo.bar.baz'); // Returns true

export(): string

将当前数据导出为JSON字符串。

示例

$jsonOutput = $model->export();

parse(string $json): void

解析并存储JSON对象。

示例

$model->parse($jsonInput);

parseKeys(string $keyString): array

解析键并确定其类型。验证键以确保方括号内只允许使用数字索引。

抛出

InvalidArgumentException if a key format is invalid.

示例用法

use Ryantxr\JsonModel\Model;

$jsonInput = '
{
    "foo" : {
        "bar" : {
            "baz": [
                {"a": 100},
                {"b": 200},
                {"c": 300}
            ],
            "fiz" : {
                "x": "xyzzy", 
                "y": "yyzzy",
                "z": "zyzzy"
            },
            "buzz": [
                91, 92, 93, 94, 95
            ]
        }
    }
}';

$model = new Model($jsonInput);

echo $model->get('foo.bar.baz[0].a'); // Output: 100
$model->set('foo.bar.buzz[1]', '1001');
echo $model->get('foo.bar.buzz[1]'); // Output: 1001

if ($model->isset('foo.bar')) {
    echo 'Property exists';
}

echo $model->export(); // Output: JSON string

单元测试

可以使用PHPUnit测试Model类。以下是一个示例测试类。

可以使用PHPUnit测试Model类。以下是一个示例测试类。示例测试类

<?php

use PHPUnit\Framework\TestCase;
use Ryantxr\JsonModel\Model;
use InvalidArgumentException;

class ModelTest extends TestCase
{
    private $jsonInput;

    protected function setUp(): void
    {
        $this->jsonInput = '
        {
            "foo" : {
                "bar" : {
                    "baz": [
                        {"a": 100},
                        {"b": 200},
                        {"c": 300}
                    ],
                    "fiz" : {
                        "x": "xyzzy", 
                        "y": "yyzzy",
                        "z": "zyzzy"
                    },
                    "buzz": [
                        91, 92, 93, 94, 95
                    ]
                }
            }
        }';
    }

    public function testGetTopLevelObject()
    {
        $model = new Model($this->jsonInput);
        $this->assertIsObject($model->get('foo'));
    }

    public function testGetNestedObject()
    {
        $model = new Model($this->jsonInput);
        $this->assertIsObject($model->get('foo.bar'));
    }

    public function testGetDeepNestedObject()
    {
        $model = new Model($this->jsonInput);
        $this->assertIsArray($model->get('foo.bar.baz'));
    }

    public function testGetArrayElement()
    {
        $model = new Model($this->jsonInput);
        $this->assertEquals(100, $model->get('foo.bar.baz[0].a'));
    }

    public function testGetNestedValue()
    {
        $model = new Model($this->jsonInput);
        $this->assertEquals('zyzzy', $model->get('foo.bar.fiz.z'));
    }

    public function testSetAndGet()
    {
        $model = new Model();
        $model->set('foo.bar.baz', 'testValue');
        $this->assertEquals('testValue', $model->get('foo.bar.baz'));
    }

    public function testSetAndGetArr()
    {
        $model = new Model();
        $model->set('foo.bar.buzz[1]', '1001');
        $this->assertEquals('1001', $model->get('foo.bar.buzz[1]'));
    }

    public function testIsset()
    {
        $model = new Model($this->jsonInput);
        $this->assertTrue($model->isset('foo.bar'));
        $this->assertFalse($model->isset('foo.bar.nonexistent'));
    }

    public function testExport()
    {
        $model = new Model($this->jsonInput);
        $this->assertJson($model->export());
    }

    public function testInvalidKeyGet()
    {
        $this->expectException(InvalidArgumentException::class);
        $model = new Model($this->jsonInput);
        $model->get('foo.bar.baz[abc]');
    }

    public function testInvalidKeySet()
    {
        $this->expectException(InvalidArgumentException::class);
        $model = new Model($this->jsonInput);
        $model->set('foo.bar.baz[abc]', 999);
    }

    public function testInvalidKeyWithSpecialCharsGet()
    {
        $this->expectException(InvalidArgumentException::class);
        $model = new Model($this->jsonInput);
        $model->get('foo.bar.baz[@#$]');
    }

    public function testInvalidKeyWithSpecialCharsSet()
    {
        $this->expectException(InvalidArgumentException::class);
        $model = new Model($this->jsonInput);
        $model->set('foo.bar.baz[@#$]', 999);
    }

    public function testNonExistentArrayIndexGet()
    {
        $model = new Model($this->jsonInput);
        $this->assertNull($model->get('foo.bar.baz[10]'));
    }

    public function testNonExistentPropertyGet()
    {
        $model = new Model($this->jsonInput);
        $this->assertNull($model->get('foo.bar.nonexistent'));
    }
}

使用PHPUnit运行测试

vendor/bin/phpunit --bootstrap vendor/autoload.php tests/ModelTest.php

许可证

本项目采用MIT许可证。