Skip to content

Commit 2043e27

Browse files
committed
fix #30
1 parent ce68dbf commit 2043e27

File tree

2 files changed

+144
-74
lines changed

2 files changed

+144
-74
lines changed

src/UuidBinaryModelTrait.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,12 @@ public function toArray()
9898

9999
private function deepArray($array)
100100
{
101+
$useOptimization = !empty($this->uuidOptimization);
101102
foreach ($array as $key => $value) {
102-
$value = $array[$key];
103-
if (is_array($value)) {
104-
$array[$key] = $this->deepArray($value);
105-
} elseif (!preg_match('//u', $value)) {
106-
// TODO: drop the preg_match because it's slow and
107-
// what if a binary value in the uuid gets represented
108-
// by valid ASCII or UTF symbols?
109-
$array[$key] = (property_exists($this, 'uuidOptimization') && $this::$uuidOptimization) ?
110-
self::toNormal($value) :
111-
bin2hex($value)
112-
;
103+
if (!is_string($value)) {
104+
$array[$key] = $this->deepArray((array)$value);
105+
} elseif (!ctype_print($value)) {
106+
$array[$key] = $useOptimization ? self::toNormal($value) : bin2hex($value);
113107
}
114108
}
115109
return $array;
@@ -143,4 +137,17 @@ public static function toNormal($uuid)
143137
bin2hex(substr($uuid, 8, 2)) .
144138
bin2hex(substr($uuid, 10));
145139
}
140+
141+
public function fromJson($json, $asObject = false)
142+
{
143+
$mixed = parent::fromJson($json, $asObject);
144+
$key = $this->getKeyName();
145+
if ($asObject) {
146+
$mixed->{$key} = static::toOptimized($mixed->{$key});
147+
} else {
148+
$mixed[$key] = static::toOptimized($mixed[$key]);
149+
}
150+
151+
return $mixed;
152+
}
146153
}

0 commit comments

Comments
 (0)