Skip to content

Commit

Permalink
Switch from serialize to json_encode. Restrict via a list of accepted…
Browse files Browse the repository at this point in the history
… value types. Shrink varchar size to 7. Code tweaks.
  • Loading branch information
muzzwood committed Nov 1, 2024
1 parent d6fac55 commit 8d4a850
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
4 changes: 2 additions & 2 deletions _build/schema/versionx.mysql.schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@
Keep track of the before and after value types so reverting doesn't decode an intended json string,
or cast an int as a string etc. Empty value means it's a string.
-->
<field key="before_type" dbtype="varchar" precision="20" phptype="string" null="false" default="" />
<field key="after_type" dbtype="varchar" precision="20" phptype="string" null="false" default="" />
<field key="before_type" dbtype="varchar" precision="7" phptype="string" null="false" default="" />
<field key="after_type" dbtype="varchar" precision="7" phptype="string" null="false" default="" />

<field key="before" dbtype="mediumtext" phptype="string" null="false" default="" />
<field key="after" dbtype="mediumtext" phptype="string" null="false" default="" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@
'before_type' =>
array (
'dbtype' => 'varchar',
'precision' => '20',
'precision' => '7',
'phptype' => 'string',
'null' => false,
'default' => '',
),
'after_type' =>
array (
'dbtype' => 'varchar',
'precision' => '20',
'precision' => '7',
'phptype' => 'string',
'null' => false,
'default' => '',
Expand Down
14 changes: 9 additions & 5 deletions core/components/versionx/src/DeltaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use Jfcherng\Diff\DiffHelper;
use modmore\VersionX\Fields\Field;
use modmore\VersionX\Types\Type;
use modmore\VersionX\Enums\RevertAction;
use MODX\Revolution\modX;
Expand Down Expand Up @@ -31,7 +32,6 @@ class DeltaManager
'showHeader' => false,
];


function __construct(VersionX $versionX)
{
$this->versionX = $versionX;
Expand Down Expand Up @@ -118,12 +118,16 @@ public function createDelta(int $id, Type $type, bool $isSnapshot = false): ?\vx
$fieldType = $type->getFieldClass($field);
$fieldTypeObj = new $fieldType($value);

// Flatten any arrays and set the value as a string
// Get the value and the value type
$value = $fieldTypeObj->getValue();
$valueType = gettype($value);
// If the value is not an accepted type, default to string
if (!in_array(strtolower($valueType), Field::ACCEPTED_VALUE_TYPES)) {
$valueType = '';
}

if (is_array($value)) {
$value = serialize($value);
if (is_array($value) || is_object($value)) {
$value = json_encode($value);
} else {
$value = (string)$value;
}
Expand All @@ -150,7 +154,7 @@ public function createDelta(int $id, Type $type, bool $isSnapshot = false): ?\vx
'field' => $field,
'field_type' => $fieldType,
'before_type' => $prevValueType,
'after_type' => $valueType !== 'string' ? $valueType : '',
'after_type' => $valueType,
'before' => $prevValue,
'after' => $value,
'diff' => $renderedDiff, // Not persisted. Kept on object until cached.
Expand Down
12 changes: 12 additions & 0 deletions core/components/versionx/src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ abstract class Field
protected array $options = [];
protected string $tpl = '';

public const ACCEPTED_VALUE_TYPES = [
'array',
'object',
'boolean',
'bool',
'integer',
'int',
'float',
'double',
'null'
];

function __construct($value, $name = '', $options = [])
{
if (!$this->value) {
Expand Down
16 changes: 6 additions & 10 deletions core/components/versionx/src/Fields/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,17 @@ public static function revertPropertyValue(\vxDeltaField $field, &$data)
continue;
}

// The last 'piece' will be the key we're after.
if ($piece === $last) {
// $json = json_decode($field->get('before'), true);
// if (json_last_error() === JSON_ERROR_NONE) {
// $data[$piece] = $json;
// }

$beforeValue = $field->get('before');
$beforeType = $field->get('before_type');

if ($beforeType === 'array') {
// Unserialize if it's meant to be an array
$unserialized = unserialize($beforeValue);
$data[$piece] = $unserialized !== false ? $unserialized : $field->get('before');
if (in_array($beforeType, ['array', 'object'])) {
// Decode if it's meant to be an array/object
$decoded = json_decode($beforeValue, true);
$data[$piece] = $decoded ?: $field->get('before');
}
else if (in_array($beforeType, ['boolean', 'bool', 'integer', 'int', 'float', 'double'])) {
else if (in_array(strtolower($beforeType), self::ACCEPTED_VALUE_TYPES)) {
// Cast as the set type
$data[$piece] = settype($beforeValue, $beforeType);
}
Expand Down

0 comments on commit 8d4a850

Please sign in to comment.