{"id":21823536,"url":"https://github.com/jbzoo/simpletypes","last_synced_at":"2025-08-21T06:30:39.273Z","repository":{"id":34503715,"uuid":"38444681","full_name":"JBZoo/SimpleTypes","owner":"JBZoo","description":"The universal PHP library to convert any values and measures (money, weight, currency converter, length, etc.).","archived":false,"fork":false,"pushed_at":"2024-01-28T12:22:22.000Z","size":463,"stargazers_count":62,"open_issues_count":0,"forks_count":3,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-12-10T22:10:10.632Z","etag":null,"topics":["arithmetic","currencies","currency-coverter","html","measure","measures","money","smart-parser","universal-php-library","weight"],"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/JBZoo.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":"2015-07-02T16:43:30.000Z","updated_at":"2024-10-24T19:09:30.000Z","dependencies_parsed_at":"2023-12-19T04:22:42.252Z","dependency_job_id":"89d11611-6550-4df8-8dc7-6989e95c50aa","html_url":"https://github.com/JBZoo/SimpleTypes","commit_stats":{"total_commits":347,"total_committers":8,"mean_commits":43.375,"dds":0.6397694524495677,"last_synced_commit":"2e8f4f1268053f9602acd9e1ef53c7f6ac12883d"},"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBZoo%2FSimpleTypes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBZoo%2FSimpleTypes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBZoo%2FSimpleTypes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBZoo%2FSimpleTypes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JBZoo","download_url":"https://codeload.github.com/JBZoo/SimpleTypes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230494921,"owners_count":18235046,"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":["arithmetic","currencies","currency-coverter","html","measure","measures","money","smart-parser","universal-php-library","weight"],"created_at":"2024-11-27T17:33:16.841Z","updated_at":"2024-12-19T20:08:03.168Z","avatar_url":"https://github.com/JBZoo.png","language":"PHP","readme":"# JBZoo / SimpleTypes\n\n[![CI](https://github.com/JBZoo/SimpleTypes/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/JBZoo/SimpleTypes/actions/workflows/main.yml?query=branch%3Amaster)    [![Coverage Status](https://coveralls.io/repos/github/JBZoo/SimpleTypes/badge.svg?branch=master)](https://coveralls.io/github/JBZoo/SimpleTypes?branch=master)    [![Psalm Coverage](https://shepherd.dev/github/JBZoo/SimpleTypes/coverage.svg)](https://shepherd.dev/github/JBZoo/SimpleTypes)    [![Psalm Level](https://shepherd.dev/github/JBZoo/SimpleTypes/level.svg)](https://shepherd.dev/github/JBZoo/SimpleTypes)    [![CodeFactor](https://www.codefactor.io/repository/github/jbzoo/simpletypes/badge)](https://www.codefactor.io/repository/github/jbzoo/simpletypes/issues)    \n[![Stable Version](https://poser.pugx.org/jbzoo/simpletypes/version)](https://packagist.org/packages/jbzoo/simpletypes/)    [![Total Downloads](https://poser.pugx.org/jbzoo/simpletypes/downloads)](https://packagist.org/packages/jbzoo/simpletypes/stats)    [![Dependents](https://poser.pugx.org/jbzoo/simpletypes/dependents)](https://packagist.org/packages/jbzoo/simpletypes/dependents?order_by=downloads)    [![GitHub License](https://img.shields.io/github/license/jbzoo/simpletypes)](https://github.com/JBZoo/SimpleTypes/blob/master/LICENSE)\n\n\nThe universal PHP library to convert any values and measures - money, weight, currency coverter, length and what ever you want ;)\n\n\n## Installation\n```\ncomposer require jbzoo/simpletypes\n```\n\n## Examples\n\n```php\nuse JBZoo\\SimpleTypes\\Config;\nuse JBZoo\\SimpleTypes\\Money;\nuse JBZoo\\SimpleTypes\\ConfigMoney;\n\n// Set config object for all Money objects as default\nConfig::registerDefault('money', new ConfigMoney());\n\n// Create any object, some different ways\n$money  = new Money('10 eur');\n$weight = new Weight('1000'); // Gram is default in the ConfigWeight class\n$length = new Length('500 km');\n$money  = new Money('100500 usd', new ConfigMoney()); // my custom params only for that object\n```\n\n## A lot of types are ready to use\n\nSimpleTypes has such ready configurations like\n  * [Area](https://github.com/JBZoo/SimpleTypes/blob/master/src/config/area.php)\n  * [Degree](https://github.com/JBZoo/SimpleTypes/blob/master/src/config/degree.php) (geometry)\n  * [Info](https://github.com/JBZoo/SimpleTypes/blob/master/src/config/info.php) (bytes, bits...)\n  * [Length](https://github.com/JBZoo/SimpleTypes/blob/master/src/config/length.php)\n  * [Money](https://github.com/JBZoo/SimpleTypes/blob/master/src/config/money.php) (Currency converter)\n  * [Temperature](https://github.com/JBZoo/SimpleTypes/blob/master/src/config/temp.php) (Kelvin, Fahrenheit, Celsius and etc)\n  * [Volume](https://github.com/JBZoo/SimpleTypes/blob/master/src/config/volume.php)\n  * [Weight](https://github.com/JBZoo/SimpleTypes/blob/master/src/config/weight.php)\n\nYou can add your own type. It's really easy. See this page below.\n\n### Smart and useful parser\n\nSimpleTypes has really smart parser for all input values.\nIt can find number, understand any decimal symbols, trim, letter cases, e.t.c...\nand it works really fast!\n\n```php\n$money = new Money(' - 1 2 3 , 4 5 6 rub ');    // Equals -123.456 rubles\n$money = new Money('1.0e+18 EUR ');             // Really huge number. I'm rich! =)\n$money = new Money('  EuR 3,50   ');\n$money = new Money('usd');                      // Just object with usd rule\n```\n\n### Chaining method calls\n```php\n$value = (new Money('4.95 usd'))\n    -\u003eadd('10 usd')                         // $14.95\n    -\u003esubtract('2 eur')                     // $10.95\n    -\u003enegative()                            // -$10.95\n    -\u003egetClone()                            // copy of object is created\n    -\u003edivision(5)                           // -$2.19\n    -\u003emultiply(10)                          // -$21.90\n    -\u003econvert('eur')                        // -10.95€ (For easy understanding we use 1 EUR = 2 USD)\n    -\u003ecustomFunc(function (Money $value) {  // sometimes we would like something more than plus/minus ;)\n        $value\n            -\u003eadd(new Money('600 rub'))     // 1.05€ (1 EUR = 50 RUB)\n            -\u003eadd('-500%');                 // -4.2€\n    })\n    -\u003eabs();                                // 4.2€\n```\n\n## Basic arithmetic\nDifferent ways to use basic arithmetic\n```php\n// example #1\n$usd = new Money('10 usd');\n$usd-\u003eadd(new Money('10 eur'));\n\n// example #2\n$usd = (new Money('10 usd'))-\u003eadd(new Money('10 eur'));\n\n// example #3\n$usd-\u003eadd('10 eur');\n\n// example #4\n$usd-\u003eadd('10'); // eur is default in the ConfigMoney\n\n// example #5\n$usd-\u003eadd(['10', 'eur']);\n```\n\nSimpleTypes can\n * add\n * subtract\n * division\n * multiply\n * use custom functions (Closure)\n * negative / positibe / invert sign / abs\n * use percent\n * setting empty value\n * setting another value and rule\n * create clone\n * converting to any rules (currencies, units)\n * rounding\n * comparing\n * ... and others\n\n## Compare values\n\n```php\n$kg = new Weight('1 kg'); // one kilogram\n$lb = new Weight('2 lb'); // two pounds\n\nvar_dump($kg-\u003ecompare($lb));            // false (\"==\" by default)\nvar_dump($kg-\u003ecompare($lb, '=='));      // false\nvar_dump($kg-\u003ecompare($lb, '\u003c'));       // false\nvar_dump($kg-\u003ecompare($lb, '\u003c='));      // false\nvar_dump($kg-\u003ecompare($lb, '\u003e'));       // true\nvar_dump($kg-\u003ecompare($lb, '\u003e='));      // true\n```\n\nAnd same examples but we will use smart parser\n```php\n$kg = new Weight('1 kg');\n$lb = new Weight('2 lb');\n\nvar_dump($kg-\u003ecompare('1000 g'));       // true\nvar_dump($kg-\u003ecompare('2 lb', '=='));   // false\nvar_dump($kg-\u003ecompare('2 lb', '\u003c'));    // false\nvar_dump($kg-\u003ecompare('2 lb', '\u003c='));   // false\nvar_dump($kg-\u003ecompare('2 lb', '\u003e'));    // true\nvar_dump($kg-\u003ecompare('2 lb', '\u003e='));   // true\n```\n\n## Percent method\nSimple way for count difference between two values\n```php\n$origPrice = new Money('100 usd');\n$realPrice = new Money('40 eur');\n\n$diff = $realPrice-\u003epercent($origPrice);\necho $diff-\u003etext(); // 80%\n\n$discount = $realPrice-\u003epercent($origPrice, true); // revert flag added\necho $discount-\u003etext(); // 20%\n```\n\n## PHP magic methods\n\nSafe serialize/unserialize\n```php\n$valBefore = $this-\u003eval('500 usd');\n$valString = serialize($valBefore);\n$valAfter  = unserialize($valString)-\u003econvert('eur');\n$valBefore-\u003ecompare($valAfter);// true\n```\n\n__toString() works like text() method\n```php\n$val = $this-\u003eval('500 usd');\necho $val; // \"$500.00\"\n```\n\n__invoke()\n```php\n$val = $this-\u003eval('10 eur');\n// it's converting\n$val('usd'); // so object now contains \"20 usd\" (1 eur = 2 usd)\n// set new value and rule\n$val('100 rub');\n$val('100', 'uah');\n```\n\n## Different ways for output and rendering\n### Only text\n\n```php\n$value = new Money('-50.666666 usd');\necho $value-\u003etext();            // \"-$50.67\"\necho $value-\u003etext('rub');       // \"-1 266,67 руб.\" (output without changing inner state)\necho $value-\u003enoStyle('rub');    // \"-1 266,67\" (without symbol)\n```\n\n### Simple HTML rendering\n```php\necho (new Money('-50.666666 usd'))-\u003ehtml('rub'); // render HTML, useful for JavaScript\n```\nOutput (warping added just for clarity)\n```php\n\u003cspan\n    class=\"simpleType simpleType-block simpleType-money\"\n    data-simpleType-id=\"1\"\n    data-simpleType-value=\"-1266.66665\"\n    data-simpleType-rule=\"rub\"\n    data-simpleType-orig-value=\"-50.666666\"\n    data-simpleType-orig-rule=\"usd\"\u003e\n        -\u003cspan class=\"simpleType-value\"\u003e1 266,67\u003c/span\u003e\n        \u003cspan class=\"simpleType-symbol\"\u003eруб.\u003c/span\u003e\n\u003c/span\u003e\n```\n\n### HTML Input type[text]\n```php\necho $value-\u003ehtmlInput('rub', 'input-name-attr');\n```\nOutput (warping added just for clarity)\n```html\n\u003cinput\n    value=\"-1 266,67\"\n    name=\"input-name-attr\"\n    type=\"text\"\n    class=\"simpleType simpleType-money simpleType-input\"\n    data-simpleType-id=\"1\"\n    data-simpleType-value=\"-1266.66665\"\n    data-simpleType-rule=\"rub\"\n    data-simpleType-orig-value=\"-50.666666\"\n    data-simpleType-orig-rule=\"usd\"\n/\u003e\n```\n\n**Notice:** Yes, we added a lot of data-attributes in the HTML code. It will be useful for JavaScript and converting without reload a page.\n\n\n## Configuration of type\n\nAll configuration classes should be extended from Config class\nFor example, config for information\n```php\n/**\n * Class ConfigInfo\n * @package JBZoo\\SimpleTypes\n */\nclass ConfigInfo extends Config\n{\n    /**\n     * SimpleTypes uses it for converting and while parsing undefined values\n     * @var string\n     */\n    public $default = 'byte';\n    \n    /**\n     * To collect or not to collect logs for each object (need additional memory a little bit)\n     * @var bool\n     */\n    public $isDebug = true;\n    \n    /**\n     * Array of converting rules and output format\n     * return array\n     */\n    public function getRules()\n    {\n        // key of array is alias for parser\n        return array(\n            'byte' =\u003e array(\n                'rate' =\u003e 1 // Because 1 byte to byte is 1 =)))\n            ),\n\n            'kb'   =\u003e array(\n                'symbol'          =\u003e 'KB',                     // symbol for output (-\u003etext(), -\u003ehtml(), ...)\n                'round_type'      =\u003e Formatter::ROUND_CLASSIC, // classic, float, ceil, none\n                'round_value'     =\u003e Formatter::ROUND_DEFAULT, // Count of valuable number after decimal point for any arithmetic actions\n                'num_decimals'    =\u003e '2',       // Sets the number of decimal points\n                'decimal_sep'     =\u003e '.',       // Sets the separator for the decimal point.\n                'thousands_sep'   =\u003e ' ',       // Sets the thousands separator.\n                'format_positive' =\u003e '%v %s',   // %v - replace to rounded and formated (number_format()) value\n                'format_negative' =\u003e '-%v %s',  // %s - replace to symbol\n                'rate'            =\u003e 1024,      // How many bytes (default measure) in the 1 KB ?\n            ),\n\n            'mb'   =\u003e array( // Other params gets from $this-\u003edefaultParams variable\n                'symbol' =\u003e 'MB',\n                'rate'   =\u003e 1024 * 1024,\n            ),\n\n            'gb'   =\u003e array( // Other params gets from $this-\u003edefaultParams variable\n                'symbol' =\u003e 'GB',\n                'rate'   =\u003e 1024 * 1024 * 1024,\n            ),\n\n            'bit'  =\u003e array(\n                'symbol' =\u003e 'Bit',\n                'rate'   =\u003e function ($value, $to) { // Custom callback function for difficult conversion\n                    if ($to == 'bit') {\n                         return $value * 8;\n                    }\n                    return $value / 8;\n                },\n            ),\n        );\n    }\n}\n```\n\nUsage example for our information type\n```php\n// create config object\n$config = new ConfigInfo();\n\n// you can register default config for all info-objects,\nConfig::registerDefault('info', $config);\n$info1 = new Info('700 MB');\n$info2 = new Info('1.4 GB');\n\n// or add config object manually\n$info1 = new Info('700 MB', $config);\n$info2 = new Info('1.4 GB', $config);\n\n// Well... some calculations\necho $info2-\u003esubtract($info1)-\u003edump() . PHP_EOL;\necho $info2-\u003econvert('mb')-\u003edump() . PHP_EOL;\nprint_r($info2-\u003elogs());\n```\n\nOutput\n```\n0.71640625 gb; id=4\n733.6 mb; id=4\nArray\n(\n    [0] =\u003e Id=4 has just created; dump=\"1.4 gb\"\n    [1] =\u003e Subtract \"700 mb\"; New value = \"0.71640625 gb\"\n    [2] =\u003e Converted \"gb\"=\u003e\"mb\"; New value = \"733.6 mb\"; 1 gb = 1024 mb\n)\n```\n\n### Debug information\nShow list of all actions with object. For example, this is history for chaining code\n```php\nprint_r($value-\u003elogs());\n\n/**\n * Array\n * (\n *     [0]  =\u003e Id=16 has just created; dump=\"4.95 usd\"\n *     [1]  =\u003e Add \"10 usd\"; New value = \"14.95 usd\"\n *     [2]  =\u003e Subtract \"2 eur\"; New value = \"10.95 usd\"\n *     [3]  =\u003e Set negative; New value = \"-10.95 usd\"\n *     [4]  =\u003e Cloned from id=16 and created new with id=19; dump=-10.95 usd\n *     [5]  =\u003e Division with \"5\"; New value = \"-2.19 usd\"\n *     [6]  =\u003e Multiply with \"10\"; New value = \"-21.9 usd\"\n *     [7]  =\u003e Converted \"usd\"=\u003e\"eur\"; New value = \"-10.95 eur\"; 1 usd = 0.5 eur\n *     [8]  =\u003e --\u003e Function start\n *     [9]  =\u003e Add \"600 rub\"; New value = \"1.05 eur\"\n *     [10] =\u003e Add \"-500 %\"; New value = \"-4.2 eur\"\n *     [11] =\u003e \u003c-- Function finished; New value = \"-4.2 eur\"\n *     [12] =\u003e Set positive/abs; New value = \"4.2 eur\"\n * )\n */\n```\n\nShow real inner data without any formating and rounding. ID is unique number for SimpleType objects.\n```php\necho $value-\u003edump(); // \"4.2 eur; id=19\"\n```\n\nGet object id\n```php\necho $value-\u003egetId(); // \"19\"\n```\nShow current value\n```php\necho $value-\u003eval(); // \"4.2\"\n```\n\nShow current rule\n```php\necho $value-\u003erule(); // \"eur\"\n```\n\n## License\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjbzoo%2Fsimpletypes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjbzoo%2Fsimpletypes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjbzoo%2Fsimpletypes/lists"}