Completed
Pull Request — master (#58)
by
unknown
06:56
created

UnitOfWork::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Mapado\RestClientSdk;
4
5
class UnitOfWork
6
{
7
    private $storage;
8
9
    public function __construct()
10
    {
11
        $this->storage = [];
12
    }
13
14
    private function arrayRecursiveDiff($newModel, $oldModel, $isInDepth = false)
15
    {
16
        $diff = [];
17
        $hasDiff = false;
18
19
        foreach ($newModel as $key => $value) {
20
            if (array_key_exists($key, $oldModel)) {
21
                if (is_array($value)) {
22
                    $recursiveDiff = $this->arrayRecursiveDiff($value, $oldModel[$key], true);
23
                    if (count($recursiveDiff)) {
24
                        $hasDiff = true;
25
                        $diff[$key] = $recursiveDiff;
26
27
                        //if theres only ids, keep them
28
                        foreach ($value as $valueKey => $valueId) {
29
                            if (is_string($valueId) && is_int($valueKey)) {
30
                                $diff[$key][$valueKey] = $valueId;
31
                            }
32
                        }
33
                    } elseif (count($value) != count($oldModel[$key])) {
34
                        $hasDiff = true;
35
                        //get all objects ids of new array
36
                        $diff[$key] = [];
37
                        $diff[$key] = $this->addIds($value, $diff[$key]);
38
                    }
39
                } else {
40
                    if ($value != $oldModel[$key]) {
41
                        $diff[$key] = $value;
42
                    }
43
                }
44
            } else {
45
                $diff[$key] = $value;
46
            }
47
        }
48
49
        if ($isInDepth && $hasDiff) {
50
            // in depth add ids of modified objects
51
            $diff = $this->addIds($newModel, $diff);
52
        }
53
54
        return $diff;
55
    }
56
57
    private function addIds($newModel, $diff)
58
    {
59
        foreach ($newModel as $key => $value) {
60
            if (isset($value['@id'])) {
61
                $diff[$key]['@id'] = $value['@id'];
62
            }
63
        }
64
65
        return $diff;
66
    }
67
68
    public function getObjectStorage()
69
    {
70
        return $this->storage;
71
    }
72
73
    public function getDirtyData($newModel, $oldModel)
74
    {
75
        return $this->arrayRecursiveDiff($newModel, $oldModel);
76
    }
77
78
    public function registerClean($id, $entity)
79
    {
80
        if ($entity) {
81
            $entityStored = clone $entity;
82
            $this->storage[$id] = $entityStored;
83
        }
84
85
        return $this;
86
    }
87
88
    public function getDirtyEntity($id)
89
    {
90
        if (isset($this->storage[$id])) {
91
            return $this->storage[$id];
92
        }
93
        return null;
94
    }
95
96
    public function clear($id)
97
    {
98
        unset($this->storage[$id]);
99
        return $this;
100
    }
101
}
102