{"id":15068625,"url":"https://github.com/leongrdic/php-smplang","last_synced_at":"2025-07-06T15:04:00.191Z","repository":{"id":37978256,"uuid":"490283985","full_name":"leongrdic/php-smplang","owner":"leongrdic","description":"simple expression language written in PHP that executes code in an isolated environment","archived":false,"fork":false,"pushed_at":"2023-11-20T22:45:59.000Z","size":73,"stargazers_count":16,"open_issues_count":3,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-28T19:14:51.073Z","etag":null,"topics":["eval","evaluation","evaluator","expression","expression-evaluator","expression-language","language","php","php8","sandbox"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leongrdic.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.md","contributing":null,"funding":null,"license":null,"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":"2022-05-09T12:58:19.000Z","updated_at":"2025-03-03T11:45:32.000Z","dependencies_parsed_at":"2024-06-21T14:27:16.029Z","dependency_job_id":null,"html_url":"https://github.com/leongrdic/php-smplang","commit_stats":{"total_commits":55,"total_committers":4,"mean_commits":13.75,"dds":0.3090909090909091,"last_synced_commit":"60b8f6877318b7289c53107830202944dc8b54c3"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/leongrdic/php-smplang","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leongrdic%2Fphp-smplang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leongrdic%2Fphp-smplang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leongrdic%2Fphp-smplang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leongrdic%2Fphp-smplang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leongrdic","download_url":"https://codeload.github.com/leongrdic/php-smplang/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leongrdic%2Fphp-smplang/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263922469,"owners_count":23530334,"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":["eval","evaluation","evaluator","expression","expression-evaluator","expression-language","language","php","php8","sandbox"],"created_at":"2024-09-25T01:38:36.915Z","updated_at":"2025-07-06T15:04:00.169Z","avatar_url":"https://github.com/leongrdic.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SMPLang\n\n[![release](http://poser.pugx.org/leongrdic/smplang/v)](https://packagist.org/packages/leongrdic/smplang)\n[![php-version](http://poser.pugx.org/leongrdic/smplang/require/php)](https://packagist.org/packages/leongrdic/smplang)\n[![license](http://poser.pugx.org/leongrdic/smplang/license)](https://packagist.org/packages/leongrdic/smplang)\n[![run-tests](https://github.com/leongrdic/php-smplang/actions/workflows/run-tests.yml/badge.svg)](https://github.com/leongrdic/php-smplang/actions/workflows/run-tests.yml)\n\n[![try](https://img.shields.io/badge/Try%20it%20out-on%20PHPSandbox-%237E29CE)](https://play.phpsandbox.io/leongrdic/smplang?input=%24smpl%20%3D%20new%20%5CLe%5CSMPLang%5CSMPLang%28%5B%0A%20%20%27foo%27%20%3D%3E%205%0A%5D%29%3B%0A%0A%24result%20%3D%20%24smpl-%3Eevaluate%28%27%281%20%2B%20foo%20%2A%204%29%20%2F%207%27%29%3B%0A%0Aprint_r%28%24result%29%3B)\n\n\nSMPLang is a simple expression language written in PHP. It can be considered similar to PHP's native `eval()` function but SMPLang has its own syntax and the expressions are evaluated in sort-of a sandbox with access to only vars and functions/closures that you pass into it.\n\nThe language is partly inspired by Symfony Expression Language but there are some major differences like array unpacking, named arguments and easier function definition, thus SMPLang may not be a replacement for some use cases.\n\nInstall:\n```\ncomposer require leongrdic/smplang\n```\n\nTo use SMPLang, create a new instance of the `\\Le\\SMPLang\\SMPLang` class and pass in an associative array of vars that you want to use in your expressions.\n\n```php\n$smpl = new \\Le\\SMPLang\\SMPLang([\n    'variableName' =\u003e 'value',\n]);\n```\n\nYou can then call the `evaluate()` method on the instance, passing in an expression string. This will return the result of the expression.\n```php\n$result = $smpl-\u003eevaluate($expression);\n```\n\nOptionally, you can pass in an associative array to the second parameter to provide additional local vars to use in the expression:\n```php\n$result = $smpl-\u003eevaluate($expression, [\n    'localVariable' =\u003e 123,\n]);\n```\n\nVars passed this way will override vars passed in the constructor and will only be available in this specific expression, in case multiple expressions are evaluated from the same object.\n\n\nIn case of an exception, `\\Le\\SMPLang\\Exception` will be thrown with a short description of the error.\n\n## Expression syntax\n\nVars are accessed by only their name. If a var is not defined in neither constructor or `evaluate()`, an exception will be thrown.\n\n### Supported literals\n- `null`\n- booleans (`true` and `false`)\n- strings (`\"string\"` or `'string'` or \u003ccode\u003e\\`string\\`\u003c/code\u003e)\n- numbers (`1`, `1.2`, `-1`, `-1.2`)\n- arrays (`[23, 'string']` or `[\"key\": 23, 'key2': 'string']`)\n- objects (`{foo: \"bar\", baz: 23}`)\n\n\n### Arrays\n\nArray definition: `[element1, element2]`\n\nAssociative array definition: `[\"key\": element, string_variable: element2]`\n\nYou can define associative arrays with dynamic keys by using string vars in place of keys.\n\nArray unpacking is supported: `[element1, ...array, ...array2]`\n\nAccess array elements using either of the following syntaxes: `array.key.0` or `array['key'][0]` (which allows for dynamic array access).\n\n### Objects\n\nObject definition: `{foo: \"bar\", baz: 23}` (supports array unpacking)\n\nObject property access: `object.property`\n\nObject method call: `object.method(parameters)`\n\n### Function / closure call\n\nCall a function or closure var: `closure_var(param1, param2)`.\n\nNamed arguments: `foo(search: value, count: 1)`.\n\nFunction / closure calls support array unpacking: `bar(param1, ...array, ...array2)`\n\n\n\n### Arithmetic operators\n- `+`: addition\n- `-`: subtraction\n- `*`: multiplication\n- `/`: division\n- `%`: modulo (`a*b%c*d == (a*b)%(c*d)`)\n- `**`: exponentiation (`a*b**c*d == (a*b)**(c*d)`)\n\n### Comparison operators\n- `===`: strict equality\n- `!==`: strict inequality\n- `==`: equality\n- `!=`: inequality\n- `\u003e`: greater than\n- `\u003c`: less than\n- `\u003e=`: greater than or equal to\n- `\u003c=`: less than or equal to\n\n### Logical operators\n- `\u0026\u0026`: logical and\n- `||`: logical or\n- `!`: logical not\n\n### String concatenation\n- `~`: string concatenation\n\n### Ternary expressions\n- `a ? b : c` \n- `a ?: b` (is equivalent to `a ? a : b`)\n- `a ? b` (is equivalent to `a ? b : null`)\n\n\n## Examples\n```php\n$smpl = new \\Le\\SMPLang\\SMPLang();\n$result = $smpl-\u003eevaluate('(1 + 2 * 3) / 7');\n// $result will be 1\n```\n[![try](https://img.shields.io/badge/Try%20it%20out-on%20PHPSandbox-%237E29CE)](https://play.phpsandbox.io/leongrdic/smplang?input=%24smpl%20%3D%20new%20%5CLe%5CSMPLang%5CSMPLang%28%29%3B%0A%24result%20%3D%20%24smpl-%3Eevaluate%28%27%281%20%2B%202%20%2A%203%29%20%2F%207%27%29%3B%0Aprint_r%28%24result%29%3B)\n\n\n```php\n$smpl = new \\Le\\SMPLang\\SMPLang([\n    'foo' =\u003e 'bar',\n    'arr' =\u003e [1, 2, 3],\n    'hash' =\u003e ['a' =\u003e 'b'],\n]);\n\n$result = $smpl-\u003eevaluate('foo ~ \" \" ~ arr[1] ~ \" \" ~ hash.a');\n// $result will be \"bar 2 b\"\n```\n[![try](https://img.shields.io/badge/Try%20it%20out-on%20PHPSandbox-%237E29CE)](https://play.phpsandbox.io/leongrdic/smplang?input=%24smpl%20%3D%20new%20%5CLe%5CSMPLang%5CSMPLang%28%5B%0A%20%20%20%20%27foo%27%20%3D%3E%20%27bar%27%2C%0A%20%20%20%20%27arr%27%20%3D%3E%20%5B1%2C%202%2C%203%5D%2C%0A%20%20%20%20%27hash%27%20%3D%3E%20%5B%27a%27%20%3D%3E%20%27b%27%5D%2C%0A%5D%29%3B%0A%0A%24result%20%3D%20%24smpl-%3Eevaluate%28%27foo%20~%20%22%20%22%20~%20arr%5B1%5D%20~%20%22%20%22%20~%20hash.a%27%29%3B%0Aprint_r%28%24result%29%3B)\n\n\n```php\n$smpl = new \\Le\\SMPLang\\SMPLang([\n    'prepend' =\u003e fn(string $a): string =\u003e \"hello $a\",\n    'reverse' =\u003e strrev(...),\n]);\n\n$result = $smpl-\u003eevaluate('prepend(\"simple \" ~ reverse(\"world\"))');\n// $result will be \"hello simple dlrow\"\n```\n[![try](https://img.shields.io/badge/Try%20it%20out-on%20PHPSandbox-%237E29CE)](https://play.phpsandbox.io/leongrdic/smplang?input=%24smpl%20%3D%20new%20%5CLe%5CSMPLang%5CSMPLang%28%5B%0A%20%20%20%20%27prepend%27%20%3D%3E%20fn%28string%20%24a%29%3A%20string%20%3D%3E%20%22hello%20%24a%22%2C%0A%20%20%20%20%27reverse%27%20%3D%3E%20strrev%28...%29%2C%0A%5D%29%3B%0A%0A%24result%20%3D%20%24smpl-%3Eevaluate%28%27prepend%28%22simple%20%22%20~%20reverse%28%22world%22%29%29%27%29%3B%0Aprint_r%28%24result%29%3B)\n\n\n```php\n$smpl = new \\Le\\SMPLang\\SMPLang([\n    'foo' =\u003e 'bar',\n]);\n\n$result = $smpl-\u003eevaluate('foo !== \"bar\" ? \"yes\" : \"no\"');\n// $result will be \"no\"\n```\n[![try](https://img.shields.io/badge/Try%20it%20out-on%20PHPSandbox-%237E29CE)](https://play.phpsandbox.io/leongrdic/smplang?input=%24smpl%20%3D%20new%20%5CLe%5CSMPLang%5CSMPLang%28%5B%0A%20%20%20%20%27foo%27%20%3D%3E%20%27bar%27%2C%0A%5D%29%3B%0A%0A%24result%20%3D%20%24smpl-%3Eevaluate%28%27foo%20%21%3D%3D%20%22bar%22%20%3F%20%22yes%22%20%3A%20%22no%22%27%29%3B%0Aprint_r%28%24result%29%3B)\n\n\n```php\n// SMPL can even be used as an (incredibly slow) JSON parser!\n\n$smpl = new \\Le\\SMPLang\\SMPLang();\n$json = '{ \"foo\": null, \"bar\": 15.23, \"baz\": true, \"arr\": [5, 6], \"str\": \"cool\" }';\n$result = $smpl-\u003eevaluate($json);\nprint_r($result);\n```\n[![try](https://img.shields.io/badge/Try%20it%20out-on%20PHPSandbox-%237E29CE)](https://play.phpsandbox.io/leongrdic/smplang?input=%3C%3Fphp%0A%24smpl%20%3D%20new%20%5CLe%5CSMPLang%5CSMPLang%28%29%3B%0A%24json%20%3D%20%27%7B%20%22foo%22%3A%20null%2C%20%22bar%22%3A%2015.23%2C%20%22baz%22%3A%20true%2C%20%22arr%22%3A%20%5B5%2C%206%5D%2C%20%22str%22%3A%20%22cool%22%20%7D%27%3B%0A%24result%20%3D%20%24smpl-%3Eevaluate%28%24json%29%3B%0Aprint_r%28%24result%29%3B)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleongrdic%2Fphp-smplang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleongrdic%2Fphp-smplang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleongrdic%2Fphp-smplang/lists"}