Bot releases are hidden (Show)
Published by sagittaracc 6 months ago
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<Pen>();
call_decorator_func_array([$box, 'addItem'], [new Pencil]); // throws a GenericError
Published by sagittaracc 6 months ago
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:
(new Console('hello'))->setParameters(['name' => 'Yuriy'])->getMethod(Controller::class)->run();
Done generics refactor
Published by sagittaracc 6 months ago
Done generics refactor
Published by sagittaracc 6 months ago
use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\modules\generics\aliases\T;
#[T]
class PaymentInfo
{
use Decorator;
public string $id;
public int $amount;
#[T]
public $currency;
}
$paymentInfo = new PaymentInfo();
$paymentInfo(Number::class); // new PaymentInfo<Number>();
set_decorator_prop($paymentInfo, 'currency', 'rubles'); // throws a GenericError
Published by sagittaracc 7 months ago
use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\T;
#[T]
class Box
{
use Decorator;
#[T]
public array $items;
}
$box = new Box();
$box(Pencil::class); // new Box<Pencil>();
$pencil = new Pencil();
$pen = new Pen();
$box->_items = [$pencil, $pen]; // throws a GenericError
Published by sagittaracc 7 months ago
Published by sagittaracc 7 months ago
Published by sagittaracc 7 months ago
Pass the arguments of a method into the decorator wrappers
Published by sagittaracc about 1 year ago
call_decorator_func_array
method to call a method with its decoratorsPublished by sagittaracc about 1 year ago
class Request
{
use Decorator;
#[Length(8)]
public string $name;
#[Length(32)]
public string $caption;
#[SerializeOf(Progress::class)]
public array $progress;
#[SerializeOf(DataTable::class)]
public array $data;
}
class Progress
{
use Decorator;
#[UInt8]
public $max;
#[UInt8]
#[LessThan('max')]
public $pos;
#[In('progress', 'finish', 'aborted')]
public $status;
#[Length(32)]
public string $caption;
}
class DataTable
{
use Decorator;
#[ArrayOf(Str::class)]
public array $header;
#[Table]
public array $table;
}
#[Attribute]
final class In extends Validator
{
public array $in;
function __construct(...$in)
{
$this->in = $in;
}
public function validation($value)
{
return in_array($value, $this->in);
}
}
#[Attribute]
final class LessThan extends Validator
{
function __construct(
public $supreme
)
{}
public function validation($value)
{
$supreme = $this->supreme;
return $value <= $this->getObject()->$supreme;
}
}
#[Attribute]
final class Table extends Validator
{
public function validation($value)
{
if (isset($value['ins']) && !$this->validateIns($value['ins'])) {
return false;
}
return true;
}
private function validateIns($ins)
{
if (!is_array($ins)) {
return false;
}
if (!array_is_list($ins)) {
return false;
}
$colCount = count($this->getObject()->header);
foreach ($ins as $row) {
if (!is_array($row)) {
return false;
}
if (!array_is_list($row)) {
return false;
}
if (count($row) !== $colCount) {
return false;
}
}
return true;
}
}
Published by sagittaracc over 1 year ago
use Sagittaracc\PhpPythonDecorator\Decorator;
class Request
{
use Decorator;
#[Int8]
public $id;
#[UInt8]
public $uid;
#[Str]
#[Length(5)]
public $method;
#[ArrayOf(UInt8::class)]
public array $params = [1, 2, 3];
#[SerializeOf(User::class)]
public array $user = ['id' => 1];
#[SerializeArrayOf(User::class)]
public array $userList = [['id' => 1], ['id' => 2]];
}
Published by sagittaracc over 1 year ago
Published by sagittaracc over 1 year ago
class Request
{
use Decorator;
#[UInt8]
protected $uid;
}
#[Attribute]
final class UInt8 extends Validator
{
public function validation($value)
{
return $value >= 0 && $value <= 255;
}
}
$request = new Request;
$request->uid = 255; // it's fine
$request->uid = 512; // throws an exception `Request::uid validation error! 512 is not UInt8!`
Published by sagittaracc over 1 year ago
$calc = new Calc();
$calc->{get_decor_name('sum')}(1, 2); //It will generate a decorated rule name for the sum method so it will apply its decorators
Published by sagittaracc over 1 year ago
Разделение Python
декораторов и Php
декораторов. Последние не обязаны быть атрибутом
Published by sagittaracc over 1 year ago
$needExtraRoom = true;
$needWifi = true;
$room = new Booking();
$price = $room->getPrice(); // 1000
if ($needExtraRoom) {
$price = (new ExtraRoom)->decorate($price); // (2000) Having an extra room doubles the price
}
if ($needWifi) {
$price = (new Wifi)->decorate($price); // (20000) Having wifi in the room makes it ten times expensive
}
Published by sagittaracc over 1 year ago
Переработан PhpAttribute
(Удалены методы runIn
, getFrom
, equalTo
)
old syntax: (new Route('/hello'))->runIn(Controller::class)
new syntax: (new Route('/hello'))->getMethod(Controller::class)->run()
old syntax: (new Primary)->getFrom(Controller::class)
new syntax: (new Primary)->getProperty(Controller::class)
rename equalTo
-> matchTo
Published by sagittaracc over 1 year ago
Published by sagittaracc over 1 year ago