Skip to content

Commit

Permalink
fix: Fixed bug with field type AsArrayObject
Browse files Browse the repository at this point in the history
Closes #5
  • Loading branch information
Chance-fyi committed Aug 28, 2023
1 parent 0e597f7 commit dbf2660
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/orm/illuminate/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand All @@ -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)) {
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions tests/function.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand Down Expand Up @@ -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}";
}
Expand Down
53 changes: 53 additions & 0 deletions tests/illuminate/BugFixesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Created by PhpStorm
* Date 2023/8/28 11:46.
*/

namespace Chance\Log\Test\illuminate;

use Chance\Log\facades\OperationLog;
use Chance\Log\Test\illuminate\model\Casts;

use function PHPUnit\Framework\assertEquals;

/**
* @internal
*
* @coversNothing
*/
class BugFixesTest extends TestCase
{
public function test5()
{
OperationLog::setTableModelMapping([
'test' => [
'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());
}
}
16 changes: 16 additions & 0 deletions tests/illuminate/model/Casts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Created by PhpStorm
* Date 2023/8/28 11:27.
*/

namespace Chance\Log\Test\illuminate\model;

use Illuminate\Database\Eloquent\Casts\AsArrayObject;

class Casts extends Timestamps
{
protected $casts = [
'json' => AsArrayObject::class,
];
}

0 comments on commit dbf2660

Please sign in to comment.