acdphp / data-guard
从给定的规格和条件下隐藏或屏蔽数组或集合的特定级别元素。
2.0.0
2023-10-12 12:20 UTC
Requires
- php: ^7.4 || ^8.0
Requires (Dev)
- ekino/phpstan-banned-code: ^1.0
- friendsofphp/php-cs-fixer: ^3.34
- nunomaduro/larastan: ^1.0
- orchestra/testbench: ^6.36
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.6
README
从给定的规格和条件下隐藏或屏蔽数组或集合的特定级别元素。
安装
composer require acdphp/data-guard
用法
# Hide (new DataGuard()) ->hide(array $data, string $resource, string $search, string $operator, mixed $value); # Mask (new DataGuard()) ->mask(array $data, string $resource, string $search, string $operator, mixed $value);
集合支持
- 这也可以直接与 illuminate collection 一起使用。
$protectedData = collect(['a' => 1, 'b' => 2]) ->hide('a') ->mask('b'); print_r($protectedData->toArray()); # Result: ['b' => '###'];
资源和搜索指示符
|
- 键分割,同一级别的键匹配。:
- 键分隔符,从根到子键的键层级。[]
- 数组指示符,DataGuard 将查看每个值而不是直接查找下一个键。###
- 隐藏值,在调用mask()
时,数据将被字符串替换而不是删除。
您可以通过将其作为构造函数参数传递来修改指示符。
new DataGuard(':', '|', '[]', '###')
在框架(如laravel)中使用时,您可以发布配置以更改指示符。
php artisan vendor:publish --tag=dataguard-config
数据(数组)
- 您的数据数组(最好是关联数组)
资源(字符串)
- 字符串(示例格式:
'orders[]|order:line_items[]:sku'
) - 这是要处理的数据的关键点。
搜索(字符串,可选)
- 除了直接匹配给定的资源外,您还可以通过将另一个资源(格式与资源相同)作为条件的第一个索引传递,来匹配操作符+值。搜索资源将被搜索和匹配,但处理点仍然在给定的资源上。
- 如果没有提供,将匹配资源的最后一个节点。
操作符(字符串,可选)
1. = : equals
2. != : not equals
3. in : in array
4. !in : not in array
5. > : greater than
6. < : less than
7. <= : less than or equal
8. >= : greater than or equal
9. regex : Regular Expression; condition value must be a proper expression
- 如果没有提供,将使用
=
(等于)
值(混合类型,可选)
- 使用给定的操作符与搜索或资源匹配。
示例
use Cdinopol\DataGuard\DataGuard; $data = [ 'hero' => [ 'name' => 'Thor', 'profile' => [ 'address' => [ 'city' => 'Asgard', 'country' => 'Asgard', ], ], ], 'villain' => [ 'name' => 'Loki', 'profile' => [ 'address' => [ 'city' => 'Asgard', 'country' => 'Asgard', ], ], ], 'others' => [ [ 'name' => 'John', 'profile' => [ 'address' => [ 'city' => 'Asgard', 'country' => 'Asgard', ], ], ], [ 'name' => 'Doe', 'profile' => [ 'address' => [ 'city' => 'New York', 'country' => 'USA', ], ], ], [ 'name' => 'Carl', 'profile' => [ 'address' => [ [ 'city' => 'Chicago', 'country' => 'USA', ], [ 'city' => 'Asgard', 'country' => 'Asgard', ], ], ], ], ], ]; // Hides profile if city = Asgard $protectedData = (new DataGuard()) ->hide($data, 'heroes[]|hero|villain|others[]:profile', 'address|address[]:city', '=', 'Asgard'); print_r($protectedData); # Result: [ 'hero' => [ 'name' => 'Thor', ], 'villain' => [ 'name' => 'Loki', ], 'others' => [ [ 'name' => 'John', ], [ 'name' => 'Doe', 'profile' => [ 'address' => [ 'city' => 'New York', 'country' => 'USA', ], ], ], [ 'name' => 'Carl', ], ], ];
请查看单元测试以获取更多用法示例。
许可证
MIT 许可证(MIT)。请参阅许可证文件以获取更多信息。