Completed
Push — master ( 597293...345e29 )
by Joao
04:12 queued 02:23
created

Mapper   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Test Coverage

Coverage 92.96%

Importance

Changes 0
Metric Value
wmc 25
lcom 3
cbo 0
dl 0
loc 194
ccs 66
cts 71
cp 0.9296
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A prepareField() 0 16 4
A addFieldMap() 0 18 3
A addFieldAlias() 0 4 1
A getEntity() 0 5 1
A getTable() 0 4 1
A getPrimaryKey() 0 4 1
A isPreserveCasename() 0 4 1
A getFieldMap() 0 20 4
A getFieldAlias() 0 12 3
A generateKey() 0 10 2
A defaultClosure() 0 6 1
A doNotUpdateClosure() 0 6 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jg
5
 * Date: 21/06/16
6
 * Time: 13:55
7
 */
8
9
namespace ByJG\MicroOrm;
10
11
class Mapper
12
{
13
14
    const FIELDMAP_FIELD = 'fieldname';
15
    const FIELDMAP_UPDATEMASK = 'updatemask';
16
    const FIELDMAP_SELECTMASK = 'selectmask';
17
18
    private $entity;
19
    private $table;
20
    private $primaryKey;
21
    private $keygenFunction = null;
22
    private $fieldMap = [];
23
    private $fieldAlias = [];
24
    private $preserveCasename = false;
25
26
    /**
27
     * Mapper constructor.
28
     *
29
     * @param string $entity
30
     * @param string $table
31
     * @param string $primaryKey
32
     * @param \Closure $keygenFunction
33
     * @param bool $preserveCasename
34
     * @throws \Exception
35
     */
36 21
    public function __construct(
37
        $entity,
38
        $table,
39
        $primaryKey,
40
        \Closure $keygenFunction = null,
41
        $preserveCasename = false
42
    ) {
43 21
        if (!class_exists($entity)) {
44
            throw new \Exception("Entity '$entity' does not exists");
45
        }
46 21
        $this->entity = $entity;
47 21
        $this->table = $table;
48 21
        $this->preserveCasename = $preserveCasename;
49 21
        $this->primaryKey = $this->prepareField($primaryKey);
50 21
        $this->keygenFunction = $keygenFunction;
51 21
    }
52
53 21
    public function prepareField($field)
54
    {
55 21
        if (!$this->preserveCasename) {
56 21
            if (!is_array($field)) {
57 21
                return strtolower($field);
58
            }
59
60 8
            $result = [];
61 8
            foreach ($field as $key => $value) {
62 8
                $result[strtolower($key)] = $value;
63 8
            }
64 8
            return $result;
65
        }
66
67
        return $field;
68
    }
69
70
    /**
71
     * @param string $property
72
     * @param string $fieldName
73
     * @param \Closure|null|bool $updateMask
74
     * @param \Closure $selectMask
75
     * @return $this
76
     */
77 21
    public function addFieldMap($property, $fieldName, \Closure $updateMask = null, \Closure $selectMask = null)
78
    {
79 21
        if (empty($selectMask)) {
80 21
            $selectMask = Mapper::defaultClosure();
81 21
        }
82
83 21
        if (empty($updateMask)) {
84 21
            $updateMask = Mapper::defaultClosure();
85 21
        }
86
87 21
        $this->fieldMap[$this->prepareField($property)] = [
88 21
            self::FIELDMAP_FIELD => $this->prepareField($fieldName),
89 21
            self::FIELDMAP_UPDATEMASK => $updateMask,
90 21
            self::FIELDMAP_SELECTMASK => $selectMask
91 21
        ];
92
93 21
        return $this;
94
    }
95
96
    /**
97
     * @param $fieldName
98
     * @param $alias
99
     */
100 4
    public function addFieldAlias($fieldName, $alias)
101
    {
102 4
        $this->fieldAlias[$this->prepareField($fieldName)] = $this->prepareField($alias);
103 4
    }
104
105
    /**
106
     * @return string
107
     */
108 21
    public function getEntity()
109
    {
110 21
        $class = $this->entity;
111 21
        return new $class();
112
    }
113
114
    /**
115
     * @return string
116
     */
117 20
    public function getTable()
118
    {
119 20
        return $this->table;
120
    }
121
122
    /**
123
     * @return string
124
     */
125 14
    public function getPrimaryKey()
126
    {
127 14
        return $this->primaryKey;
128
    }
129
130
    /**
131
     * @return bool
132
     */
133
    public function isPreserveCasename()
134
    {
135
        return $this->preserveCasename;
136
    }
137
138
    /**
139
     * @param string|null $property
140
     * @param string|null $key
141
     * @return array
142
     */
143 21
    public function getFieldMap($property = null, $key = null)
144
    {
145 21
        if (empty($property)) {
146 21
            return $this->fieldMap;
147
        }
148
149 1
        $property = $this->prepareField($property);
150
151 1
        if (!isset($this->fieldMap[$property])) {
152 1
            return null;
153
        }
154
155 1
        $fieldMap = $this->fieldMap[$property];
156
157 1
        if (empty($key)) {
158
            return $fieldMap;
159
        }
160
161 1
        return $fieldMap[$key];
162
    }
163
164 21
    public function getFieldAlias($fieldName = null)
165
    {
166 21
        if (empty($fieldName)) {
167 21
            return $this->fieldAlias;
168
        }
169
        
170 1
        if (!isset($this->fieldAlias[$fieldName])) {
171 1
            return null;
172
        }
173
174 1
        return $this->fieldAlias[$fieldName];
175
    }
176
177
    /**
178
     * @return mixed|null
179
     */
180 5
    public function generateKey()
181
    {
182 5
        if (empty($this->keygenFunction)) {
183 4
            return null;
184
        }
185
186 1
        $func = $this->keygenFunction;
187
188 1
        return $func();
189
    }
190
191 21
    public static function defaultClosure()
192
    {
193
        return function ($value) {
194 9
            return $value;
195 21
        };
196
    }
197
198
    public static function doNotUpdateClosure()
199
    {
200 2
        return function () {
201 2
            return false;
202 2
        };
203
    }
204
}
205