From dbf2660e48e2c51f67095c6e507e5b7c0c120485 Mon Sep 17 00:00:00 2001 From: Chance Date: Mon, 28 Aug 2023 13:40:44 +0800 Subject: [PATCH] fix: Fixed bug with field type `AsArrayObject` Closes #5 --- src/orm/illuminate/Log.php | 15 ++++++++- tests/function.php | 9 ++++++ tests/illuminate/BugFixesTest.php | 53 +++++++++++++++++++++++++++++++ tests/illuminate/model/Casts.php | 16 ++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 tests/illuminate/BugFixesTest.php create mode 100644 tests/illuminate/model/Casts.php diff --git a/src/orm/illuminate/Log.php b/src/orm/illuminate/Log.php index 68831c8..86ed74b 100644 --- a/src/orm/illuminate/Log.php +++ b/src/orm/illuminate/Log.php @@ -9,6 +9,7 @@ use Chance\Log\OperationLog; use Chance\Log\OperationLogInterface; +use Illuminate\Database\Eloquent\Casts\ArrayObject; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; @@ -78,6 +79,10 @@ public function getValue($model, string $key): string $keyText = $key . '_text'; $value = $model->{$keyText} ?? $model->{$key}; + if ($value instanceof ArrayObject) { + $value = $value->toArray(); + } + if (is_array($value)) { return json_encode($value, JSON_UNESCAPED_UNICODE); } @@ -96,7 +101,11 @@ public function getOldValue($model, string $key): string $keyText = $key . '_text'; $attributeFun = 'get' . Str::studly(Str::lower($keyText)) . 'Attribute'; - $value = (string) (method_exists($model, $attributeFun) ? $model->{$attributeFun}($model->getOriginal($key)) : $model->getOriginal($key)); + $value = (method_exists($model, $attributeFun) ? $model->{$attributeFun}($model->getOriginal($key)) : $model->getOriginal($key)); + + if ($value instanceof ArrayObject) { + return json_encode($value->toArray(), JSON_UNESCAPED_UNICODE); + } $val = json_decode($value, true); if (!isset($jsonKey) || is_null($val) || !is_array($val)) { @@ -124,6 +133,10 @@ public function created($model, array $data): void */ public function updated($model, array $oldData, array $data): void { + $data = array_map(function ($value) { + return is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value; + }, $data); + $model->setRawAttributes($oldData, true); $model->setRawAttributes(array_merge($oldData, $data)); $model->syncChanges(); diff --git a/tests/function.php b/tests/function.php index d6cb2ae..ee832cb 100644 --- a/tests/function.php +++ b/tests/function.php @@ -41,6 +41,12 @@ function updateLog(array $old, array $new, $batch = false): string if ($batch) { $log = sprintf('批量修改 用户 (id:%s):', $old['id']); } + $old = array_map(function ($value) { + return is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value; + }, $old); + $new = array_map(function ($value) { + return is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value; + }, $new); $diffKeys = diffKeys($old, $new); if (empty($diffKeys)) { return ''; @@ -70,6 +76,9 @@ function deleteLog($data, $batch = false): string $log = sprintf('批量删除 用户 (id:%s):', $data['id']); } unset($data['id']); + $data = array_map(function ($value) { + return is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value; + }, $data); foreach ($data as $key => $val) { $log .= (columnComment[$key] ?? $key) . ":{$val},"; } diff --git a/tests/illuminate/BugFixesTest.php b/tests/illuminate/BugFixesTest.php new file mode 100644 index 0000000..9bba147 --- /dev/null +++ b/tests/illuminate/BugFixesTest.php @@ -0,0 +1,53 @@ + [ + 'tb_user' => 'Chance\Log\Test\illuminate\model\Casts', + ], + ]); + + $data = mockData(); + $data['json'] = $data; + + /** @var Casts $user */ + $user = Casts::query()->create($data); + $id = $user->id; + array_unshift($data, $id); + $log = createLog($data); + + $user = Casts::query()->find($id); + $old = $user->toArray(); + $new = mockData(); + $new['json'] = array_merge($old['json'], ['name' => $new['name']]); + Casts::query()->where('id', $id)->update($new); + $log .= updateLog($old, $new); + + $data = Casts::query()->find($id)->toArray(); + Casts::query()->where('id', $id)->delete(); + $log .= deleteLog($data); + + OperationLog::setTableModelMapping([]); + + assertEquals(trim($log), OperationLog::getLog()); + } +} diff --git a/tests/illuminate/model/Casts.php b/tests/illuminate/model/Casts.php new file mode 100644 index 0000000..a61056e --- /dev/null +++ b/tests/illuminate/model/Casts.php @@ -0,0 +1,16 @@ + AsArrayObject::class, + ]; +}