web-fu / php-dot-notation
一个库,允许使用点表示法访问数组和对像,并支持强类型
v1.6.0
2024-09-19 21:02 UTC
Requires
- php: 8.0.* || 8.1.* || 8.2.* || 8.3.*
- web-fu/reflection: ^2.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^5.4
README
一个库,允许使用点表示法访问对象和数组
此库允许使用点表示法访问对象和数组。
它还允许使用点表示法获取、设置和创建对象和数组。
安装
composer require web-fu/php-dot-notation
获取和设置值
$array = [ 'foo' => [ 'bar' => 'test', ], ]; // Accessing an array $dot = new Dot($array); echo $dot->get('foo.bar'); //test // Setting a value in an array $dot->set('foo.bar', 'baz'); echo $array['foo']['bar']; //baz $class = new class() { public string $property = 'test'; public function method(): string { return 'foo'; } } // Accessing an object $dot = new Dot($class); echo $dot->get('property'); //test echo $dot->get('method()'); //foo // Setting a value in an object $dot->set('property', 'baz'); echo $class->property; //baz
初始化、创建和取消设置路径
$class = new class() { public string $property; } $dot = new Dot($class); $dot->init('property'); var_dump($class->property); //string[0] "" $array = []; $dot = new Dot($array); $dot->create('foo.bar'); var_dump(['foo']['bar']); //NULL // Unsetting a value in an array or an object $test = new class { public array $array = [ 'foo' => 'bar', ]; }; $dot = new Dot($test); $dot->unset('array.foo'); var_dump(array_key_exists('foo', $test->array)); // false
注意:`init` 方法在尝试初始化之前会检查路径是否存在。
`create` 方法如果路径不存在,则尽可能创建路径。
从点和到点表示法的转换
// Turning an object or an array into the dotified version of it $array = [ 'foo' => [ 'bar' => 'test', ], ]; $dotified = Dot::dotify($array); echo $dotified['foo.bar']; //test // Turning a dotified array into a normal array $normal = Dot::undotify($dotified); echo $normal['foo']['bar']; //test
反射支持
反射支持由我的反射库提供:https://github.com/web-fu/reflection
$class = new class() { public string $property = 'test'; public function method(): string { return 'foo'; } }; $dot = new Dot($class); $propertyReflectionType = $dot->getReflectionType('property')->getTypeNames(); // ['string'] $methodReturnReflectionType = $dot->getReflectionType('method()')->getTypeNames(); // ['string'] $array = [ 'foo' => [ 'bar' => 'test', ], ]; $dot = new Dot($array); $indexReflectionType = $dot->getReflectionType('foo.bar')->getTypeNames(); // ['string']
请参阅 /examples 文件夹中的完整示例
局限性
此工具有一些局限性
获取方法将执行该方法
$class = new class() { public function iDoSomething(): int { echo 'I Do Something '; return 0; } } $dot = new Dot($class); echo $dot->get('iDoSomething'); // I Do Something 0
无法访问私有或受保护的属性
$class = new class() { private string $property = 'test'; } $dot = new Dot($class); echo $dot->get('property'); //Unhandled Exception: WebFu\DotNotation\Exception\PathNotFoundException Path `property` not found
无法判断方法是否返回 NULL 或根本不返回
$class = new class() { public function thisMethodReturnsNull(): int|null { return null; } public function thisMethodDoesNotReturn(): void { //do something } } $dot = new Dot($class); var_dump($dot->get('thisMethodReturnsNull')); //NULL var_dump($dot->get('thisMethodDoesNotReturn')); //NULL
无法设置方法
$class = new class() { public function method(): int { return 0; } } $dot = new Dot($class); $dot->set('method()', 20); //Unhandled Exception: WebFu\Proxy\UnsupportedOperationException Cannot set a class method