Skip to content

Commit

Permalink
fix #30
Browse files Browse the repository at this point in the history
  • Loading branch information
alsofronie committed Jul 20, 2017
1 parent ce68dbf commit 2043e27
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 74 deletions.
29 changes: 18 additions & 11 deletions src/UuidBinaryModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,12 @@ public function toArray()

private function deepArray($array)
{
$useOptimization = !empty($this->uuidOptimization);
foreach ($array as $key => $value) {
$value = $array[$key];
if (is_array($value)) {
$array[$key] = $this->deepArray($value);
} elseif (!preg_match('//u', $value)) {
// TODO: drop the preg_match because it's slow and
// what if a binary value in the uuid gets represented
// by valid ASCII or UTF symbols?
$array[$key] = (property_exists($this, 'uuidOptimization') && $this::$uuidOptimization) ?
self::toNormal($value) :
bin2hex($value)
;
if (!is_string($value)) {
$array[$key] = $this->deepArray((array)$value);
} elseif (!ctype_print($value)) {
$array[$key] = $useOptimization ? self::toNormal($value) : bin2hex($value);
}
}
return $array;
Expand Down Expand Up @@ -143,4 +137,17 @@ public static function toNormal($uuid)
bin2hex(substr($uuid, 8, 2)) .
bin2hex(substr($uuid, 10));
}

public function fromJson($json, $asObject = false)
{
$mixed = parent::fromJson($json, $asObject);
$key = $this->getKeyName();
if ($asObject) {
$mixed->{$key} = static::toOptimized($mixed->{$key});
} else {
$mixed[$key] = static::toOptimized($mixed[$key]);
}

return $mixed;
}
}
Loading

0 comments on commit 2043e27

Please sign in to comment.