属性会通过定义的数据类型,自动转换
php: ^8.0
$ composer require kabunx/hydrate -vvv
<?php
declare(strict_types=1);
namespace App\Entities;
use Carbon\Carbon;
use Kabunx\Hydrate\Entity;
use Kabunx\Hydrate\Column;
/**
* Class UserEntity
* @package App\Entities
*/
class UserEntity extends Entity
{
public string $name = '';
public string $email = '';
public int $gender = 0;
public string $birthday = '';
public ?Carbon $createdAt;
#[Column(source: "modifiedAt")]
public ?Carbon $updatedAt;
/**
* @param string $value
* @return int
*/
public function setGender(string $value): int
{
return $value == 'm' ? 0 : 1;
}
}
<?php
use App\Entities\UserEntity;
$users = [
'id' => 1,
'name' => 'test01',
'email' => '[email protected]',
'gender' => 'm',
'birthday' => '2000-10-01',
'createdAt' => '2021-07-23',
'modifiedAt' => '2021-07-23 12:05:10'
];
UserEntity::instance($users);