Skip to content

Commit

Permalink
Merge pull request #547 from skipperbent/v3-development
Browse files Browse the repository at this point in the history
Version 3.17.6
  • Loading branch information
skipperbent committed Apr 1, 2024
2 parents 7088c40 + 6fedc65 commit d050005
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"pecee/simple-router": "^5.0",
"robmorgan/phinx": "^0.13.4",
"nesbot/carbon": "2.*",
"laravel/serializable-closure": "^1.3",
"ext-json": "*",
"ext-mbstring": "*",
"ext-fileinfo": "*",
Expand Down
27 changes: 24 additions & 3 deletions src/ArrayUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Pecee;

use Laravel\SerializableClosure\SerializableClosure;

class ArrayUtil
{

Expand All @@ -14,19 +16,38 @@ class ArrayUtil
*/
public static function filter(array $array, ?callable $callback = null): array
{
$array = array_map(static function($item) {
$array = array_map(static function ($item) {
return is_array($item) ? static::filter($item) : $item;
}, $array);

if($callback === null) {
$callback = static function($value) {
if ($callback === null) {
$callback = static function ($value) {
return !empty($value);
};
}

return array_filter($array, $callback);
}

public static function serialize(array $input): array
{
$output = [];
foreach ($input as $key => $value) {
$output[$key] = ($value instanceof \Closure) ? new SerializableClosure($value) : $value;
}
return $output;
}

public static function unserialize(array $input): array
{
$output = [];
foreach ($input as $key => $value) {
$output[$key] = ($value instanceof SerializableClosure) ? $value->getClosure() : $value;
}

return $output;
}

public static function average(array $arr)
{
$count = count($arr); //total numbers in array
Expand Down
29 changes: 24 additions & 5 deletions src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Pecee\Model;

use Carbon\Carbon;
use Pecee\ArrayUtil;
use Pecee\Model\Collections\ModelCollection;
use Pecee\Model\Exceptions\ModelException;
use Pecee\Model\Relation\BelongsTo;
Expand Down Expand Up @@ -885,24 +886,42 @@ public function __wakeup()

public function serialize()
{
return $this->getRows();
return [
'results' => $this->results,
'with' => ArrayUtil::serialize($this->with),
'without' => $this->without,
'rename' => $this->rename,
'hidden' => $this->hidden,
'filter' => $this->filter,
'invokedElements' => $this->invokedElements,
'relations' => $this->relations,
];
}

/**
* @param array $data
*/
public function unserialize($data)
{
$this->setRows((array)$data);
$this->results = $data['results'];
$this->with = ArrayUtil::unserialize($data['with']);
$this->without = $data['without'];
$this->rename = $data['rename'];
$this->hidden = $data['hidden'];
$this->filter = $data['filter'];
$this->invokedElements = $data['invokedElements'];
$this->relations = $data['relations'];
$this->__wakeup();
}

public function __serialize(): array
{
return $this->getRows();
return $this->serialize();
}

public function __unserialize(array $data): void
{
$this->setRows($data);
$this->__wakeup();
$this->unserialize($data);
}

}

0 comments on commit d050005

Please sign in to comment.