Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sagittaracc/decorator
Python style decorator for PHP
https://github.com/sagittaracc/decorator
declarative-programming decorators generic generic-library generic-types generics patterns php php-attribute php-attributes php-generics php-library php8 python validation validator
Last synced: 2 months ago
JSON representation
Python style decorator for PHP
- Host: GitHub
- URL: https://github.com/sagittaracc/decorator
- Owner: sagittaracc
- License: mit
- Created: 2023-01-10T10:40:41.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-23T11:10:19.000Z (8 months ago)
- Last Synced: 2024-09-28T20:41:03.779Z (3 months ago)
- Topics: declarative-programming, decorators, generic, generic-library, generic-types, generics, patterns, php, php-attribute, php-attributes, php-generics, php-library, php8, python, validation, validator
- Language: PHP
- Homepage:
- Size: 310 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP Python Decorator
Python style decorators for PHP# Requirements
PHP 8.1 or higher# Install
`composer require sagittaracc/php-python-decorator`# Example
How long it takes to run a method. See the [`Timer`](https://github.com/sagittaracc/php-python-decorator/blob/main/tests/decorators/Timer.php) decorator
```phpuse Sagittaracc\PhpPythonDecorator\Decorator;
class Calc
{
use Decorator;#[Timer]
function sum($a, $b)
{
sleep(1);
return $a + $b;
}
}
```
This is how you can call it
```php
$calc = new Calc();
echo call_decorator_func_array([$calc, 'sum'], [1, 2]); // Total execution: 1.00034234 ms; Result: 3
```
Or inline
```php
$timerOnSum = (new Timer)->wrapper(fn($a, $b) => $calc->sum($a, $b));
echo $timerOnSum(1, 2); // Total execution: 1.00034234 ms; Result: 3
```# Generics
```php
use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\modules\generics\aliases\T;
use Sagittaracc\PhpPythonDecorator\modules\validation\core\validators\ArrayOf;#[T]
class Box
{
use Decorator;#[ArrayOf(T::class)]
public $items;public function addItem(#[T] $item)
{
$this->items[] = $item;
}
}$box = new Box();
$box(Pen::class); // new Box();
call_decorator_func_array([$box, 'addItem'], [new Pencil]); // throws a GenericError
```# Validation
```phpuse Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\tests\examples\Progress;
use Sagittaracc\PhpPythonDecorator\tests\validators\Length;
use Sagittaracc\PhpPythonDecorator\tests\validators\SerializeOf;
use Sagittaracc\PhpPythonDecorator\tests\validators\In;
use Sagittaracc\PhpPythonDecorator\tests\validators\LessThan;
use Sagittaracc\PhpPythonDecorator\tests\validators\UInt8;class Progress
{
use Decorator;#[UInt8]
public $max;#[UInt8]
#[LessThan('max')]
public $pos;#[In('progress', 'finish', 'aborted')]
public $status;#[Length(32)]
public string $caption;
}$progress = new Progress();
set_decorator_prop($progress, 'max', 255); // max uint8 - 255
set_decorator_prop($progress, 'pos', 100); // should be less than max
set_decorator_prop($progress, 'status', 'progress'); // status is one of possible cases (progress, finish or aborted)
set_decorator_prop($progress, 'caption', 'in progress ...'); // just a string (max length is 32)
```# Router
```php
class Controller
{
use Decorator;#[Route('/hello')]
#[Route('/hello/(\w+)')]
function greetingPerson($name = 'guest')
{
return "Hello, $name";
}
}// index.php
(new Route('/hello/Yuriy'))->getMethod(Controller::class)->run(); // output: Hello, Yuriy
```# Console
```php
use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\modules\console\core\Console;class Controller
{
use Decorator;#[Console('hello')]
function greetingPerson($name)
{
return "Hello, $name";
}
}
```in the command line it would be calling for example something like this:
`php index.php -c hello --name Yuriy`
then in `index.php` you should read the command and the parameters and after that call it like this:
```php
(new Console('hello'))->setParameters(['name' => 'Yuriy'])->getMethod(Controller::class)->run();
```