{"id":21874515,"url":"https://github.com/smoren/type-tools-php","last_synced_at":"2025-04-15T01:23:52.497Z","repository":{"id":65182705,"uuid":"586086233","full_name":"Smoren/type-tools-php","owner":"Smoren","description":"Helpers for different operations with PHP data types, variables and containers","archived":false,"fork":false,"pushed_at":"2024-12-18T21:27:31.000Z","size":68,"stargazers_count":7,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-22T22:39:44.459Z","etag":null,"topics":["accessor","data-types","helpers","php","php-library","reflection","type-casting"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Smoren.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-06T22:48:14.000Z","updated_at":"2024-12-18T21:27:34.000Z","dependencies_parsed_at":"2024-12-18T22:25:05.517Z","dependency_job_id":"bf5c62c3-ad88-471e-aacb-004a2508765e","html_url":"https://github.com/Smoren/type-tools-php","commit_stats":{"total_commits":21,"total_committers":2,"mean_commits":10.5,"dds":0.09523809523809523,"last_synced_commit":"fb202fae3be4a8bc4c803befd7cbb1a54d83c64b"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Ftype-tools-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Ftype-tools-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Ftype-tools-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Ftype-tools-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Smoren","download_url":"https://codeload.github.com/Smoren/type-tools-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248986797,"owners_count":21194122,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["accessor","data-types","helpers","php","php-library","reflection","type-casting"],"created_at":"2024-11-28T07:12:34.514Z","updated_at":"2025-04-15T01:23:52.451Z","avatar_url":"https://github.com/Smoren.png","language":"PHP","readme":"# PHP Type Tools\n\n![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/smoren/type-tools)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Smoren/type-tools-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Smoren/type-tools-php/?branch=master)\n[![Coverage Status](https://coveralls.io/repos/github/Smoren/type-tools-php/badge.svg?branch=master)](https://coveralls.io/github/Smoren/type-tools-php?branch=master)\n![Build and test](https://github.com/Smoren/type-tools-php/actions/workflows/test_master.yml/badge.svg)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nHelpers for different operations with PHP data types, variables and containers.\n\n## How to install to your project\n```\ncomposer require smoren/type-tools\n```\n\n## Quick Reference\n\n### Unique Extractor\n| Method                     | Description                                          | Code Snippet                                |\n|----------------------------|------------------------------------------------------|---------------------------------------------|\n| [`getString`](#Get-String) | Returns unique string of the given variable          | `UniqueExtractor::getString($var, $strict)` |\n| [`getHash`](#Get-Hash)     | Returns unique md5 hash string of the given variable | `UniqueExtractor::getHash($var, $strict)`   |\n\n### Object Type Caster\n| Method          | Description                           | Code Snippet                                                |\n|-----------------|---------------------------------------|-------------------------------------------------------------|\n| [`cast`](#Cast) | Cast object to another relative type  | `ObjectTypeCaster::cast($sourceObject, $destinationClass)`  |\n\n### Object Access\n| Method                                          | Description                                                       | Code Snippet                                                     |\n|-------------------------------------------------|-------------------------------------------------------------------|------------------------------------------------------------------|\n| [`getPropertyValue`](#Get-Property-Value)       | Returns value of the object property                              | `ObjectAccess::getPropertyValue($object, $propertyName)`         |\n| [`setPropertyValue`](#Set-Property-Value)       | Sets value of the object property                                 | `ObjectAccess::setPropertyValue($object, $propertyName, $value)` |\n| [`hasReadableProperty`](#Has-Readable-Property) | Returns true if object has readable property by name or by getter | `ObjectAccess::hasReadableProperty($object, $propertyName)`      |\n| [`hasWritableProperty`](#Has-Writable-Property) | Returns true if object has writable property by name or by getter | `ObjectAccess::hasWritableProperty($object, $propertyName)`      |\n| [`hasPublicProperty`](#Has-Public-Property)     | Returns true if object has public property                        | `ObjectAccess::hasPublicProperty($object, $propertyName)`        |\n| [`hasPublicMethod`](#Has-Public-Method)         | Returns true if object has public method                          | `ObjectAccess::hasPublicMethod($object, $methodName)`            |\n| [`hasProperty`](#Has-Property)                  | Returns true if object has property                               | `ObjectAccess::hasProperty($object, $propertyName)`              |\n| [`hasMethod`](#Has-Method)                      | Returns true if object has method                                 | `ObjectAccess::hasMethod($object, $methodName)`                  |\n\n### Map Access\n| Method              | Description                                            | Code Snippet                                      |\n|---------------------|--------------------------------------------------------|---------------------------------------------------|\n| [`get`](#Get)       | Returns value from the container by key                | `MapAccess::get($container, $key, $defaultValue)` |\n| [`set`](#Set)       | Sets value to the container by key                     | `MapAccess::set($container, $key, $value)`        |\n| [`exists`](#Exists) | Returns true if accessible key exists in the container | `MapAccess::exists($container, $key)`             |\n\n## Usage\n\n### Unique Extractor\n\nTool for extracting unique IDs and hashes of any PHP variables and data structures.\n\nWorks in two modes: strict and non-strict.\n\nIn strict mode:\n- scalars: unique strictly by type;\n- objects: unique by instance;\n- arrays: unique by serialized value;\n- resources: result is unique by instance.\n\nIn non-strict mode:\n- scalars: unique by value;\n- objects: unique by serialized value;\n- arrays: unique by serialized value;\n- resources: result is unique by instance.\n\n#### Get String\n\nReturns unique string of the given variable.\n\n```UniqueExtractor::getString(mixed $var, bool $strict): string```\n\n```php\nuse Smoren\\TypeTools\\UniqueExtractor;\n\n$intValue = 5;\n$floatValue = 5.0;\n\n$intValueStrictUniqueId = UniqueExtractor::getString($intValue, true);\n$floatValueStrictUniqueId = UniqueExtractor::getString($floatValue, true);\n\nvar_dump($intValueStrictUniqueId === $floatValueStrictUniqueId);\n// false\n\n$intValueNonStrictUniqueId = UniqueExtractor::getString($intValue, false);\n$floatValueNonStrictUniqueId = UniqueExtractor::getString($floatValue, false);\n\nvar_dump($intValueNonStrictUniqueId === $floatValueNonStrictUniqueId);\n// true\n```\n\n#### Get Hash\n\nReturns unique md5 hash string of the given variable.\n\n```UniqueExtractor::getHash(mixed $var, bool $strict): string```\n\n```php\nuse Smoren\\TypeTools\\UniqueExtractor;\n\n$intValue = 5;\n$floatValue = 5.0;\n\n$intValueStrictHash = UniqueExtractor::getHash($intValue, true);\n$floatValueStrictHash = UniqueExtractor::getHash($floatValue, true);\n\nvar_dump($intValueStrictHash === $floatValueStrictHash);\n// false\n\n$intValueNonStrictHash = UniqueExtractor::getHash($intValue, false);\n$floatValueNonStrictHash = UniqueExtractor::getHash($floatValue, false);\n\nvar_dump($intValueNonStrictHash === $floatValueNonStrictHash);\n// true\n```\n\n### Object Type Caster\n\nTool for casting types of objects.\n\n#### Cast\n\nCast object to another relative type (upcast or downcast).\n\n```ObjectTypeCaster::cast(object $sourceObject, string $destinationClass): mixed```\n\n```php\nuse Smoren\\TypeTools\\ObjectTypeCaster;\n\nclass ParentClass\n{\n    public int $a;\n    protected int $b;\n\n    public function __construct(int $a, int $b)\n    {\n        $this-\u003ea = $a;\n        $this-\u003eb = $b;\n    }\n\n    public function toArray(): array\n    {\n        return [$this-\u003ea, $this-\u003eb];\n    }\n}\n\nclass ChildClass extends ParentClass\n{\n    private $c = null;\n\n    public function __construct(int $a, int $b, int $c)\n    {\n        parent::__construct($a, $b);\n        $this-\u003ec = $c;\n    }\n\n    public function toArray(): array\n    {\n        return [$this-\u003ea, $this-\u003eb, $this-\u003ec];\n    }\n}\n\n/* Downcast */\n\n$parentClassObject = new ParentClass(1, 2);\nprint_r($parentClassObject-\u003etoArray());\n// [1, 2]\n\n$castedToChildClass = ObjectTypeCaster::cast($parentClassObject, ChildClass::class);\nprint_r($castedToChildClass-\u003etoArray());\n// [1, 2, null]\n\nvar_dump(get_class($castedToChildClass));\n// ChildClass\n\n/* Upcast */\n\n$childClassObject = new ChildClass(1, 2, 3);\nprint_r($childClassObject-\u003etoArray());\n// [1, 2, 3]\n\n$castedToParentClass = ObjectTypeCaster::cast($childClassObject, ParentClass::class);\nprint_r($castedToParentClass-\u003etoArray());\n// [1, 2]\n\nvar_dump(get_class($castedToParentClass));\n// ParentClass\n```\n\n### Object Access\n\nTool for reflecting and accessing object properties and methods.\n\n#### Get Property Value\n\nReturns value of the object property.\n\n```ObjectAccess::getPropertyValue(object $object, string $propertyName): mixed```\n\nCan access property by its name or by getter.\n\nThrows `Smoren\\TypeTools\\Exceptions\\KeyError` if property is not accessible to read.\n\n```php\nuse Smoren\\TypeTools\\ObjectAccess;\n\nclass MyClass {\n    public int $publicProperty = 1;\n    private int $privateProperty = 2;\n    \n    public function getPrivateProperty(): int\n    {\n        return $this-\u003eprivateProperty;\n    }\n}\n\n$myObject = new MyClass();\n\n// Getting by name:\nvar_dump(ObjectAccess::getPropertyValue($myObject, 'publicProperty'));\n// 1\n\n// Getting by getter (getPrivateProperty()):\nvar_dump(ObjectAccess::getPropertyValue($myObject, 'privateProperty'));\n// 2\n```\n\n#### Set Property Value\n\nSets value of the object property.\n\n```ObjectAccess::setPropertyValue(object $object, string $propertyName, mixed $value): void```\n\nCan access property by its name or by setter.\n\nThrows `Smoren\\TypeTools\\Exceptions\\KeyError` if property is not accessible to write.\n\n```php\nuse Smoren\\TypeTools\\ObjectAccess;\n\nclass MyClass {\n    public int $publicProperty = 1;\n    private int $privateProperty = 2;\n    \n    public function setPrivateProperty(int $value): void\n    {\n        $this-\u003eprivateProperty = $value;\n    }\n    \n    public function toArray(): array\n    {\n        return [$this-\u003epublicProperty, $this-\u003eprivateProperty];\n    }\n}\n\n$myObject = new MyClass();\n\n// Setting by name:\nObjectAccess::setPropertyValue($myObject, 'publicProperty', 11);\n\n// Setting by setter (setPrivateProperty()):\nObjectAccess::getPropertyValue($myObject, 'privateProperty', 22);\n\nprint_r($myObject-\u003etoArray());\n// [11, 22]\n\n```\n\n#### Has Readable Property\n\nReturns true if object has property that is readable by name or by getter.\n\n```ObjectAccess::hasReadableProperty(object $object, string $propertyName): bool```\n\n```php\nuse Smoren\\TypeTools\\ObjectAccess;\n\nclass MyClass {\n    public int $publicProperty = 1;\n    private int $privateProperty = 2;\n    private int $notAccessibleProperty = 3;\n    \n    public function getPrivateProperty(): int\n    {\n        return $this-\u003eprivateProperty;\n    }\n}\n\n$myObject = new MyClass();\n\n// Accessible by name:\nvar_dump(ObjectAccess::hasReadableProperty($myObject, 'publicProperty'));\n// true\n\n// Accessible by getter:\nvar_dump(ObjectAccess::hasReadableProperty($myObject, 'privateProperty'));\n// true\n\n// Not accessible:\nvar_dump(ObjectAccess::hasReadableProperty($myObject, 'notAccessibleProperty'));\n// false\n```\n\n#### Has Writable Property\n\nReturns true if object has property that is writable by name or by setter.\n\n```ObjectAccess::hasWritableProperty(object $object, string $propertyName): bool```\n\n```php\nuse Smoren\\TypeTools\\ObjectAccess;\n\nclass MyClass {\n    public int $publicProperty = 1;\n    private int $privateProperty = 2;\n    private int $notAccessibleProperty = 3;\n    \n    public function setPrivateProperty(int $value): void\n    {\n        $this-\u003eprivateProperty = $value;\n    }\n}\n\n$myObject = new MyClass();\n\n// Accessible by name:\nvar_dump(ObjectAccess::hasWritableProperty($myObject, 'publicProperty'));\n// true\n\n// Accessible by setter:\nvar_dump(ObjectAccess::hasWritableProperty($myObject, 'privateProperty'));\n// true\n\n// Not accessible:\nvar_dump(ObjectAccess::hasWritableProperty($myObject, 'notAccessibleProperty'));\n// false\n```\n\n#### Has Public Property\n\nReturns true if object has public property.\n\n```ObjectAccess::hasPublicProperty(object $object, string $propertyName): bool```\n\n```php\nuse Smoren\\TypeTools\\ObjectAccess;\n\nclass MyClass {\n    public int $publicProperty = 1;\n    private int $privateProperty = 2;\n}\n\n$myObject = new MyClass();\n\nvar_dump(ObjectAccess::hasPublicProperty($myObject, 'publicProperty'));\n// true\n\nvar_dump(ObjectAccess::hasPublicProperty($myObject, 'privateProperty'));\n// false\n```\n\n#### Has Public Method\n\nReturns true if object has public method.\n\n```ObjectAccess::hasPublicMethod(object $object, string $methodName): bool```\n\n```php\nuse Smoren\\TypeTools\\ObjectAccess;\n\nclass MyClass {\n    public function publicMethod(): int\n    {\n        return 1;\n    }\n    \n    private function privateMethod(): int\n    {\n        return 2;\n    }\n}\n\n$myObject = new MyClass();\n\nvar_dump(ObjectAccess::hasPublicMethod($myObject, 'publicMethod'));\n// true\n\nvar_dump(ObjectAccess::hasPublicMethod($myObject, 'privateMethod'));\n// false\n```\n\n#### Has Property\n\nReturns true if object has property.\n\n```ObjectAccess::hasProperty(object $object, string $propertyName): bool```\n\n```php\nuse Smoren\\TypeTools\\ObjectAccess;\n\nclass MyClass {\n    public int $publicProperty = 1;\n    private int $privateProperty = 2;\n}\n\n$myObject = new MyClass();\n\nvar_dump(ObjectAccess::hasProperty($myObject, 'publicProperty'));\n// true\n\nvar_dump(ObjectAccess::hasProperty($myObject, 'privateProperty'));\n// true\n\nvar_dump(ObjectAccess::hasProperty($myObject, 'anotherProperty'));\n// false\n```\n\n#### Has Method\n\nReturns true if object has method.\n\n```ObjectAccess::hasMethod(object $object, string $methodName): bool```\n\n```php\nuse Smoren\\TypeTools\\ObjectAccess;\n\nclass MyClass {\n    public function publicMethod(): int\n    {\n        return 1;\n    }\n    \n    private function privateMethod(): int\n    {\n        return 2;\n    }\n}\n\n$myObject = new MyClass();\n\nvar_dump(ObjectAccess::hasMethod($myObject, 'publicMethod'));\n// true\n\nvar_dump(ObjectAccess::hasMethod($myObject, 'privateMethod'));\n// true\n```\n\n### Map Access\n\nTool for map-like accessing of different containers by string keys.\n\nCan access:\n- properties of objects (by name or by getter);\n- elements of arrays and ArrayAccess objects (by key).\n\n#### Get\n\nReturns value from the container by key or default value if key does not exist or not accessible.\n\nThrows `Smoren\\TypeTools\\Exceptions\\KeyError` if key is not accessible to read.\n\n```MapAccess::get(mixed $container, string $key, mixed $defaultValue = null): mixed```\n\n```php\nuse Smoren\\TypeTools\\MapAccess;\n\n$array = [\n    'a' =\u003e 1,\n];\n\nvar_dump(MapAccess::get($array, 'a', 0));\n// 1\n\nvar_dump(MapAccess::get($array, 'b', 0));\n// 0\n\nvar_dump(MapAccess::get($array, 'b'));\n// null\n\nclass MyClass {\n    public int $publicProperty = 1;\n    private int $privateProperty = 2;\n    private int $notAccessibleProperty = 3;\n    \n    public function getPrivateProperty(): int\n    {\n        return $this-\u003eprivateProperty;\n    }\n}\n\n$myObject = new MyClass();\n\n// Accessible by name:\nvar_dump(MapAccess::get($myObject, 'publicProperty', 0));\n// 1\n\n// Accessible by getter:\nvar_dump(MapAccess::get($myObject, 'privateProperty'));\n// 2\n\n// Not accessible:\nvar_dump(MapAccess::get($myObject, 'notAccessibleProperty', -1));\n// -1\n\nvar_dump(MapAccess::get($myObject, 'notAccessibleProperty'));\n// null\n\n// Nonexistent:\nvar_dump(MapAccess::get($myObject, 'nonexistentProperty', -1));\n// -1\n\nvar_dump(MapAccess::get($myObject, 'nonexistentProperty'));\n// null\n```\n\n#### Set\n\nSets value to the container by key.\n\n```MapAccess::set(mixed $container, string $key, mixed $value): void```\n\nThrows `Smoren\\TypeTools\\Exceptions\\KeyError` if key is not accessible to write.\n\n```php\nuse Smoren\\TypeTools\\MapAccess;\n\n$array = [\n    'a' =\u003e 1,\n];\n\nMapAccess::set($array, 'a', 11);\nMapAccess::set($array, 'b', 22);\n\nprint_r($array);\n// ['a' =\u003e 11, 'b' =\u003e 22]\n\nclass MyClass {\n    public int $publicProperty = 1;\n    private int $privateProperty = 2;\n    \n    public function setPrivateProperty(int $value): void\n    {\n        $this-\u003eprivateProperty = $value;\n    }\n    \n    public function toArray(): array\n    {\n        return [$this-\u003epublicProperty, $this-\u003eprivateProperty];\n    }\n}\n\n$myObject = new MyClass();\n\n// Accessible by name:\nMapAccess::get($myObject, 'publicProperty', 11);\n\n// Accessible by getter:\nMapAccess::get($myObject, 'privateProperty', 22);\n\nprint_r($myObject-\u003etoArray());\n// [11, 22]\n```\n\n#### Exists\n\nReturns true if accessible key exists in the container.\n\n```MapAccess::exists(mixed $container, string $key): bool```\n\n```php\nuse Smoren\\TypeTools\\MapAccess;\n\n$array = [\n    'a' =\u003e 1,\n];\n\nvar_dump(MapAccess::exists($array, 'a'));\n// true\n\nvar_dump(MapAccess::exists($array, 'b'));\n// false\nclass MyClass {\n    public int $publicProperty = 1;\n    private int $privateProperty = 2;\n    private int $notAccessibleProperty = 3;\n    \n    public function getPrivateProperty(): int\n    {\n        return $this-\u003eprivateProperty;\n    }\n}\n\n$myObject = new MyClass();\n\n// Accessible by name:\nvar_dump(MapAccess::exists($myObject, 'publicProperty'));\n// true\n\n// Accessible by getter:\nvar_dump(MapAccess::exists($myObject, 'privateProperty'));\n// true\n\n// Not accessible:\nvar_dump(MapAccess::get($myObject, 'notAccessibleProperty'));\n// false\n\n// Nonexistent:\nvar_dump(MapAccess::get($myObject, 'nonexistentProperty', -1));\n// false\n```\n\n## Unit testing\n```\ncomposer install\ncomposer test-init\ncomposer test\n```\n\n## License\n\nPHP Type Tools is licensed under the MIT License.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmoren%2Ftype-tools-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmoren%2Ftype-tools-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmoren%2Ftype-tools-php/lists"}