{"id":13828379,"url":"https://github.com/sebastiaanluca/php-helpers","last_synced_at":"2025-07-09T06:31:44.940Z","repository":{"id":62541602,"uuid":"122551290","full_name":"sebastiaanluca/php-helpers","owner":"sebastiaanluca","description":"THIS PACKAGE HAS BEEN DEPRECATED — An extensive set of PHP helper functions and classes.","archived":true,"fork":false,"pushed_at":"2023-02-06T03:15:17.000Z","size":41,"stargazers_count":27,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-25T19:57:07.379Z","etag":null,"topics":["array","carbon","class","constants","datetime","debug","functions","helpers","methods","object","php","string"],"latest_commit_sha":null,"homepage":"https://sebastiaanluca.com","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/sebastiaanluca.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-02-23T00:12:35.000Z","updated_at":"2023-02-06T03:20:17.000Z","dependencies_parsed_at":"2024-01-18T05:29:07.265Z","dependency_job_id":"40b08bbe-5082-4207-b9da-fe9fab28d896","html_url":"https://github.com/sebastiaanluca/php-helpers","commit_stats":{"total_commits":35,"total_committers":1,"mean_commits":35.0,"dds":0.0,"last_synced_commit":"93930ff5d987095864c0b8c1d52aa1aa830e2a38"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiaanluca%2Fphp-helpers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiaanluca%2Fphp-helpers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiaanluca%2Fphp-helpers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiaanluca%2Fphp-helpers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sebastiaanluca","download_url":"https://codeload.github.com/sebastiaanluca/php-helpers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225492420,"owners_count":17482869,"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":["array","carbon","class","constants","datetime","debug","functions","helpers","methods","object","php","string"],"created_at":"2024-08-04T09:02:43.742Z","updated_at":"2024-11-20T08:30:29.440Z","avatar_url":"https://github.com/sebastiaanluca.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# An extensive set of PHP helper functions and classes\n\n[![Latest stable release][version-badge]][link-packagist]\n[![Software license][license-badge]](LICENSE.md)\n[![Build status][githubaction-badge]][link-githubaction]\n[![Total downloads][downloads-badge]][link-packagist]\n[![Total stars][stars-badge]][link-github]\n\n[![Read my blog][blog-link-badge]][link-blog]\n[![View my other packages and projects][packages-link-badge]][link-packages]\n[![Follow @sebastiaanluca on Twitter][twitter-profile-badge]][link-twitter]\n[![Share this package on Twitter][twitter-share-badge]][link-twitter-share]\n\n## Table of contents\n\n- [Requirements](#requirements)\n- [How to install](#how-to-install)\n- [Global helper functions](#global-helper-functions)\n    - [rand_bool](#rand_bool)\n    - [str_wrap](#str_wrap)\n    - [is\\_assoc\\_array](#is_assoc_array)\n    - [array_expand](#array_expand)\n    - [array_without](#array_without)\n    - [array\\_pull\\_value](#array_pull_value)\n    - [array\\_pull\\_values](#array_pull_values)\n    - [array_hash](#array_hash)\n    - [object_hash](#object_hash)\n    - [has\\_public\\_method](#has_public_method)\n    - [carbon](#carbon)\n    - [temporary_file](#temporary_file)\n- [Class helpers](#class-helpers)\n    - [Constants trait](#constants-trait)\n        - [Retrieving constants](#retrieving-constants)\n        - [Retrieving constant keys](#retrieving-constant-keys)\n        - [Retrieving constant values](#retrieving-constant-values)\n    - [ProvidesClassInfo trait](#providesclassinfo-trait)\n    - [MethodHelper](#methodhelper)\n- [License](#license)\n- [Change log](#change-log)\n- [Testing](#testing)\n- [Contributing](#contributing)\n- [Security](#security)\n- [Credits](#credits)\n- [About](#about)\n\n## Requirements\n\n- PHP 8 or higher\n\n## How to install\n\nVia Composer:\n\n```bash\ncomposer require sebastiaanluca/php-helpers\n```\n\nAll function helpers will be enabled by default (if those functions haven't already been defined). Class helpers are enabled per-case when used.\n\nYou can find more info on how to use a helper and what there requirements are in their respective section (see the table of contents above for an overview).\n\n## Global helper functions\n\n### rand_bool\n\nRandomly return `true` or `false`.\n\n```php\nrand_bool();\n\n// true\n```\n\n### str_wrap\n\nWrap a string with another string.\n\n```php\nstr_wrap('foo', '*');\n\n// \"*foo*\"\n```\n\n### is\\_assoc\\_array\n\nCheck if an array is associative.\n\nPerforms a simple check to determine if the given array's keys are numeric, start at 0, and count up to the amount of values it has.\n\n```php\nis_assoc_array(['color' =\u003e 'blue', 'age' =\u003e 31]);\n\n// true\n```\n\n```php\nis_assoc_array([0 =\u003e 'blue', 7 =\u003e 31]);\n\n// true\n```\n\n```php\nis_assoc_array(['blue', 31]);\n\n// false\n```\n\n```php\nis_assoc_array([0 =\u003e 'blue', 1 =\u003e 31]);\n\n// false\n```\n\n### array_expand\n\nExpand a flat dotted array into a multi-dimensional associative array.\n\nIf a key is encountered that is already present and the existing value is an array, each new value will be added to that array. If it's not an array, each new value will override the existing one.\n\n```php\narray_expand(['products.desk.price' =\u003e 200]);\n\n/*\n[\n    \"products\" =\u003e [\n        \"desk\" =\u003e [\n            \"price\" =\u003e 200,\n        ],\n    ],\n]\n*/\n```\n\n### array_without\n\nGet the array without the given values.\n\nAccepts either an array or a value as parameter to remove.\n\n```php\n$cars = ['bmw', 'mercedes', 'audi'];\n$soldOut = ['audi', 'bmw'];\n\n$inStock = array_without($cars, $soldOut);\n\n// [\"mercedes\"]\n```\n\n```php\narray_without(['one', 'two', 'three'], 'two');\n\n// [\"one\", \"three\"]\n```\n\n### array\\_pull\\_value\n\nPull a single value from a given array.\n\nReturns the given value if it was successfully removed from the source array or `null` if it was not found.\n\n```php\n$source = ['A', 'B', 'C'];\n\n$removed = array_pull_value($source, 'C');\n\n// $removed = \"C\"\n// $source = [\"A\", \"B\"]\n```\n\n### array\\_pull\\_values\n\nPull an array of values from a given array.\n\nReturns the values that were successfully removed from the source array or an empty array if none were found.\n\n```php\n$source = ['A', 'B', 'C'];\n$removed = array_pull_values($source, ['A', 'B']);\n\n// $removed = [\"A\", \"B\"]\n// $source = [\"C\"]\n```\n\n### array_hash\n\nCreate a unique string identifier for an array.\n\nThe identifier will be entirely unique for each combination of keys and values.\n\n```php\narray_hash([1, 2, 3]);\n\n// \"262bbc0aa0dc62a93e350f1f7df792b9\"\n```\n\n```php\narray_hash(['hash' =\u003e 'me']);\n\n// \"f712e79b502bda09a970e2d4d47e3f88\"\n```\n\n### object_hash\n\nCreate a unique string identifier for an object.\n\nSimilar to [array_hash](#array_hash), this uses `serialize` to *stringify* all public properties first. The identifier will be entirely unique based on the object class, properties, and its values.\n\n```php\nclass ValueObject {\n    public $property = 'randomvalue';\n}\n\nobject_hash(new ValueObject);\n\n// \"f39eaea7a1cf45f5a0c813d71b5f2f57\"\n```\n\n### has\\_public\\_method\n\nCheck if a class has a certain public method.\n\n```php\nclass Hitchhiker {\n    public function answer() {\n        return 42;\n    }\n}\n\nhas_public_method(Hitchhiker::class, 'answer');\n\n// true\n\nhas_public_method(new Hitchhiker, 'answer');\n\n// true\n```\n\n### carbon\n\nCreate a Carbon datetime object from a string or return a new object referencing the current date and time.\n\nRequires the [nesbot/carbon](https://github.com/briannesbitt/Carbon) package.\n\n```php\ncarbon('2017-01-18 11:30');\n\n/*\nCarbon\\Carbon {\n    \"date\": \"2017-01-18 11:30:00.000000\",\n    \"timezone_type\": 3,\n    \"timezone\": \"UTC\",\n}\n*/\n\ncarbon();\n\n/*\nCarbon\\Carbon {\n    \"date\": \"2017-10-27 16:18:00.000000\",\n    \"timezone_type\": 3,\n    \"timezone\": \"UTC\",\n}\n*/\n```\n\n### temporary_file\n\nCreate a temporary file.\n\nReturns an array with the file handle (resource) and the full path as string.\n\nThe temporary file is readable and writeable by default. The file is automatically removed when closed (for example, by calling fclose() on the handle, or when there are no remaining references to the file handle), or when the script ends.\n\nSee [](http://php.net/manual/en/function.tmpfile.php) for more information.\n\n```php\ntemporary_file();\n\n/*\n[\n    \"file\" =\u003e stream resource {\n        timed_out: false\n        blocked: true\n        eof: false\n        wrapper_type: \"plainfile\"\n        stream_type: \"STDIO\"\n        mode: \"r+b\"\n        unread_bytes: 0\n        seekable: true\n        uri: \"/tmp/phpxm4bcZ\"\n        options: []\n    }\n    \"path\" =\u003e \"/tmp/phpxm4bcZ\"\n]\n*/\n```\n\n## Class helpers\n\n### Enum trait\n\nThe primary use of the `Enum` trait is to enable you to store all cases of a specific type in a single class or value object and have it return those with a single call.\n\nThis can be useful for instance when your database uses integers to store states, but you want to use descriptive strings throughout your code (i.e. enums). It also allows you to refactor these elements at any time without having to waste time searching your code for any raw values (and probably miss a few, introducing new bugs along the way).\n\n#### Retrieving elements\n\nReturns an array of element keys and their values.\n\n```php\n\u003c?php\n\nuse SebastiaanLuca\\PhpHelpers\\Classes\\Enum;\n\nclass UserStates\n{\n    use Enum;\n\n    public const REGISTERED = 'registered';\n    public const ACTIVATED = 'activated';\n    public const DISABLED = 'disabled';\n}\n\nUserStates::enums();\n\n// or\n\n(new UserStates)-\u003eenums();\n\n/*\n[\n    \"REGISTERED\" =\u003e \"registered\",\n    \"ACTIVATED\" =\u003e \"activated\",\n    \"DISABLED\" =\u003e \"disabled\",\n]\n*/\n```\n\n#### Retrieving element keys\n\nReturns all the keys of the elements in an enum.\n\n```php\n\u003c?php\n\nuse SebastiaanLuca\\PhpHelpers\\Classes\\Enum;\n\nclass UserStates\n{\n    use Enum;\n\n    public const REGISTERED = 'registered';\n    public const ACTIVATED = 'activated';\n    public const DISABLED = 'disabled';\n}\n\nUserStates::keys();\n\n/*\n[\n    \"REGISTERED\",\n    \"ACTIVATED\",\n    \"DISABLED\",\n]\n*/\n```\n\n#### Retrieving constant values\n\nReturns all the values of the elements in an enum.\n\n```php\n\u003c?php\n\nuse SebastiaanLuca\\PhpHelpers\\Classes\\Enum;\n\nclass UserStates\n{\n    use Enum;\n\n    public const REGISTERED = 'registered';\n    public const ACTIVATED = 'activated';\n    public const DISABLED = 'disabled';\n}\n\nUserStates::values();\n\n/*\n[\n    \"registered\",\n    \"activated\",\n    \"disabled\",\n]\n*/\n```\n\n### ProvidesClassInfo trait\n\nThe `ProvidesClassInfo` trait provides an easy-to-use `getClassDirectory()` helper method that returns the directory of the current class.\n\n```php\n\u003c?php\n\nnamespace Kyle\\Helpers;\n\nuse SebastiaanLuca\\PhpHelpers\\Classes\\ProvidesClassInfo;\n\nclass MyClass\n{\n    use ProvidesClassInfo;\n\n    public function __construct()\n    {\n        var_dump($this-\u003egetClassDirectory());\n    }\n}\n\n// \"/Users/Kyle/Projects/php-helpers\"\n```\n\n### MethodHelper\n\nA static class helper to help you figure out the visibility/accessibility of an object's methods.\n\n```php\n\u003c?php\n\nclass SomeClass\n{\n    private function aPrivateMethod() : string\n    {\n        return 'private';\n    }\n\n    protected function aProtectedMethod() : string\n    {\n        return 'protected';\n    }\n\n    public function aPublicMethod() : string\n    {\n        return 'public';\n    }\n}\n\nMethodHelper::hasMethodOfType($class, 'aPrivateMethod', 'private');\n\n// true\n\nMethodHelper::hasProtectedMethod($class, 'aProtectedMethod');\n\n// true\n\nMethodHelper::hasPublicMethod($class, 'aPublicMethod');\n\n// true\n\nMethodHelper::hasProtectedMethod($class, 'aPrivateMethod');\n\n// false\n\nMethodHelper::hasPublicMethod($class, 'invalidMethod');\n\n// false\n```\n\n## License\n\nThis package operates under the MIT License (MIT). Please see [LICENSE](LICENSE.md) for more information.\n\n## Change log\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Testing\n\n```bash\ncomposer install\ncomposer test\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) and [CODE OF CONDUCT](CODE_OF_CONDUCT.md) for details.\n\n## Security\n\nIf you discover any security related issues, please email [hello@sebastiaanluca.com][link-author-email] instead of using the issue tracker.\n\n## Credits\n\n- [Sebastiaan Luca][link-github-profile]\n- [All Contributors][link-contributors]\n\n## About\n\nMy name is Sebastiaan and I'm a freelance back-end developer specializing in building custom Laravel applications. Check out my [portfolio][link-portfolio] for more information, [my blog][link-blog] for the latest tips and tricks, and my other [packages][link-packages] to kick-start your next project.\n\nHave a project that could use some guidance? Send me an e-mail at [hello@sebastiaanluca.com][link-author-email]!\n\n[version-badge]: https://img.shields.io/packagist/v/sebastiaanluca/php-helpers.svg?label=stable\n[license-badge]: https://img.shields.io/badge/license-MIT-informational.svg\n[githubaction-badge]: https://github.com/sebastiaanluca/php-helpers/actions/workflows/test.yml/badge.svg?branch=master\n[downloads-badge]: https://img.shields.io/packagist/dt/sebastiaanluca/php-helpers.svg?color=brightgreen\n[stars-badge]: https://img.shields.io/github/stars/sebastiaanluca/php-helpers.svg?color=brightgreen\n\n[blog-link-badge]: https://img.shields.io/badge/link-blog-lightgrey.svg\n[packages-link-badge]: https://img.shields.io/badge/link-other_packages-lightgrey.svg\n[twitter-profile-badge]: https://img.shields.io/twitter/follow/sebastiaanluca.svg?style=social\n[twitter-share-badge]: https://img.shields.io/twitter/url/http/shields.io.svg?style=social\n\n[link-github]: https://github.com/sebastiaanluca/php-helpers\n[link-packagist]: https://packagist.org/packages/sebastiaanluca/php-helpers\n[link-githubaction]: https://github.com/sebastiaanluca/php-helpers/actions/workflows/test.yml?query=branch%3Amaster\n[link-twitter-share]: https://twitter.com/intent/tweet?text=Check%20out%20this%20extensive%20set%20of%20generic%20PHP%20helper%20functions%20and%20classes!%20Via%20@sebastiaanluca%20https://github.com/sebastiaanluca/php-helpers\n[link-contributors]: ../../contributors\n\n[link-portfolio]: https://sebastiaanluca.com\n[link-blog]: https://sebastiaanluca.com/blog\n[link-packages]: https://packagist.org/packages/sebastiaanluca\n[link-twitter]: https://twitter.com/sebastiaanluca\n[link-github-profile]: https://github.com/sebastiaanluca\n[link-author-email]: mailto:hello@sebastiaanluca.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastiaanluca%2Fphp-helpers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsebastiaanluca%2Fphp-helpers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastiaanluca%2Fphp-helpers/lists"}