Test Failed
Push — master ( ce96ba...918ead )
by Joao
01:53
created

src/Mapper.php (1 issue)

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