{"id":22104692,"url":"https://github.com/riimu/kit-phpencoder","last_synced_at":"2025-04-07T12:09:11.525Z","repository":{"id":11559182,"uuid":"14045428","full_name":"Riimu/Kit-PHPEncoder","owner":"Riimu","description":"Highly customizable alternative to var_export for PHP code generation","archived":false,"fork":false,"pushed_at":"2022-12-12T12:28:50.000Z","size":217,"stargazers_count":71,"open_issues_count":2,"forks_count":4,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-03-31T11:02:22.255Z","etag":null,"topics":["code-generator","export","php","php-library"],"latest_commit_sha":null,"homepage":"http://kit.riimu.net","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/Riimu.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-11-01T14:47:39.000Z","updated_at":"2024-01-12T18:06:21.000Z","dependencies_parsed_at":"2023-01-13T16:33:54.057Z","dependency_job_id":null,"html_url":"https://github.com/Riimu/Kit-PHPEncoder","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Riimu%2FKit-PHPEncoder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Riimu%2FKit-PHPEncoder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Riimu%2FKit-PHPEncoder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Riimu%2FKit-PHPEncoder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Riimu","download_url":"https://codeload.github.com/Riimu/Kit-PHPEncoder/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247648977,"owners_count":20972945,"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":["code-generator","export","php","php-library"],"created_at":"2024-12-01T06:34:11.668Z","updated_at":"2025-04-07T12:09:11.500Z","avatar_url":"https://github.com/Riimu.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Variable Exporter #\n\n*PHPEncoder* is a PHP library for exporting variables and generating PHP code\nrepresentations for said variables similar to the built in function\n`var_export()`. Compared to the built in function, however, this library\nprovides more options to customize the output, which makes it easier to generate\ncode for different kinds of purposes such as readable configuration files or\noptimized cache files.\n\nThe purpose of this library is to address some of the shortcomings with the\nbuilt in `var_export()`. For example, there is no way to control the amount of\nwhitespace in the output and there is no way to choose between different array\nnotations. This library also provides functionality to convert objects into PHP\ncode that is actually useful when compared to the built in function.\n\nThe large number of customization options in this library allows you to create\ncode that fits your purposes. You can create very compact code, when you need to\nlimit the size of the output, or you can create code in the style that actually\nfits in any of your dynamically generated PHP files.\n\nThe API documentation is available at: http://kit.riimu.net/api/phpencoder/\n\n[![CI](https://img.shields.io/github/workflow/status/Riimu/Kit-PHPEncoder/CI/master?style=flat-square)](https://github.com/Riimu/Kit-PHPEncoder/actions)\n[![Scrutinizer](https://img.shields.io/scrutinizer/quality/g/Riimu/Kit-PHPEncoder/master?style=flat-square)](https://scrutinizer-ci.com/g/Riimu/Kit-PHPEncoder/)\n[![codecov](https://img.shields.io/codecov/c/github/Riimu/Kit-PHPEncoder/master?style=flat-square)](https://codecov.io/gh/Riimu/Kit-PHPEncoder)\n[![Packagist](https://img.shields.io/packagist/v/riimu/kit-phpencoder.svg?style=flat-square)](https://packagist.org/packages/riimu/kit-phpencoder)\n\n## Requirements ##\n\n  * The minimum supported PHP version is 5.6\n\n## Installation ##\n\n### Installation with Composer ###\n\nThe easiest way to install this library is to use Composer to handle your\ndependencies. In order to install this library via Composer, simply follow\nthese two steps:\n\n  1. Acquire the `composer.phar` by running the Composer\n     [Command-line installation](https://getcomposer.org/download/)\n     in your project root.\n\n  2. Once you have run the installation script, you should have the `composer.phar`\n     file in you project root and you can run the following command:\n\n     ```\n     php composer.phar require \"riimu/kit-phpencoder:^2.3\"\n     ```\n\nAfter installing this library via Composer, you can load the library by\nincluding the `vendor/autoload.php` file that was generated by Composer during\nthe installation.\n\n### Adding the library as a dependency ###\n\nIf you are already familiar with how to use Composer, you may alternatively add\nthe library as a dependency by adding the following `composer.json` file to your\nproject and running the `composer install` command:\n\n```json\n{\n    \"require\": {\n        \"riimu/kit-phpencoder\": \"^2.3\"\n    }\n}\n```\n\n### Manual installation ###\n\nIf you do not wish to use Composer to load the library, you may also download\nthe library manually by downloading the [latest release](https://github.com/Riimu/Kit-PHPEncoder/releases/latest)\nand extracting the `src` folder to your project. You may then include the\nprovided `src/autoload.php` file to load the library classes.\n\n## Usage ##\n\nThe most relevant method provided by this library is the `encode()` method\nprovided by `PHPEncoder`. The method takes any value as an argument and returns\nthe PHP code representation for that value.\n\nFor example:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n$encoder = new \\Riimu\\Kit\\PHPEncoder\\PHPEncoder();\necho $encoder-\u003eencode(['foo' =\u003e 'bar', [1, true, false, null, 1.0]]);\n```\n\nThis would create the following output:\n\n```\n[\n    'foo' =\u003e 'bar',\n    [1, true, false, null, 1.0],\n]\n```\n\nOf course, the most important feature of this library is the ability to\ncustomize the created the PHP code. As the second argument, the `encode()`\nmethod takes an array of options, which can be used to customize the returned\nPHP code. For example:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n$encoder = new \\Riimu\\Kit\\PHPEncoder\\PHPEncoder();\necho $encoder-\u003eencode(['foo' =\u003e 'bar', [1, true, false, null, 1.0]], [\n    'array.inline' =\u003e false,\n    'array.omit' =\u003e false,\n    'array.indent' =\u003e 2,\n    'boolean.capitalize' =\u003e true,\n    'null.capitalize' =\u003e true,\n]);\n```\n\nThis would create the following output:\n\n```\n[\n  'foo' =\u003e 'bar',\n  0 =\u003e [\n    0 =\u003e 1,\n    1 =\u003e TRUE,\n    2 =\u003e FALSE,\n    3 =\u003e NULL,\n    4 =\u003e 1.0,\n  ],\n]\n```\n\n### Options ###\n\nEncoding options allow you to customize the output of the `encode()` method. It\nis possible to set these options in three different ways:\n\n  * Options can be provided as an array to the `PHPEncoder` constructor.\n  * Option values can be set via the `setOption()` method.\n  * Options can be passed as an array as the second argument to the `encode()`\n    method.\n\nNote that options passed to the `encode()` method are only temporary and do not\napply to following calls.\n\n#### List of Options ####\n\n  * **whitespace** : \u0026lt;boolean\u0026gt; (true)\u003cbr\u003e\n    When set to `false`, generation of all extra whitespace is disabled and all\n    other settings that affect whitespace are ignored.\n\n  * **hex.capitalize** : \u0026lt;boolean\u0026gt; (false)\u003cbr\u003e\n    When set to `true` all hexadecimal characters in the output are written\n    using upper case instead of lower case.\n\n  * **null.capitalize** : \u0026lt;boolean\u0026gt; (false)\u003cbr\u003e\n    When set to `true`, all `null` values are written in upper case instead of\n    lower case.\n\n  * **boolean.capitalize** : \u0026lt;boolean\u0026gt; (false)\u003cbr\u003e\n    When set to `true`, all `true` and `false` values are written in upper case\n    instead of lower case.\n\n  * **integer.type** : \u0026lt;\"binary\"|\"octal\"|\"decimal\"|\"hexadecimal\"\u0026gt; (\"decimal\")\u003cbr\u003e\n    Change the output syntax of integers. For example, using the type `\"hexadecimal\"`\n    would output the number `15` as `0xf`.\n\n  * **float.integers** : \u0026lt;boolean|\"all\"\u0026gt; (false)\u003cbr\u003e\n    When set to `true`, any float that represents an integer and has a value\n    that is accurately represented by the floating point number will be encoded\n    as an integer instead of a float. (e.g. the value `2.0` will be encoded as\n    `2`). To include the values that are not accurately represented, you may set\n    option to `\"all\"`.\n\n  * **float.export** : \u0026lt;boolean\u0026gt; (false)\u003cbr\u003e\n    When set to `true` floats are encoded using `var_export()`, which causes a\n    slightly different output on non integer floating point numbers compared to\n    the standard implemented method. In some cases, this may produce more\n    accurate numbers but with less cleaner representation.\n\n  * **float.precision** : \u0026lt;integer|false\u0026gt; (17)\u003cbr\u003e\n    The maximum precision of encoded floating point values, which usually also\n    means the maximum number of digits in encoded floats. If the value is set to\n    `false`, the PHP ini setting `serialize_precision` will be used instead.\n    Note that due to the way floating point values work, a value greater than 17\n    does not provide any additional precision.\n\n  * **string.binary** : \u0026lt;boolean\u0026gt; (false)\u003cbr\u003e\n    When set to `true` any string that is not valid UTF-8 will be encoded in\n    base 64 and wrapped with `base64_decode()` call.\n\n  * **string.escape** : \u0026lt;boolean\u0026gt; (true)\u003cbr\u003e\n    When set to `true`, all strings containing bytes outside the 32-126 ASCII\n    range will be written with double quotes and the characters outside the\n    range will be escaped.\n\n  * **string.utf8** : \u0026lt;boolean\u0026gt; (false)\u003cbr\u003e\n    When both this option and `string.escape` are set to `true`, all valid\n    multibyte UTF-8 characters in strings are encoded using the PHP7 unicode\n    code point syntax. Note that this syntax does not work in PHP versions\n    earlier than 7.0.\n    \n  * **string.classes** : \u0026lt;string[]\u0026gt; ([])\u003cbr\u003e\n    Defines a list of classes or class namespace prefixes for classes that\n    can be encoded using the class resolution operator `::class` when \n    encountered in strings. e.g. providing the value `['Riimu\\\\']` would encode\n    all strings that look like class names in the `Riimu` namespace like\n    `Riimu\\Kit\\PHPEncoder::class`.\n    \n  * **string.imports** : \u0026lt;string[]\u0026gt; ([])\u003cbr\u003e\n    List of imports used in the resulting code file, which allows class names\n    to be written using shorter format. The list should be an associative array\n    with the namespace or class as the key and the used name as the value. Use\n    empty string to indicate the current namespace.\n    \n    For example, if the resulting file would have `namespace Riimu\\Kit\\PHPEncoder;`\n    and `use PHPUnit\\Framework\\TestCase;`, you could set up the array as\n    `['Riimu\\\\Kit\\\\PHPEncoder\\\\' =\u003e '', 'PHPUnit\\\\Framework\\\\TestCase' =\u003e 'TestCase']`\n\n  * **array.short** : \u0026lt;boolean\u0026gt; (true)\u003cbr\u003e\n    When set to `true`, arrays are enclosed using square brackets `[]` instead\n    using of the long array notation `array()`.\n\n  * **array.base** : \u0026lt;integer|string\u0026gt; (0)\u003cbr\u003e\n    Base indentation for arrays as a number of spaces or as a string. Provides\n    convenience when you need to output code to a file with specific level of\n    indentation.\n\n  * **array.indent** : \u0026lt;integer|string\u0026gt; (4)\u003cbr\u003e\n    Amount of indentation for single level of indentation as a number of spaces\n    or a string.\n\n  * **array.align** : \u0026lt;boolean\u0026gt; (false)\u003cbr\u003e\n    When set to `true`, array assignment operators `=\u003e` are aligned to the same\n    column using spaces. Even if enabled, `array.omit` and `array.inline`\n    options are still respected, but only if all the keys in the specific array\n    can be omitted.\n\n  * **array.inline** : \u0026lt;boolean|integer\u0026gt; (70)\u003cbr\u003e\n    When set to `true`, any array that can be written without any array keys\n    will be written in a single line. If an integer is provided instead, the\n    array will be written as a single line only if it does not exceed that\n    number of characters. This option has no effect when `array.omit` is set to\n    false.\n\n  * **array.omit** : \u0026lt;boolean\u0026gt; (true)\u003cbr\u003e\n    When set to `true`, any redundant array keys will not be included (e.g. the\n    array `[0 =\u003e 'a', 1 =\u003e 'b']` would be encoded just as `['a', 'b']`).\n\n  * **array.eol** : \u0026lt;string|false\u0026gt; (false)\u003cbr\u003e\n    The end of line character used by array output. When set to `false`, the\n    default `PHP_EOL` will be used instead.\n\n  * **object.method** : \u0026lt;boolean\u0026gt; (true)\u003cbr\u003e\n    When set to `true`, any encoded object will be checked for methods `toPHP()`\n    and `toPHPValue()`. If the method `toPHP()` exists, the returned string will\n    be used as the PHP code representation of the object. If the method\n    `toPHPValue()` exists instead, the returned value will be encoded as PHP and\n    used as the code representation of the object.\n\n  * **object.format** : \u0026lt;string\u0026gt; ('vars')\u003cbr\u003e\n    Default object encoding format. The possible values are:\n\n     * `string` casts the object to string and then encodes that string as PHP.\n     * `serialize` serializes the object and wraps it with `unserialize()`\n     * `export` mimics the `var_export()` object representation\n     * `array` casts the object to an array and encodes that array\n     * `vars` turns object into an array using `get_object_vars()`\n     * `iterate` turns the object into an array by iterating over it with `foreach`\n\n  * **object.cast** : \u0026lt;boolean\u0026gt; (true)\u003cbr\u003e\n    Whether to add an `(object)` cast in front of arrays generated from objects\n    or not when using the object encoding formats `vars`, `array` or `iterate`.\n\n  * **recursion.detect** : \u0026lt;boolean\u0026gt; (true)\u003cbr\u003e\n    When set to `true`, the encoder will attempt to detect circular references\n    in arrays and objects to avoid infinite loops.\n\n  * **recursion.ignore** : \u0026lt;boolean\u0026gt; (false)\u003cbr\u003e\n    When set to `true`, any circular reference will be replaced with `null`\n    instead of throwing an exception.\n\n  * **recursion.max** : \u0026lt;integer|false\u0026gt; (false)\u003cbr\u003e\n    Maximum number of levels when encoding arrays and objects. Exception is\n    thrown when the maximum is exceeded. Set to `false` to have no limit.\n\n## Credits ##\n\nThis library is Copyright (c) 2013-2022 Riikka Kalliomäki.\n\nSee LICENSE for license and copying information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Friimu%2Fkit-phpencoder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Friimu%2Fkit-phpencoder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Friimu%2Fkit-phpencoder/lists"}