PHP 数据类型、变量和容器的不同操作的帮助程序。
composer require smoren/type-tools
方法 | 描述 | 代码片段 |
---|---|---|
getString | 返回给定变量的唯一字符串 | UniqueExtractor::getString($var, $strict) |
getHash | 返回给定变量的唯一 md5 哈希字符串 | UniqueExtractor::getHash($var, $strict) |
方法 | 描述 | 代码片段 |
---|---|---|
cast | 将对象转换为另一个相对类型 | ObjectTypeCaster::cast($sourceObject, $destinationClass) |
方法 | 描述 | 代码片段 |
---|---|---|
getPropertyValue | 返回对象属性的值 | ObjectAccess::getPropertyValue($object, $propertyName) |
setPropertyValue | 设置对象属性的值 | ObjectAccess::setPropertyValue($object, $propertyName, $value) |
hasReadableProperty | 如果对象具有按名称或 getter 可读的属性,则返回 true | ObjectAccess::hasReadableProperty($object, $propertyName) |
hasWritableProperty | 如果对象通过名称或 getter 具有可写属性,则返回 true | ObjectAccess::hasWritableProperty($object, $propertyName) |
hasPublicProperty | 如果对象具有公共属性,则返回 true | ObjectAccess::hasPublicProperty($object, $propertyName) |
hasPublicMethod | 如果对象有公共方法则返回 true | ObjectAccess::hasPublicMethod($object, $methodName) |
hasProperty | 如果对象具有属性,则返回 true | ObjectAccess::hasProperty($object, $propertyName) |
hasMethod | 如果对象有方法则返回 true | ObjectAccess::hasMethod($object, $methodName) |
方法 | 描述 | 代码片段 |
---|---|---|
get | 通过键从容器返回值 | MapAccess::get($container, $key, $defaultValue) |
set | 通过 key 设置容器的值 | MapAccess::set($container, $key, $value) |
exists | 如果容器中存在可访问密钥,则返回 true | MapAccess::exists($container, $key) |
用于提取任何 PHP 变量和数据结构的唯一 ID 和哈希值的工具。
以两种模式工作:严格模式和非严格模式。
在严格模式下:
在非严格模式下:
返回给定变量的唯一字符串。
UniqueExtractor::getString(mixed $var, bool $strict): string
use Smoren TypeTools UniqueExtractor ;
$ intValue = 5 ;
$ floatValue = 5.0 ;
$ intValueStrictUniqueId = UniqueExtractor:: getString ( $ intValue , true );
$ floatValueStrictUniqueId = UniqueExtractor:: getString ( $ floatValue , true );
var_dump ( $ intValueStrictUniqueId === $ floatValueStrictUniqueId );
// false
$ intValueNonStrictUniqueId = UniqueExtractor:: getString ( $ intValue , false );
$ floatValueNonStrictUniqueId = UniqueExtractor:: getString ( $ floatValue , false );
var_dump ( $ intValueNonStrictUniqueId === $ floatValueNonStrictUniqueId );
// true
返回给定变量的唯一 md5 哈希字符串。
UniqueExtractor::getHash(mixed $var, bool $strict): string
use Smoren TypeTools UniqueExtractor ;
$ intValue = 5 ;
$ floatValue = 5.0 ;
$ intValueStrictHash = UniqueExtractor:: getHash ( $ intValue , true );
$ floatValueStrictHash = UniqueExtractor:: getHash ( $ floatValue , true );
var_dump ( $ intValueStrictHash === $ floatValueStrictHash );
// false
$ intValueNonStrictHash = UniqueExtractor:: getHash ( $ intValue , false );
$ floatValueNonStrictHash = UniqueExtractor:: getHash ( $ floatValue , false );
var_dump ( $ intValueNonStrictHash === $ floatValueNonStrictHash );
// true
用于铸造对象类型的工具。
将对象转换为另一种相对类型(向上转换或向下转换)。
ObjectTypeCaster::cast(object $sourceObject, string $destinationClass): mixed
use Smoren TypeTools ObjectTypeCaster ;
class ParentClass
{
public int $ a ;
protected int $ b ;
public function __construct ( int $ a , int $ b )
{
$ this -> a = $ a ;
$ this -> b = $ b ;
}
public function toArray (): array
{
return [ $ this -> a , $ this -> b ];
}
}
class ChildClass extends ParentClass
{
private $ c = null ;
public function __construct ( int $ a , int $ b , int $ c )
{
parent :: __construct ( $ a , $ b );
$ this -> c = $ c ;
}
public function toArray (): array
{
return [ $ this -> a , $ this -> b , $ this -> c ];
}
}
/* Downcast */
$ parentClassObject = new ParentClass ( 1 , 2 );
print_r ( $ parentClassObject -> toArray ());
// [1, 2]
$ castedToChildClass = ObjectTypeCaster:: cast ( $ parentClassObject , ChildClass::class);
print_r ( $ castedToChildClass -> toArray ());
// [1, 2, null]
var_dump ( get_class ( $ castedToChildClass ));
// ChildClass
/* Upcast */
$ childClassObject = new ChildClass ( 1 , 2 , 3 );
print_r ( $ childClassObject -> toArray ());
// [1, 2, 3]
$ castedToParentClass = ObjectTypeCaster:: cast ( $ childClassObject , ParentClass::class);
print_r ( $ castedToParentClass -> toArray ());
// [1, 2]
var_dump ( get_class ( $ castedToParentClass ));
// ParentClass
用于反映和访问对象属性和方法的工具。
返回对象属性的值。
ObjectAccess::getPropertyValue(object $object, string $propertyName): mixed
可以通过名称或 getter 来访问属性。
如果属性不可读取,则抛出SmorenTypeToolsExceptionsKeyError
。
use Smoren TypeTools ObjectAccess ;
class MyClass {
public int $ publicProperty = 1 ;
private int $ privateProperty = 2 ;
public function getPrivateProperty (): int
{
return $ this -> privateProperty ;
}
}
$ myObject = new MyClass ();
// Getting by name:
var_dump (ObjectAccess:: getPropertyValue ( $ myObject , ' publicProperty ' ));
// 1
// Getting by getter (getPrivateProperty()):
var_dump (ObjectAccess:: getPropertyValue ( $ myObject , ' privateProperty ' ));
// 2
设置对象属性的值。
ObjectAccess::setPropertyValue(object $object, string $propertyName, mixed $value): void
可以通过名称或 setter 访问属性。
如果属性不可写入,则抛出SmorenTypeToolsExceptionsKeyError
。
use Smoren TypeTools ObjectAccess ;
class MyClass {
public int $ publicProperty = 1 ;
private int $ privateProperty = 2 ;
public function setPrivateProperty ( int $ value ): void
{
$ this -> privateProperty = $ value ;
}
public function toArray (): array
{
return [ $ this -> publicProperty , $ this -> privateProperty ];
}
}
$ myObject = new MyClass ();
// Setting by name:
ObjectAccess:: setPropertyValue ( $ myObject , ' publicProperty ' , 11 );
// Setting by setter (setPrivateProperty()):
ObjectAccess:: getPropertyValue ( $ myObject , ' privateProperty ' , 22 );
print_r ( $ myObject -> toArray ());
// [11, 22]
如果对象具有可通过名称或 getter 读取的属性,则返回 true。
ObjectAccess::hasReadableProperty(object $object, string $propertyName): bool
use Smoren TypeTools ObjectAccess ;
class MyClass {
public int $ publicProperty = 1 ;
private int $ privateProperty = 2 ;
private int $ notAccessibleProperty = 3 ;
public function getPrivateProperty (): int
{
return $ this -> privateProperty ;
}
}
$ myObject = new MyClass ();
// Accessible by name:
var_dump (ObjectAccess:: hasReadableProperty ( $ myObject , ' publicProperty ' ));
// true
// Accessible by getter:
var_dump (ObjectAccess:: hasReadableProperty ( $ myObject , ' privateProperty ' ));
// true
// Not accessible:
var_dump (ObjectAccess:: hasReadableProperty ( $ myObject , ' notAccessibleProperty ' ));
// false
如果对象具有可按名称或 setter 写入的属性,则返回 true。
ObjectAccess::hasWritableProperty(object $object, string $propertyName): bool
use Smoren TypeTools ObjectAccess ;
class MyClass {
public int $ publicProperty = 1 ;
private int $ privateProperty = 2 ;
private int $ notAccessibleProperty = 3 ;
public function setPrivateProperty ( int $ value ): void
{
$ this -> privateProperty = $ value ;
}
}
$ myObject = new MyClass ();
// Accessible by name:
var_dump (ObjectAccess:: hasWritableProperty ( $ myObject , ' publicProperty ' ));
// true
// Accessible by setter:
var_dump (ObjectAccess:: hasWritableProperty ( $ myObject , ' privateProperty ' ));
// true
// Not accessible:
var_dump (ObjectAccess:: hasWritableProperty ( $ myObject , ' notAccessibleProperty ' ));
// false
如果对象具有公共属性,则返回 true。
ObjectAccess::hasPublicProperty(object $object, string $propertyName): bool
use Smoren TypeTools ObjectAccess ;
class MyClass {
public int $ publicProperty = 1 ;
private int $ privateProperty = 2 ;
}
$ myObject = new MyClass ();
var_dump (ObjectAccess:: hasPublicProperty ( $ myObject , ' publicProperty ' ));
// true
var_dump (ObjectAccess:: hasPublicProperty ( $ myObject , ' privateProperty ' ));
// false
如果对象具有公共方法,则返回 true。
ObjectAccess::hasPublicMethod(object $object, string $methodName): bool
use Smoren TypeTools ObjectAccess ;
class MyClass {
public function publicMethod (): int
{
return 1 ;
}
private function privateMethod (): int
{
return 2 ;
}
}
$ myObject = new MyClass ();
var_dump (ObjectAccess:: hasPublicMethod ( $ myObject , ' publicMethod ' ));
// true
var_dump (ObjectAccess:: hasPublicMethod ( $ myObject , ' privateMethod ' ));
// false
如果对象具有属性,则返回 true。
ObjectAccess::hasProperty(object $object, string $propertyName): bool
use Smoren TypeTools ObjectAccess ;
class MyClass {
public int $ publicProperty = 1 ;
private int $ privateProperty = 2 ;
}
$ myObject = new MyClass ();
var_dump (ObjectAccess:: hasProperty ( $ myObject , ' publicProperty ' ));
// true
var_dump (ObjectAccess:: hasProperty ( $ myObject , ' privateProperty ' ));
// true
var_dump (ObjectAccess:: hasProperty ( $ myObject , ' anotherProperty ' ));
// false
如果对象有方法则返回 true。
ObjectAccess::hasMethod(object $object, string $methodName): bool
use Smoren TypeTools ObjectAccess ;
class MyClass {
public function publicMethod (): int
{
return 1 ;
}
private function privateMethod (): int
{
return 2 ;
}
}
$ myObject = new MyClass ();
var_dump (ObjectAccess:: hasMethod ( $ myObject , ' publicMethod ' ));
// true
var_dump (ObjectAccess:: hasMethod ( $ myObject , ' privateMethod ' ));
// true
用于通过字符串键对不同容器进行类似映射访问的工具。
可以访问:
如果键不存在或不可访问,则按键或默认值从容器返回值。
如果密钥无法读取,则抛出SmorenTypeToolsExceptionsKeyError
。
MapAccess::get(mixed $container, string $key, mixed $defaultValue = null): mixed
use Smoren TypeTools MapAccess ;
$ array = [
' a ' => 1 ,
];
var_dump (MapAccess:: get ( $ array , ' a ' , 0 ));
// 1
var_dump (MapAccess:: get ( $ array , ' b ' , 0 ));
// 0
var_dump (MapAccess:: get ( $ array , ' b ' ));
// null
class MyClass {
public int $ publicProperty = 1 ;
private int $ privateProperty = 2 ;
private int $ notAccessibleProperty = 3 ;
public function getPrivateProperty (): int
{
return $ this -> privateProperty ;
}
}
$ myObject = new MyClass ();
// Accessible by name:
var_dump (MapAccess:: get ( $ myObject , ' publicProperty ' , 0 ));
// 1
// Accessible by getter:
var_dump (MapAccess:: get ( $ myObject , ' privateProperty ' ));
// 2
// Not accessible:
var_dump (MapAccess:: get ( $ myObject , ' notAccessibleProperty ' , - 1 ));
// -1
var_dump (MapAccess:: get ( $ myObject , ' notAccessibleProperty ' ));
// null
// Nonexistent:
var_dump (MapAccess:: get ( $ myObject , ' nonexistentProperty ' , - 1 ));
// -1
var_dump (MapAccess:: get ( $ myObject , ' nonexistentProperty ' ));
// null
通过键为容器设置值。
MapAccess::set(mixed $container, string $key, mixed $value): void
如果密钥无法写入,则抛出SmorenTypeToolsExceptionsKeyError
。
use Smoren TypeTools MapAccess ;
$ array = [
' a ' => 1 ,
];
MapAccess:: set ( $ array , ' a ' , 11 );
MapAccess:: set ( $ array , ' b ' , 22 );
print_r ( $ array );
// ['a' => 11, 'b' => 22]
class MyClass {
public int $ publicProperty = 1 ;
private int $ privateProperty = 2 ;
public function setPrivateProperty ( int $ value ): void
{
$ this -> privateProperty = $ value ;
}
public function toArray (): array
{
return [ $ this -> publicProperty , $ this -> privateProperty ];
}
}
$ myObject = new MyClass ();
// Accessible by name:
MapAccess:: get ( $ myObject , ' publicProperty ' , 11 );
// Accessible by getter:
MapAccess:: get ( $ myObject , ' privateProperty ' , 22 );
print_r ( $ myObject -> toArray ());
// [11, 22]
如果容器中存在可访问密钥,则返回 true。
MapAccess::exists(mixed $container, string $key): bool
use Smoren TypeTools MapAccess ;
$ array = [
' a ' => 1 ,
];
var_dump (MapAccess:: exists ( $ array , ' a ' ));
// true
var_dump (MapAccess:: exists ( $ array , ' b ' ));
// false
class MyClass {
public int $ publicProperty = 1 ;
private int $ privateProperty = 2 ;
private int $ notAccessibleProperty = 3 ;
public function getPrivateProperty (): int
{
return $ this -> privateProperty ;
}
}
$ myObject = new MyClass ();
// Accessible by name:
var_dump (MapAccess:: exists ( $ myObject , ' publicProperty ' ));
// true
// Accessible by getter:
var_dump (MapAccess:: exists ( $ myObject , ' privateProperty ' ));
// true
// Not accessible:
var_dump (MapAccess:: get ( $ myObject , ' notAccessibleProperty ' ));
// false
// Nonexistent:
var_dump (MapAccess:: get ( $ myObject , ' nonexistentProperty ' , - 1 ));
// false
composer install
composer test-init
composer test
PHP 类型工具根据 MIT 许可证获得许可。