Skip to content

Commit

Permalink
实体模型增加hidden/visible/append方法 及字段映射支持
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Nov 19, 2024
1 parent 0604f45 commit 8dca158
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 17 deletions.
143 changes: 131 additions & 12 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public function __construct(array | object $data = [], ?Model $model = null)
'origin' => [],
'schema' => [],
'together' => [],
'hidden' => [],
'visible' => [],
'append' => [],
'mapping' => [],
'strict' => true,
'model' => $model,
];
Expand Down Expand Up @@ -108,6 +112,12 @@ protected function initializeData(array | object $data, bool $fromSave = false)

// 实体模型赋值
foreach ($data as $name => $val) {
if (!empty(self::$weakMap[$this]['mapping'])) {
$key = array_search($name, self::$weakMap[$this]['mapping']);
if (is_string($key)) {
$name = $key;
}
}
$trueName = $this->getRealFieldName($name);
if (in_array($trueName, $fields)) {
// 读取数据后进行类型转换
Expand Down Expand Up @@ -270,7 +280,7 @@ protected function getFields(?string $field = null)
$schema = [];

foreach ($propertys as $property) {
$name = $weakMap['model']->getRealFieldName($property->getName());
$name = $this->getRealFieldName($property->getName());
$type = $property->hasType() ? $property->getType()->getName() : 'string';
$schema[$name] = $type;
}
Expand Down Expand Up @@ -325,6 +335,93 @@ public function together(array $relation)
return $this;
}

/**
* 强制写入或删除
*
* @param bool $force 强制更新
*
* @return $this
*/
public function force(bool $force = true)
{
$this->model()->force($force);

return $this;
}

/**
* 新增数据是否使用Replace.
*
* @param bool $replace
*
* @return $this
*/
public function replace(bool $replace = true)
{
$this->model()->replace($replace);

return $this;
}

/**
* 设置需要附加的输出属性.
*
* @param array $append 属性列表
* @param bool $merge 是否合并
*
* @return $this
*/
public function append(array $append, bool $merge = false)
{
self::$weakMap[$this]['append'] = $merge ? array_merge(self::$weakMap[$this]['append'], $append) : $append;

return $this;
}

/**
* 设置需要隐藏的输出属性.
*
* @param array $hidden 属性列表
* @param bool $merge 是否合并
*
* @return $this
*/
public function hidden(array $hidden, bool $merge = false)
{
self::$weakMap[$this]['hidden'] = $merge ? array_merge(self::$weakMap[$this]['hidden'], $hidden) : $hidden;

return $this;
}

/**
* 设置需要输出的属性.
*
* @param array $visible
* @param bool $merge 是否合并
*
* @return $this
*/
public function visible(array $visible, bool $merge = false)
{
self::$weakMap[$this]['visible'] = $merge ? array_merge(self::$weakMap[$this]['visible'], $visible) : $visible;

return $this;
}

/**
* 设置属性的映射输出.
*
* @param array $map
*
* @return $this
*/
public function mapping(array $map)
{
self::$weakMap[$this]['mapping'] = $map;

return $this;
}

/**
* 字段值增长
*
Expand Down Expand Up @@ -372,7 +469,7 @@ public function save(array | object $data = []): bool

$data = $this->getData();
$origin = $this->getOrigin();
$isUpdate = $this->model()->getKey();
$isUpdate = $this->model()->getKey() && !$this->model()->isForce();

foreach ($data as $name => &$val) {
if ($val instanceof Entity) {
Expand Down Expand Up @@ -505,10 +602,11 @@ public static function create(array | object $data): Entity
* 删除记录.
*
* @param mixed $data 主键列表 支持闭包查询条件
* @param bool $force 是否强制删除
*
* @return bool
*/
public static function destroy($data): bool
public static function destroy($data, bool $force = false): bool
{
if (empty($data) && 0 !== $data) {
return false;
Expand All @@ -532,7 +630,7 @@ public static function destroy($data): bool
$resultSet = $query->select((array) $data);

foreach ($resultSet as $result) {
$result->delete();
$result->force($force)->delete();
}

return true;
Expand Down Expand Up @@ -594,6 +692,12 @@ public function getOrigin(): array
public function toArray(array $allow = []): array
{
$data = $this->getData();
if (empty($allow)) {
$hidden = self::$weakMap[$this]['hidden'];
$visible = self::$weakMap[$this]['visible'];
$allow = array_diff($visible ?: array_keys($data), $hidden);
}

foreach ($data as $name => &$item) {
if (!empty($allow) && !in_array($name, $allow)) {
unset($data[$name]);
Expand All @@ -613,18 +717,20 @@ public function toArray(array $allow = []): array
$this->setData('get', $name, $item);
}
}
}

// 输出额外属性
foreach ($this->getWeakData('get') as $name => $val) {
if (!empty($allow) && !in_array($name, $allow)) {
continue;
if (isset(self::$weakMap[$this]['mapping'][$name])) {
// 检查字段映射
$key = self::$weakMap[$this]['mapping'][$name];
$data[$key] = $data[$name];
unset($data[$name]);
}
}

if (!array_key_exists($name, $data)) {
$data[$name] = $val;
}
// 输出额外属性
foreach (self::$weakMap[$this]['append'] as $key) {
$data[$key] = $this->get($key);
}

return $data;
}

Expand All @@ -648,6 +754,12 @@ public function isEmpty(): bool
*/
public function set(string $name, $value): void
{
if (!empty(self::$weakMap[$this]['mapping'])) {
$key = array_search($name, self::$weakMap[$this]['mapping']);
if (is_string($key)) {
$name = $key;
}
}
$name = $this->getRealFieldName($name);
if ($this->isStrictMode()) {
$this->$name = $value;
Expand All @@ -665,6 +777,11 @@ public function set(string $name, $value): void
*/
public function get(string $name)
{
if (isset(self::$weakMap[$this]['mapping'][$name])) {
// 检查字段映射
$name = self::$weakMap[$this]['mapping'][$name];
}

$name = $this->getRealFieldName($name);
if (array_key_exists($name, self::$weakMap[$this]['get'])) {
return self::$weakMap[$this]['get'][$name];
Expand Down Expand Up @@ -763,10 +880,12 @@ public function __debugInfo()
if (!$this->isStrictMode()) {
return [
'data' => self::$weakMap[$this]['data'],
'origin' => self::$weakMap[$this]['origin'],
'schema' => self::$weakMap[$this]['schema'],
];
} else {
return [
'origin' => self::$weakMap[$this]['origin'],
'schema' => self::$weakMap[$this]['schema'],
];
}
Expand Down
5 changes: 0 additions & 5 deletions src/model/concern/TimeStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ public function isAutoWriteTimestamp($auto)
return $this;
}

public function getDateTimeFields(bool $update = false)
{
return $update ? $this->updateTime : [$this->createTime, $this->updateTime];
}

/**
* 检测时间字段的实际类型.
*
Expand Down

0 comments on commit 8dca158

Please sign in to comment.