| 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 | 30 | public function __construct( |
|
| 34 | $entity, |
||
| 35 | $table, |
||
| 36 | $primaryKey, |
||
| 37 | \Closure $keygenFunction = null, |
||
| 38 | $preserveCasename = false |
||
| 39 | ) { |
||
| 40 | 30 | if (!class_exists($entity)) { |
|
| 41 | throw new OrmModelInvalidException("Entity '$entity' does not exists"); |
||
| 42 | } |
||
| 43 | 30 | $this->entity = $entity; |
|
| 44 | 30 | $this->table = $table; |
|
| 45 | 30 | $this->preserveCasename = $preserveCasename; |
|
| 46 | 30 | $this->primaryKey = $this->prepareField($primaryKey); |
|
| 47 | 30 | $this->keygenFunction = $keygenFunction; |
|
| 48 | 30 | } |
|
| 49 | |||
| 50 | 30 | public function prepareField($field) |
|
| 51 | { |
||
| 52 | 30 | if (!$this->preserveCasename) { |
|
| 53 | 30 | if (!is_array($field)) { |
|
| 54 | 30 | return strtolower($field); |
|
| 55 | } |
||
| 56 | |||
| 57 | 10 | $result = []; |
|
| 58 | 10 | foreach ($field as $key => $value) { |
|
| 59 | 10 | $result[strtolower($key)] = $value; |
|
| 60 | } |
||
| 61 | 10 | return $result; |
|
| 62 | } |
||
| 63 | |||
| 64 | 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 | 30 | public function addFieldMap($property, $fieldName, \Closure $updateMask = null, \Closure $selectMask = null) |
|
| 76 | { |
||
| 77 | 30 | if (empty($selectMask)) { |
|
| 78 | 30 | $selectMask = Mapper::defaultClosure(); |
|
| 79 | } |
||
| 80 | |||
| 81 | 30 | if (empty($updateMask)) { |
|
| 82 | 30 | $updateMask = Mapper::defaultClosure(); |
|
| 83 | } |
||
| 84 | |||
| 85 | 30 | if (!is_null($updateMask) && !($updateMask instanceof \Closure)) { |
|
| 86 | throw new InvalidArgumentException('UpdateMask must be a \Closure or NULL'); |
||
| 87 | } |
||
| 88 | |||
| 89 | 30 | $this->fieldMap[$this->prepareField($property)] = [ |
|
| 90 | 30 | self::FIELDMAP_FIELD => $this->prepareField($fieldName), |
|
| 91 | 30 | self::FIELDMAP_UPDATEMASK => $updateMask, |
|
| 92 | 30 | self::FIELDMAP_SELECTMASK => $selectMask |
|
| 93 | ]; |
||
| 94 | |||
| 95 | 30 | return $this; |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param $fieldName |
||
| 100 | * @param $alias |
||
| 101 | */ |
||
| 102 | 4 | public function addFieldAlias($fieldName, $alias) |
|
| 103 | { |
||
| 104 | 4 | $this->fieldAlias[$this->prepareField($fieldName)] = $this->prepareField($alias); |
|
| 105 | 4 | } |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @return object |
||
| 109 | */ |
||
| 110 | 27 | public function getEntity() |
|
| 111 | { |
||
| 112 | 27 | $class = $this->entity; |
|
| 113 | 27 | return new $class(); |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | 29 | public function getTable() |
|
| 120 | { |
||
| 121 | 29 | return $this->table; |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | 19 | public function getPrimaryKey() |
|
| 128 | { |
||
| 129 | 19 | 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 | * @return array |
||
| 144 | */ |
||
| 145 | 27 | public function getFieldMap($property = null, $key = null) |
|
| 146 | { |
||
| 147 | 27 | if (empty($property)) { |
|
| 148 | 27 | return $this->fieldMap; |
|
| 149 | } |
||
| 150 | |||
| 151 | 1 | $property = $this->prepareField($property); |
|
| 152 | |||
| 153 | 1 | if (!isset($this->fieldMap[$property])) { |
|
| 154 | 1 | return null; |
|
| 155 | } |
||
| 156 | |||
| 157 | 1 | $fieldMap = $this->fieldMap[$property]; |
|
| 158 | |||
| 159 | 1 | if (empty($key)) { |
|
| 160 | return $fieldMap; |
||
| 161 | } |
||
| 162 | |||
| 163 | 1 | return $fieldMap[$key]; |
|
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param null $fieldName |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 168 | * @return array|mixed|null |
||
| 169 | */ |
||
| 170 | 27 | public function getFieldAlias($fieldName = null) |
|
| 171 | { |
||
| 172 | 27 | if (empty($fieldName)) { |
|
| 173 | 27 | return $this->fieldAlias; |
|
| 174 | } |
||
| 175 | |||
| 176 | 1 | if (!isset($this->fieldAlias[$fieldName])) { |
|
| 177 | 1 | return null; |
|
| 178 | } |
||
| 179 | |||
| 180 | 1 | return $this->fieldAlias[$fieldName]; |
|
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return mixed|null |
||
| 185 | */ |
||
| 186 | 6 | public function generateKey() |
|
| 187 | { |
||
| 188 | 6 | if (empty($this->keygenFunction)) { |
|
| 189 | 5 | return null; |
|
| 190 | } |
||
| 191 | |||
| 192 | 1 | $func = $this->keygenFunction; |
|
| 193 | |||
| 194 | 1 | return $func(); |
|
| 195 | } |
||
| 196 | |||
| 197 | 30 | public static function defaultClosure() |
|
| 198 | { |
||
| 199 | return function ($value) { |
||
| 200 | 10 | if (empty($value)) { |
|
| 201 | 1 | return null; |
|
| 202 | } |
||
| 203 | 9 | return $value; |
|
| 204 | 30 | }; |
|
| 205 | } |
||
| 206 | |||
| 207 | 2 | public static function doNotUpdateClosure() |
|
| 208 | { |
||
| 209 | return function () { |
||
| 210 | 2 | return false; |
|
| 211 | 2 | }; |
|
| 212 | } |
||
| 213 | } |
||
| 214 |