Releases: sagittaracc/decorator
Releases · sagittaracc/decorator
v4.3.3
- add
call_decorator_func_arraymethod to call a method with its decorators
v4.3.2
Validator refactor
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;
}Validator examples
#[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;
}
}v4.3.1
Validation
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]];
}v4.3.0
validation refactor
v4.2.2
- Validation support
Example:
class Request
{
use Decorator;
#[UInt8]
protected $uid;
}Validator
#[Attribute]
final class UInt8 extends Validator
{
public function validation($value)
{
return $value >= 0 && $value <= 255;
}
}Usage
$request = new Request;
$request->uid = 255; // it's fine
$request->uid = 512; // throws an exception `Request::uid validation error! 512 is not UInt8!`v4.2.1
- Refactoring
- Добавил функции хелперы чтобы можно было управлять именами методов или свойств чтобы обращаться к ним с применением декораторов или без
$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 decoratorsv4.2.0
Разделение Python декораторов и Php декораторов. Последние не обязаны быть атрибутом
v4.1.0
- Add decorator on demand
$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
}v4.0.0
Переработан 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
v3.0.2
- В цепочке атрибутов для проперти только самый первый атрибут отнаследованный НЕ ОТ PythonDecorator может быть просвоен этому проперти как инстанс