Skip to content

Commit

Permalink
refactor to strategy classes
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgaal committed Jul 14, 2022
1 parent 3618818 commit 41dfa34
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 16 deletions.
16 changes: 16 additions & 0 deletions src/Mpociot/Versionable/Encoders/Encoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Mpociot\Versionable\Encoders;

interface Encoder
{
/**
* @param mixed $data
*/
public function encode($data): string;

/**
* @return mixed
*/
public function decode(string $data);
}
16 changes: 16 additions & 0 deletions src/Mpociot/Versionable/Encoders/JsonEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Mpociot\Versionable\Encoders;

class JsonEncoder implements Encoder
{
public function encode($data): string
{
return json_encode($data);
}

public function decode(string $data)
{
return json_decode($data, true);
}
}
16 changes: 16 additions & 0 deletions src/Mpociot/Versionable/Encoders/SerializeEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Mpociot\Versionable\Encoders;

class SerializeEncoder implements Encoder
{
public function encode($data): string
{
return serialize($data);
}

public function decode(string $data)
{
return unserialize($data);
}
}
15 changes: 9 additions & 6 deletions src/Mpociot/Versionable/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use Illuminate\Support\Facades\Config;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Model;
use Mpociot\Versionable\Encoders\Encoder;
use Mpociot\Versionable\Encoders\SerializeEncoder;

/**
* Class Version
Expand Down Expand Up @@ -32,12 +34,13 @@ public function versionable()
}

/**
* Return the encoding
* @return mixed
* Get the encoder.
*
* @return Encoder
*/
private function getEncoding()
private function getEncoder(): Encoder
{
return config('versionable.encoding', 'serialize');
return app(config('versionable.encoder', SerializeEncoder::class));
}

/**
Expand All @@ -59,12 +62,12 @@ public function getModel()
$modelData = is_resource($this->model_data)
? stream_get_contents($this->model_data,-1,0)
: $this->model_data;
$modelDataEncoded = $this->getEncoding() === 'json' ? json_decode($modelData, true) : unserialize($modelData);
$modelDataDecoded = $this->getEncoder()->decode($modelData);

$className = self::getActualClassNameForMorph($this->versionable_type);
$model = new $className();
$model->unguard();
$model->fill($modelDataEncoded);
$model->fill($modelDataDecoded);
$model->exists = true;
$model->reguard();
return $model;
Expand Down
14 changes: 7 additions & 7 deletions src/Mpociot/Versionable/VersionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Mpociot\Versionable\Encoders\Encoder;
use Mpociot\Versionable\Encoders\SerializeEncoder;

/**
* Class VersionableTrait
Expand All @@ -29,13 +31,13 @@ protected function getVersionClass()
}

/**
* Get the encoding, the default is serialize.
* Get the encoder.
*
* @return string
* @return Encoder
*/
protected function getEncoding()
protected function getEncoder(): Encoder
{
return config('versionable.encoding', 'serialize');
return app(config('versionable.encoder', SerializeEncoder::class));
}

/**
Expand Down Expand Up @@ -186,9 +188,7 @@ protected function versionablePostSave()

$versionedHiddenFields = $this->versionedHiddenFields ?? [];
$this->makeVisible($versionedHiddenFields);
$version->model_data = $this->getEncoding() === 'json'
? json_encode($this->attributesToArray())
: serialize($this->attributesToArray());
$version->model_data = $this->getEncoder()->encode($this->attributesToArray());
$this->makeHidden($versionedHiddenFields);

if (!empty( $this->reason )) {
Expand Down
4 changes: 2 additions & 2 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

/*
* The encoding to use for the model data encoding.
* Default is 'serialize' and uses PHP serialize() but 'json' is also supported
*/
'encoding' => 'serialize',
'encoder' => \Mpociot\Versionable\Encoders\SerializeEncoder::class,

];
2 changes: 1 addition & 1 deletion tests/VersionableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ public function testWhereModelHasMorphMap()

public function testVersionWithJson()
{
$this->app['config']->set('versionable.encoding', 'json');
$this->app['config']->set('versionable.encoder', \Mpociot\Versionable\Encoders\JsonEncoder::class);

$user = new TestVersionableUser();
$user->name = "Marcel";
Expand Down

0 comments on commit 41dfa34

Please sign in to comment.