{"id":15030224,"url":"https://github.com/sagittaracc/decorator","last_synced_at":"2025-04-09T20:40:47.183Z","repository":{"id":65202909,"uuid":"587261027","full_name":"sagittaracc/decorator","owner":"sagittaracc","description":"Python style decorator for PHP","archived":false,"fork":false,"pushed_at":"2024-11-19T19:00:48.000Z","size":324,"stargazers_count":12,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-23T22:37:00.656Z","etag":null,"topics":["declarative-programming","decorators","generic","generic-library","generic-types","generics","patterns","php","php-attribute","php-attributes","php-generics","php-library","php8","python","rpc","rpc-api","rpc-server","rpc-service","validation","validator"],"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/sagittaracc.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-10T10:40:41.000Z","updated_at":"2024-11-19T19:00:52.000Z","dependencies_parsed_at":"2024-04-03T15:55:42.394Z","dependency_job_id":"a49909ef-99ff-45b4-b24e-5fb180dea0b8","html_url":"https://github.com/sagittaracc/decorator","commit_stats":{"total_commits":217,"total_committers":3,"mean_commits":72.33333333333333,"dds":"0.37788018433179726","last_synced_commit":"44e9a51fac870392e00da5158b84d19093e0b109"},"previous_names":[],"tags_count":47,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagittaracc%2Fdecorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagittaracc%2Fdecorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagittaracc%2Fdecorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagittaracc%2Fdecorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sagittaracc","download_url":"https://codeload.github.com/sagittaracc/decorator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248109631,"owners_count":21049349,"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":["declarative-programming","decorators","generic","generic-library","generic-types","generics","patterns","php","php-attribute","php-attributes","php-generics","php-library","php8","python","rpc","rpc-api","rpc-server","rpc-service","validation","validator"],"created_at":"2024-09-24T20:12:48.451Z","updated_at":"2025-04-09T20:40:47.166Z","avatar_url":"https://github.com/sagittaracc.png","language":"PHP","readme":"# PHP Python Decorator\nPython style decorators for PHP\n\n# Requirements\nPHP 8.1 or higher\n\n# Install\n`composer require sagittaracc/php-python-decorator`\n\n# Example\nHow long it takes to run a method. See the [`Timer`](https://github.com/sagittaracc/php-python-decorator/blob/main/tests/decorators/Timer.php) decorator\n```php\n\nuse Sagittaracc\\PhpPythonDecorator\\Decorator;\n\nclass Calc\n{\n    use Decorator;\n\n    #[Timer]\n    function sum($a, $b)\n    {\n        sleep(1);\n        return $a + $b;\n    }\n}\n```\nThis is how you can call it\n```php\n$calc = new Calc();\necho call_decorator_func_array([$calc, 'sum'], [1, 2]); // Total execution: 1.00034234 ms; Result: 3\n```\nOr inline\n```php\n$timerOnSum = (new Timer)-\u003ewrapper(fn($a, $b) =\u003e $calc-\u003esum($a, $b));\necho $timerOnSum(1, 2); // Total execution: 1.00034234 ms; Result: 3\n```\n\n# Generics\n```php\nuse Sagittaracc\\PhpPythonDecorator\\Decorator;\nuse Sagittaracc\\PhpPythonDecorator\\modules\\generics\\aliases\\T;\nuse Sagittaracc\\PhpPythonDecorator\\modules\\validation\\core\\validators\\ArrayOf;\n\n#[T]\nclass Box\n{\n    use Decorator;\n\n    #[ArrayOf(T::class)]\n    public $items;\n\n    public function addItem(#[T] $item)\n    {\n        $this-\u003eitems[] = $item;\n    }\n}\n\n$box = new Box();\n$box(Pen::class); // new Box\u003cPen\u003e();\ncall_decorator_func_array([$box, 'addItem'], [new Pencil]); // throws a GenericError\n```\n\n# Validation\n```php\n\nuse Sagittaracc\\PhpPythonDecorator\\Decorator;\nuse Sagittaracc\\PhpPythonDecorator\\tests\\examples\\Progress;\nuse Sagittaracc\\PhpPythonDecorator\\tests\\validators\\Length;\nuse Sagittaracc\\PhpPythonDecorator\\tests\\validators\\SerializeOf;\nuse Sagittaracc\\PhpPythonDecorator\\tests\\validators\\In;\nuse Sagittaracc\\PhpPythonDecorator\\tests\\validators\\LessThan;\nuse Sagittaracc\\PhpPythonDecorator\\tests\\validators\\UInt8;\n\nclass Progress\n{\n    use Decorator;\n\n    #[UInt8]\n    public $max;\n\n    #[UInt8]\n    #[LessThan('max')]\n    public $pos;\n\n    #[In('progress', 'finish', 'aborted')]\n    public $status;\n\n    #[Length(32)]\n    public string $caption;\n}\n\n$progress = new Progress();\n\nset_decorator_prop($progress, 'max', 255);  // max uint8 - 255\nset_decorator_prop($progress, 'pos', 100);  // should be less than max\nset_decorator_prop($progress, 'status', 'progress');  // status is one of possible cases (progress, finish or aborted)\nset_decorator_prop($progress, 'caption', 'in progress ...');  // just a string (max length is 32)\n```\n\n# Rpc\n```php\nuse Sagittaracc\\PhpPythonDecorator\\Decorator;\nuse Sagittaracc\\PhpPythonDecorator\\modules\\rpc\\core\\Rpc;\n\n#[Rpc]\nclass Controller\n{\n    use Decorator;\n\n    public function sum($a, $b)\n    {\n        return $a + $b;\n    }\n}\n```\n\nin `index.php`\n\n```php\n$requestBody = file_get_contents('php://input');\n\n$controller = new Controller();\n$controller($requestBody);\n```\n\nin `terminal`\n\n```console\n$ curl -d \"{\"id\":1,\"method\":\"sum\",\"params\":[1,4]}\" http://localhost:4000\n{\"json-rpc\":\"2.0\",\"id\":1,\"result\":5}\n```\n\n# Console\n```php\nuse Sagittaracc\\PhpPythonDecorator\\Decorator;\nuse Sagittaracc\\PhpPythonDecorator\\modules\\console\\core\\Console;\n\nclass Controller\n{\n    use Decorator;\n\n    #[Console('hello')]\n    function greetingPerson($name)\n    {\n        return \"Hello, $name\";\n    }\n}\n```\n\nin the command line it would be calling for example something like this:\n\n`php index.php -c hello --name Yuriy`\n\nthen in `index.php` you should read the command and the parameters and after that call it like this:\n\n```php\n(new Console('hello'))-\u003esetParameters(['name' =\u003e 'Yuriy'])-\u003egetMethod(Controller::class)-\u003erun();\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsagittaracc%2Fdecorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsagittaracc%2Fdecorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsagittaracc%2Fdecorator/lists"}