mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Optimized code for CastsValue (#2680)
Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
parent
79f810d3af
commit
d76d1f7a16
@ -4,6 +4,11 @@
|
||||
|
||||
- [#2682](https://github.com/hyperf/hyperf/pull/2682) Added method `getCacheTTL` for `CacheableInterface` which can control cache time each models.
|
||||
|
||||
## Fixed
|
||||
|
||||
- [#2680](https://github.com/hyperf/hyperf/pull/2680) Fixed Type error for `CastsValue`, because `$isSynchronized` don't have default value.
|
||||
- [#2680](https://github.com/hyperf/hyperf/pull/2680) Fixed default value in `$items` will be replaced by `__construct` for `CastsValue`.
|
||||
|
||||
## Optimized
|
||||
|
||||
- [#2611](https://github.com/hyperf/hyperf/pull/2611) Optimized `FindDriver` for watcher, you can use it in alpine image.
|
||||
|
@ -24,30 +24,39 @@ abstract class CastsValue implements Synchronized, Arrayable
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $items;
|
||||
protected $items = [];
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $isSynchronized;
|
||||
protected $isSynchronized = false;
|
||||
|
||||
public function __construct(Model $model, $itmes = [])
|
||||
public function __construct(Model $model, $items = [])
|
||||
{
|
||||
$this->model = $model;
|
||||
$this->items = $itmes;
|
||||
$this->items = array_merge($this->items, $items);
|
||||
}
|
||||
|
||||
public function __get($name)
|
||||
{
|
||||
return $this->items[$name];
|
||||
return $this->items[$name] ?? null;
|
||||
}
|
||||
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$this->items[$name] = $value;
|
||||
$this->isSynchronized = false;
|
||||
$this->model->syncAttributes();
|
||||
$this->isSynchronized = true;
|
||||
$this->syncAttributes();
|
||||
}
|
||||
|
||||
public function __isset($name)
|
||||
{
|
||||
return isset($this->items[$name]);
|
||||
}
|
||||
|
||||
public function __unset($name)
|
||||
{
|
||||
unset($this->items[$name]);
|
||||
$this->syncAttributes();
|
||||
}
|
||||
|
||||
public function isSynchronized(): bool
|
||||
@ -59,4 +68,11 @@ abstract class CastsValue implements Synchronized, Arrayable
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
public function syncAttributes(): void
|
||||
{
|
||||
$this->isSynchronized = false;
|
||||
$this->model->syncAttributes();
|
||||
$this->isSynchronized = true;
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,8 @@ class DatabaseModelCustomCastingTest extends TestCase
|
||||
protected function tearDown()
|
||||
{
|
||||
\Mockery::close();
|
||||
UserInfoCaster::$setCount = 0;
|
||||
UserInfoCaster::$getCount = 0;
|
||||
}
|
||||
|
||||
public function testBasicCustomCasting()
|
||||
@ -215,16 +217,32 @@ class DatabaseModelCustomCastingTest extends TestCase
|
||||
$model->syncOriginal();
|
||||
|
||||
$attributes = $model->getAttributes();
|
||||
$this->assertSame(['name' => 'Hyperf', 'gender' => 1], $attributes);
|
||||
$this->assertSame(['name' => 'Hyperf', 'gender' => 1], Arr::only($attributes, ['name', 'gender']));
|
||||
|
||||
$user->name = 'Nano';
|
||||
$attributes = $model->getAttributes();
|
||||
$this->assertSame(['name' => 'Nano', 'gender' => 1], $attributes);
|
||||
$this->assertSame(['name' => 'Nano', 'gender' => 1], Arr::only($attributes, ['name', 'gender']));
|
||||
|
||||
$this->assertSame(['name' => 'Nano'], $model->getDirty());
|
||||
$this->assertSame(2, UserInfoCaster::$setCount);
|
||||
$this->assertSame(0, UserInfoCaster::$getCount);
|
||||
}
|
||||
|
||||
public function testCastsValueSupportNull()
|
||||
{
|
||||
$model = new TestModelWithCustomCast();
|
||||
$model->user = $user = new UserInfo($model, ['name' => 'Hyperf', 'gender' => 1]);
|
||||
$attributes = $model->getAttributes();
|
||||
$this->assertSame(['name' => 'Hyperf', 'gender' => 1, 'role_id' => 0], $attributes);
|
||||
$this->assertSame(0, $user->role_id);
|
||||
$user->role_id = 1;
|
||||
$this->assertSame(['name' => 'Hyperf', 'gender' => 1, 'role_id' => 1], $model->getAttributes());
|
||||
unset($user->role_id);
|
||||
$this->assertSame(['name' => 'Hyperf', 'gender' => 1, 'role_id' => null], $model->getAttributes());
|
||||
$this->assertSame(null, $user->role_id);
|
||||
unset($user->not_found);
|
||||
$this->assertSame(['name' => 'Hyperf', 'gender' => 1, 'role_id' => null], $model->getAttributes());
|
||||
}
|
||||
}
|
||||
|
||||
class TestModelWithCustomCast extends Model
|
||||
@ -325,6 +343,7 @@ class UserInfoCaster implements CastsAttributes
|
||||
return [
|
||||
'name' => $value->name,
|
||||
'gender' => $value->gender,
|
||||
'role_id' => $value->role_id,
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -416,7 +435,11 @@ class Address
|
||||
/**
|
||||
* @property string $name
|
||||
* @property int $gender
|
||||
* @property null|int $role_id
|
||||
*/
|
||||
class UserInfo extends CastsValue
|
||||
{
|
||||
protected $items = [
|
||||
'role_id' => 0,
|
||||
];
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user