michael-rubel/nullify
最新稳定版本:1.0.3
Composer 安装命令:
composer require michael-rubel/nullify
包简介
Convert empty data of any type to null.
关键字:
README 文档
README
Nullify
A plain PHP class to convert empty data of any type to null
PHP ^8.0 is required to use this class.
Installation
composer require michael-rubel/nullify
Usage
use MichaelRubel\Nullify\Nullify; Nullify::the($value);
- Note: the class checks also nested iterables and ArrayAccess objects.
Examples
$value = null; Nullify::the($value); // null $value = ''; Nullify::the($value); // null $value = []; Nullify::the($value); // null $value = (object) []; Nullify::the($value); // null $value = new \stdClass; Nullify::the($value); // null
⚡ Check nested elements:
$values = new Collection([ 'valid' => true, 'empty_array' => [], 'empty_string' => '', 'collection' => new Collection([ 'invalid' => new \stdClass, ]) ]); Nullify::the($values); // Illuminate\Support\Collection^ {#459 // #items: array:4 [ // "valid" => true // "empty_array" => null // "empty_string" => null // "collection" => Illuminate\Support\Collection^ {#461 // #items: array:1 [ // "invalid" => null // ] // #escapeWhenCastingToString: false // } // ] // #escapeWhenCastingToString: false // }
📚 If you use Laravel Collections, you can make a macro:
Collection::macro('nullify', function () { return $this->map(fn ($value) => Nullify::the($value)); }); collect(['', [], (object) [], new \stdClass, '✔']) ->nullify() ->toArray(); // [0 => null, 1 => null, 2 => null, 3 => null, 4 => '✔']
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 265
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-30
