Serializable   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 36
ccs 11
cts 11
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A __construct() 0 2 1
A hydrate() 0 12 4
1
<?php
2
3
namespace PierInfor\Gears\Entity;
4
5
/**
6
 * Entity manager for json marshalling unmarshalling
7
 *
8
 */
9
class Serializable
10
{
11
    const _ENTITY_NS_PREFIX = 'PierInfor\\Gears\\Entity\\';
12
    const _SETTER_PREFIX = 'set';
13
    const _GETTER_PREFIX = 'get';
14
15
    /**
16
     * ctor
17
     */
18
    public function __construct()
19
    {
20
    }
21
22
    /**
23
     * json encoder
24
     */
25 1
    public function __toString()
26
    {
27 1
        return json_encode($this);
28
    }
29
30
    /**
31
     * hydrate an object from \stdClass
32
     */
33 4
    public function hydrate(\stdClass $instance): Serializable
34
    {
35 4
        foreach ($instance as $key => $value) {
36 4
            $setter = self::_SETTER_PREFIX . ucfirst($key);
37 4
            $callee = [$this, $setter];
38 4
            if (is_callable($callee)) {
39 4
                $className = self::_ENTITY_NS_PREFIX . ucfirst($key);
40 4
                $arg = (is_object($value)) ? (new $className())->hydrate($value) : $value;
41 4
                call_user_func_array($callee, [$arg]);
42
            }
43
        }
44 4
        return $this;
45
    }
46
}
47