PHP code example of hsldymq / velcro

1. Go to this page and download the library: Download hsldymq/velcro library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hsldymq / velcro example snippets




use Archman\Velcro\Converters\DateTimeImmutableConverter;
use Archman\Velcro\DataModel;
use Archman\Velcro\Field;
use Archman\Velcro\RO;
 
class Foo extends DataModel
{
    #[Field('f1')]
    public int $p1;

    #[Field('f2'), DateTimeImmutableConverter(DateTimeImmutableConverter::ISO_8601)]
    public DateTimeImmutable $p2;

    #[Field('f3'), RO]
    public string $p3;
    
    #[Field('f4')]
    public readonly string $p4;
}

$foo = new Foo([
    'f1' => 123,
    'f2' => '2021-01-01T00:00:00',
    'f3' => 'value for readonly field',
    'f4' => 'value for PHP 8.1 readonly field'
]);

assert($foo->p1 === 123);
assert($foo->p2->format('Y-m-d H:i:s') === '2021-01-01 00:00:00');
assert($foo->p3 === 'value for readonly field');
assert($foo->p4 === 'value for PHP 8.1 readonly field');
$foo->p3 = 'new value'; // It throws an exception.



use Archman\Velcro\DataModel;
use Archman\Velcro\Field;

class Foo extends DataModel
{
    #[Field('field')]
    public int $bar;
}

$foo = new Foo(['field' => 1]);

doSomething($foo->bar);



class Foo
{
    public int $bar; 

    public function __construct(array $data)
    {
        if (array_key_exists('field', $data)) {
            $this->bar = $data['field'];
        }
    }
}



use Archman\Velcro\Converters\DateTimeImmutableConverter;
use Archman\Velcro\DataModel;
use Archman\Velcro\Field;

class Response extends DataModel
{
    #[Field('time')]
    #[DateTimeImmutableConverter(DateTimeImmutableConverter::TIMESTAMP)]
    public DateTimeImmutable $datetime;
}
new Response(['time' => 1609430400]);



use Archman\Velcro\DataModel;
use Archman\Velcro\Field;
use Archman\Velcro\Converters\ModelConverter;
use Archman\Velcro\Converters\ModelListConverter;

// 假设你有这样一套数据:
$data = [
    "students" => [
        [
          "name" => "Alice",
          "age" => 8,
        ],
        's' => [
          "name" => "Bob",
          "age" => 10,
        ]
    ],
    "school" => [
        "name" => "xxx",
        "address" => "yyy",
    ],
];

// 你对students和school各自建立数据模型

class Info extends DataModel
{
    /** @var Student[] */
    #[Field('students')]
    #[ModelListConverter(Student::class)]
    public array $studentList;
    
    #[Field('school')]
    #[ModelConverter]
    public School $school;
}

class Student extends DataModel
{
    #[Field('name')]
    public string $name;
    
    #[Field('age')]
    public int $age;
}

class School extends DataModel
{
    #[Field('name')]
    public string $name;
    
    #[Field('address')]
    public string $address;
}

$info = new Info($data);

assert(count($info->studentList) === 2);
assert($info->studentList[0]->name === 'Alice');
assert($info->studentList['s']->name === 'Bob');
assert($info->school->name === 'xxx');



use Archman\Velcro\DataModel;
use Archman\Velcro\Exceptions\ReadonlyException;
use Archman\Velcro\Field;
use Archman\Velcro\RO;

// 将属性添加RO注解, 会使该属性变为只读
class ConfigA extends DataModel
{
    #[Field('conf1'), RO]
    public string $config1;
    
    #[Field('conf2')]
    public int $config2;
}

$c = new ConfigA([
    'conf1' => 'xxx',
    'conf2' => 111,
]);
try {
    $c->config1 = 'yyy';
} catch(Throwable $e) {
    assert($e::class === ReadonlyException::class);
}
$c->config2 = 222;
assert($c->config2 === 222);


// 当将类添加RO注解, 等同于将其中所有标记了Field都会变为只读
#[RO]
class ConfigB extends DataModel
{
    #[Field('conf1')]
    public string $config1;
    
    #[Field('conf2')]
    public int $config2;
    
    public string $config3;
}

$c = new ConfigB([
    'conf1' => 'xxx',
    'conf2' => 111,
]);
try {
    $c->config1 = 'yyy';
} catch(Throwable $e) {
    assert($e::class === ReadonlyException::class);
}

try {
    $c->config2 = 222;
} catch(Throwable $e) {
    assert($e::class === ReadonlyException::class);
}

$c->config3 = 'xxx'; // 没有标记Field, 不会抛出异常



use Archman\Velcro\DataModel;
use Archman\Velcro\Exceptions\ReadonlyException;
use Archman\Velcro\Field;
use Archman\Velcro\RO;

// 将属性添加RO注解, 会使该属性变为只读
class ConfigX extends DataModel
{
    #[Field('conf1')]       // 它跟config2有相同的效果
    public readonly string $config1;
    
    #[Field('conf2'), RO]
    public int $config2;
}




use Archman\Velcro\DataModel;
use Archman\Velcro\Field;

// 该类拥有一个protected属性和一个private属性
class Foo extends DataModel
{
    #[Field('field1')]
    protected int $val1;
    
    #[Field('field2')]
    private int $val2;
    
    public function assertProps(int $v1, int $v2)
    {
        assert($this->val1 === $v1);
        assert($this->val2 === $v2);
    }
}

$foo = new Foo(['field1' => 1, 'field2' => 2]);
$foo->assertProps(1, 2);