aklump / default-value
根据变量类型计算默认值,包括完全限定的类名。
0.0.6
2024-02-17 23:35 UTC
Requires
- php: >=7.0
Requires (Dev)
- aklump/phpswap: ^0.0.7
- phpunit/phpunit: ^9.6
README
一个小的实用工具,用于根据变量类型或类名返回默认值或实例。
纯PHP
<?php $variable_types = [ 'array', 'bool', 'boolean', 'double', 'float', 'int', 'integer', 'null', 'number', 'object', 'string', // In addition you can pass fully-qualified class names, so long as their // constructors do not REQUIRE any parameters. '\Foo\Bar\Baz', ]; foreach ($variable_types as $variable_type) { $default_value = \AKlump\DefaultValue\DefaultValue::get($variable_type); }
Drupal 8+ 集成
当在Drupal安装中使用时,使用类 \Drupal\Component\Utility\DefaultValue
,除了上述纯PHP支持外,您还将获得特殊的Drupal支持。
<?php $special_drupal_variable_types = [ // This is a service ID. '@current_user', // This class has a ::create method with no required arguments. '\Drupal\user\Entity\User', // This class implements ContainerInjectionInterface. '\Drupal\system\Controller\CsrfTokenController', ]; foreach ($special_drupal_variable_types as $variable_type) { $default_value = \Drupal\Component\Utility\DefaultValue::get($variable_type); }