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 許可證獲得許可。