HydratableTrait::dehydrate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/*
4
 *  @copyright (c) 2019 Mendel <[email protected]>
5
 *  @license see license.txt
6
 */
7
8
namespace drycart\data;
9
10
/**
11
 * Trait for dummy hydrate/dehydrate methods
12
 * 
13
 * @author mendel
14
 */
15
trait HydratableTrait
16
{
17
    /**
18
     * Hydrate
19
     * 
20
     * @param array $data
21
     * @return HydratableInterface
22
     */
23
    public static function hydrate(array $data) : HydratableInterface
24
    {
25
        $model = new static;
26
        foreach($data as $key=>$value) {
27
            $model->$key = $value;
28
        }
29
        return $model;
30
    }
31
32
    /**
33
     * Dehydrate
34
     * 
35
     * @return array
36
     */
37
    public function dehydrate(): array
38
    {
39
        $data = [];
40
        foreach(GetterHelper::getKeys($this) as $key) {
41
            $data[$key] = $this->$key;
42
        }
43
        return $data;
44
    }
45
}
46