Recommend this page to a friend! |
Helpers for different operations with PHP data types, variables and containers.
composer require smoren/type-tools
| Method | Description | Code Snippet |
|----------------------------|------------------------------------------------------|---------------------------------------------|
| getString
| Returns unique string of the given variable | UniqueExtractor::getString($var, $strict)
|
| getHash
| Returns unique md5 hash string of the given variable | UniqueExtractor::getHash($var, $strict)
|
| Method | Description | Code Snippet |
|-----------------|---------------------------------------|-------------------------------------------------------------|
| cast
| Cast object to another relative type | ObjectTypeCaster::cast($sourceObject, $destinationClass)
|
| Method | Description | Code Snippet |
|-------------------------------------------------|-------------------------------------------------------------------|------------------------------------------------------------------|
| getPropertyValue
| Returns value of the object property | ObjectAccess::getPropertyValue($object, $propertyName)
|
| setPropertyValue
| Sets value of the object property | ObjectAccess::setPropertyValue($object, $propertyName, $value)
|
| hasReadableProperty
| Returns true if object has readable property by name or by getter | ObjectAccess::hasReadableProperty($object, $propertyName)
|
| hasWritableProperty
| Returns true if object has writable property by name or by getter | ObjectAccess::hasWritableProperty($object, $propertyName)
|
| hasPublicProperty
| Returns true if object has public property | ObjectAccess::hasPublicProperty($object, $propertyName)
|
| hasPublicMethod
| Returns true if object has public method | ObjectAccess::hasPublicMethod($object, $methodName)
|
| hasProperty
| Returns true if object has property | ObjectAccess::hasProperty($object, $propertyName)
|
| hasMethod
| Returns true if object has method | ObjectAccess::hasMethod($object, $methodName)
|
| Method | Description | Code Snippet |
|---------------------|--------------------------------------------------------|---------------------------------------------------|
| get
| Returns value from the container by key | MapAccess::get($container, $key, $defaultValue)
|
| set
| Sets value to the container by key | MapAccess::set($container, $key, $value)
|
| exists
| Returns true if accessible key exists in the container | MapAccess::exists($container, $key)
|
Tool for extracting unique IDs and hashes of any PHP variables and data structures.
Works in two modes: strict and non-strict.
In strict mode: - scalars: unique strictly by type; - objects: unique by instance; - arrays: unique by serialized value; - resources: result is unique by instance.
In non-strict mode: - scalars: unique by value; - objects: unique by serialized value; - arrays: unique by serialized value; - resources: result is unique by instance.
Returns unique string of the given variable.
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
#### Get Hash
Returns unique md5 hash string of the given variable.
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
Tool for casting types of objects.
Cast object to another relative type (upcast or downcast).
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
### Object Access
Tool for reflecting and accessing object properties and methods.
#### Get Property Value
Returns value of the object property.
Can access property by its name or by getter.
Throws Smoren\TypeTools\Exceptions\KeyError
if property is not accessible to read.
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
Sets value of the object property.
Can access property by its name or by setter.
Throws `Smoren\TypeTools\Exceptions\KeyError` if property is not accessible to write.
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]
#### Has Readable Property
Returns true if object has property that is readable by name or by getter.
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
Returns true if object has property that is writable by name or by setter.
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
#### Has Public Property
Returns true if object has public property.
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
Returns true if object has public method.
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
#### Has Property
Returns true if object has property.
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
Returns true if object has method.
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
### Map Access
Tool for map-like accessing of different containers by string keys.
Can access:
- properties of objects (by name or by getter);
- elements of arrays and ArrayAccess objects (by key).
#### Get
Returns value from the container by key or default value if key does not exist or not accessible.
Throws `Smoren\TypeTools\Exceptions\KeyError` if key is not accessible to read.
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
Sets value to the container by key.
Throws `Smoren\TypeTools\Exceptions\KeyError` if key is not accessible to write.
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]
#### Exists
Returns true if accessible key exists in the container.
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 is licensed under the MIT License.
Classes of Smoren Freelight | > | PHP Type Tools | > | Download .zip .tar.gz | > | Support forum | > | Blog (1) | > | Latest changes |
|
Groups | Applications | Files |
Groups |
Utilities and Tools | General purpose tools to simplify software development | View top rated classes |
Libraries | Frameworks and libraries of cooperating classes | View top rated classes |
Data types | Modeling and manipulating data types | View top rated classes |
PHP 7 | Classes using PHP 7 specific features | View top rated classes |
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.
Files |
File | Role | Description | ||
---|---|---|---|---|
.github (1 directory) | ||||
src (4 files, 1 directory) | ||||
tests (3 files, 2 directories) | ||||
.scrutinizer.yml | Data | Auxiliary data | ||
codeception.yml | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpcs.xml | Data | Auxiliary data | ||
phpstan.neon | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files | / | src |
File | Role | Description | ||
---|---|---|---|---|
Exceptions (1 file) | ||||
MapAccess.php | Class | Class source | ||
ObjectAccess.php | Class | Class source | ||
ObjectTypeCaster.php | Class | Class source | ||
UniqueExtractor.php | Class | Class source |
Files | / | tests |
File | Role | Description | ||
---|---|---|---|---|
unit (5 directories) | ||||
_support (1 file) | ||||
coding_standard.xml | Data | Auxiliary data | ||
unit.suite.yml | Data | Auxiliary data | ||
_bootstrap.php | Aux. | Auxiliary script |
Files | / | tests | / | unit |
File | Role | Description | ||
---|---|---|---|---|
Fixtures (5 files) | ||||
MapAccess (3 files) | ||||
ObjectAccess (8 files) | ||||
ObjectTypeCaster (1 file) | ||||
UniqueExtractor (2 files) |
Files | / | tests | / | unit | / | Fixtures |
File | Role | Description |
---|---|---|
ChildClass.php | Class | Class source |
ClassWithAccessibleProperties.php | Class | Class source |
IndependentClass.php | Class | Class source |
ParentClass.php | Class | Class source |
SerializableFixture.php | Class | Class source |
Files | / | tests | / | unit | / | MapAccess |
File | Role | Description |
---|---|---|
ExistsTest.php | Class | Class source |
GetTest.php | Class | Class source |
SetTest.php | Class | Class source |
Files | / | tests | / | unit | / | ObjectAccess |
File | Role | Description |
---|---|---|
GetPropertyValueTest.php | Class | Class source |
HasMethodTest.php | Class | Class source |
HasPropertyTest.php | Class | Class source |
HasPublicMethodTest.php | Class | Class source |
HasPublicPropertyTest.php | Class | Class source |
HasReadablePropertyTest.php | Class | Class source |
HasWritablePropertyTest.php | Class | Class source |
SetPropertyValueTest.php | Class | Class source |
Files | / | tests | / | unit | / | ObjectTypeCaster |
File | Role | Description |
---|---|---|
ObjectTypeCasterTest.php | Class | Class source |
Files | / | tests | / | unit | / | UniqueExtractor |
File | Role | Description |
---|---|---|
NonStrictTest.php | Class | Class source |
StrictTest.php | Class | Class source |
Download all files: type-tools-php.tar.gz type-tools-php.zip NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
|
Files |
File | Role | Description | ||
---|---|---|---|---|
.github (1 directory) | ||||
src (4 files, 1 directory) | ||||
tests (3 files, 2 directories) | ||||
.scrutinizer.yml | Data | Auxiliary data | ||
codeception.yml | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpcs.xml | Data | Auxiliary data | ||
phpstan.neon | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files | / | src |
File | Role | Description | ||
---|---|---|---|---|
Exceptions (1 file) | ||||
MapAccess.php | Class | Class source | ||
ObjectAccess.php | Class | Class source | ||
ObjectTypeCaster.php | Class | Class source | ||
UniqueExtractor.php | Class | Class source |
Files | / | tests |
File | Role | Description | ||
---|---|---|---|---|
unit (5 directories) | ||||
_support (1 file) | ||||
coding_standard.xml | Data | Auxiliary data | ||
unit.suite.yml | Data | Auxiliary data | ||
_bootstrap.php | Aux. | Auxiliary script |
Files | / | tests | / | unit |
File | Role | Description | ||
---|---|---|---|---|
Fixtures (5 files) | ||||
MapAccess (3 files) | ||||
ObjectAccess (8 files) | ||||
ObjectTypeCaster (1 file) | ||||
UniqueExtractor (2 files) |
Files | / | tests | / | unit | / | Fixtures |
File | Role | Description |
---|---|---|
ChildClass.php | Class | Class source |
ClassWithAccessibleProperties.php | Class | Class source |
IndependentClass.php | Class | Class source |
ParentClass.php | Class | Class source |
SerializableFixture.php | Class | Class source |
Files | / | tests | / | unit | / | MapAccess |
File | Role | Description |
---|---|---|
ExistsTest.php | Class | Class source |
GetTest.php | Class | Class source |
SetTest.php | Class | Class source |
Files | / | tests | / | unit | / | ObjectAccess |
File | Role | Description |
---|---|---|
GetPropertyValueTest.php | Class | Class source |
HasMethodTest.php | Class | Class source |
HasPropertyTest.php | Class | Class source |
HasPublicMethodTest.php | Class | Class source |
HasPublicPropertyTest.php | Class | Class source |
HasReadablePropertyTest.php | Class | Class source |
HasWritablePropertyTest.php | Class | Class source |
SetPropertyValueTest.php | Class | Class source |
Files | / | tests | / | unit | / | ObjectTypeCaster |
File | Role | Description |
---|---|---|
ObjectTypeCasterTest.php | Class | Class source |
Files | / | tests | / | unit | / | UniqueExtractor |
File | Role | Description |
---|---|---|
NonStrictTest.php | Class | Class source |
StrictTest.php | Class | Class source |
Download all files: type-tools-php.tar.gz type-tools-php.zip NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
|