Completed
Push — master ( 4deaab...ecf49a )
by Joao
04:00 queued 01:34
created

Mapper::getFieldMap()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.679

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 3
cts 7
cp 0.4286
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 4.679
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
    protected $entity;
15
    protected $table;
16
    protected $primaryKey;
17
    protected $keygenFunction = null;
18
19
    const FIELDMAP_FIELD = 'fieldname';
20
    const FIELDMAP_UPDATEMASK = 'updatemask';
21
    const FIELDMAP_SELECTMASK = 'selectmask';
22
23
    protected $fieldMap = [];
24
25
    /**
26
     * Mapper constructor.
27
     *
28
     * @param string $entity
29
     * @param string $table
30
     * @param string $primaryKey
31
     * @param \Closure $keygenFunction
32
     * @throws \Exception
33
     */
34 19
    public function __construct($entity, $table, $primaryKey, \Closure $keygenFunction = null)
35
    {
36 19
        if (!class_exists($entity)) {
37
            throw new \Exception("Entity '$entity' does not exists");
38
        }
39 19
        $this->entity = $entity;
40 19
        $this->table = $table;
41 19
        $this->primaryKey = $primaryKey;
42 19
        $this->keygenFunction = $keygenFunction;
43 19
    }
44
45
    /**
46
     * @param string $property
47
     * @param string $fieldName
48
     * @param \Closure|null|bool $updateMask
49
     * @param \Closure $selectMask
50
     * @return $this
51
     */
52 19
    public function addFieldMap($property, $fieldName, $updateMask = false, \Closure $selectMask = null)
53
    {
54 19
        if (empty($selectMask)) {
55 19
            $selectMask = Mapper::defaultClosure();
56 19
        }
57
58 19
        if ($updateMask === false) {
59 19
            $updateMask = Mapper::defaultClosure();
60 19
        }
61
62 19
        if (!is_null($updateMask) && !($updateMask instanceof \Closure)) {
63
            throw new \InvalidArgumentException('UpdateMask must be a \Closure or NULL');
64
        }
65
66 19
        $this->fieldMap[$property] = [
67 19
            self::FIELDMAP_FIELD => $fieldName,
68 19
            self::FIELDMAP_UPDATEMASK => $updateMask,
69 19
            self::FIELDMAP_SELECTMASK => $selectMask
70 19
        ];
71
72 19
        return $this;
73
    }
74
75
    /**
76
     * @return string
77
     */
78 19
    public function getEntity()
79
    {
80 19
        $class = $this->entity;
81 19
        return new $class();
82
    }
83
84
    /**
85
     * @return string
86
     */
87 19
    public function getTable()
88
    {
89 19
        return $this->table;
90
    }
91
92
    /**
93
     * @return string
94
     */
95 14
    public function getPrimaryKey()
96
    {
97 14
        return $this->primaryKey;
98
    }
99
100
    /**
101
     * @param string|null $property
102
     * @param string|null $key
103
     * @return array
104
     */
105 19
    public function getFieldMap($property = null, $key = null)
106
    {
107 19
        if (empty($property)) {
108 19
            return $this->fieldMap;
109
        }
110
111
        $fieldMap = $this->fieldMap[$property];
112
113
        if (empty($key)) {
114
            return $fieldMap;
115
        }
116
117
        return $fieldMap[$key];
118
    }
119
120
    /**
121
     * @return mixed|null
122
     */
123 5
    public function generateKey()
124
    {
125 5
        if (empty($this->keygenFunction)) {
126 4
            return null;
127
        }
128
129 1
        $func = $this->keygenFunction;
130
131 1
        return $func();
132
    }
133
134
    public static function defaultClosure()
135
    {
136 19
        return function ($value, $instance) {
0 ignored issues
show
Unused Code introduced by
The parameter $instance is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
137 7
            return $value;
138 19
        };
139
    }
140
}
141