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 | 키로 컨테이너에 값을 설정합니다. | 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
이름이나 설정자로 속성에 액세스할 수 있습니다.
쓰기 위해 속성에 액세스할 수 없는 경우 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 Type Tools는 MIT 라이선스에 따라 라이선스가 부여됩니다.