| Total Complexity | 136 |
| Total Lines | 635 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Attribute often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Attribute, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | trait Attribute |
||
|
1 ignored issue
–
show
|
|||
| 21 | { |
||
| 22 | /** |
||
| 23 | * 数据表主键 复合主键使用数组定义 |
||
| 24 | * @var string|array |
||
| 25 | */ |
||
| 26 | protected $pk = 'id'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * 数据表字段信息 留空则自动获取 |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $field = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * JSON数据表字段 |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $json = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * JSON数据取出是否需要转换为数组 |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | protected $jsonAssoc = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * JSON数据表字段类型 |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $jsonType = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * 数据表废弃字段 |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $disuse = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * 数据表只读字段 |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $readonly = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * 数据表字段类型 |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $type = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * 当前模型数据 |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private $data = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * 修改器执行记录 |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | private $set = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * 原始数据 |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | private $origin = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * 动态获取器 |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | private $withAttr = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * 获取模型对象的主键 |
||
| 96 | * @access public |
||
| 97 | * @return string|array |
||
| 98 | */ |
||
| 99 | public function getPk() |
||
| 100 | { |
||
| 101 | return $this->pk; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * 判断一个字段名是否为主键字段 |
||
| 106 | * @access public |
||
| 107 | * @param string $key 名称 |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | protected function isPk($key) |
||
| 111 | { |
||
| 112 | $pk = $this->getPk(); |
||
| 113 | if (is_string($pk) && $pk == $key) { |
||
| 114 | return true; |
||
| 115 | } elseif (is_array($pk) && in_array($key, $pk)) { |
||
| 116 | return true; |
||
| 117 | } |
||
| 118 | |||
| 119 | return false; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * 获取模型对象的主键值 |
||
| 124 | * @access public |
||
| 125 | * @return integer |
||
| 126 | */ |
||
| 127 | public function getKey() |
||
| 128 | { |
||
| 129 | $pk = $this->getPk(); |
||
| 130 | if (is_string($pk) && array_key_exists($pk, $this->data)) { |
||
| 131 | return $this->data[$pk]; |
||
| 132 | } |
||
| 133 | |||
| 134 | return; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * 设置允许写入的字段 |
||
| 139 | * @access public |
||
| 140 | * @param array|string|true $field 允许写入的字段 如果为true只允许写入数据表字段 |
||
| 141 | * @return $this |
||
| 142 | */ |
||
| 143 | public function allowField($field) |
||
| 144 | { |
||
| 145 | if (is_string($field)) { |
||
| 146 | $field = explode(',', $field); |
||
| 147 | } |
||
| 148 | |||
| 149 | $this->field = $field; |
||
| 150 | |||
| 151 | return $this; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * 设置只读字段 |
||
| 156 | * @access public |
||
| 157 | * @param array|string $field 只读字段 |
||
| 158 | * @return $this |
||
| 159 | */ |
||
| 160 | public function readonly($field) |
||
| 161 | { |
||
| 162 | if (is_string($field)) { |
||
| 163 | $field = explode(',', $field); |
||
| 164 | } |
||
| 165 | |||
| 166 | $this->readonly = $field; |
||
| 167 | |||
| 168 | return $this; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * 设置数据对象值 |
||
| 173 | * @access public |
||
| 174 | * @param mixed $data 数据或者属性名 |
||
| 175 | * @param mixed $value 值 |
||
| 176 | * @return $this |
||
| 177 | */ |
||
| 178 | public function data($data, $value = null) |
||
| 179 | { |
||
| 180 | if (is_string($data)) { |
||
| 181 | $this->data[$data] = $value; |
||
| 182 | return $this; |
||
| 183 | } |
||
| 184 | |||
| 185 | // 清空数据 |
||
| 186 | $this->data = []; |
||
| 187 | |||
| 188 | if (is_object($data)) { |
||
| 189 | $data = get_object_vars($data); |
||
| 190 | } |
||
| 191 | |||
| 192 | if ($this->disuse) { |
||
| 193 | // 废弃字段 |
||
| 194 | foreach ((array) $this->disuse as $key) { |
||
| 195 | if (array_key_exists($key, $data)) { |
||
| 196 | unset($data[$key]); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | if (true === $value) { |
||
| 202 | // 数据对象赋值 |
||
| 203 | foreach ($data as $key => $value) { |
||
| 204 | $this->setAttr($key, $value, $data); |
||
| 205 | } |
||
| 206 | } elseif (is_array($value)) { |
||
| 207 | foreach ($value as $name) { |
||
| 208 | if (isset($data[$name])) { |
||
| 209 | $this->data[$name] = $data[$name]; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } else { |
||
| 213 | $this->data = $data; |
||
| 214 | } |
||
| 215 | |||
| 216 | return $this; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * 批量设置数据对象值 |
||
| 221 | * @access public |
||
| 222 | * @param mixed $data 数据 |
||
| 223 | * @param bool $set 是否需要进行数据处理 |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | public function appendData($data, $set = false) |
||
| 227 | { |
||
| 228 | if ($set) { |
||
| 229 | // 进行数据处理 |
||
| 230 | foreach ($data as $key => $value) { |
||
| 231 | $this->setAttr($key, $value, $data); |
||
| 232 | } |
||
| 233 | } else { |
||
| 234 | if (is_object($data)) { |
||
| 235 | $data = get_object_vars($data); |
||
| 236 | } |
||
| 237 | |||
| 238 | $this->data = array_merge($this->data, $data); |
||
| 239 | } |
||
| 240 | |||
| 241 | return $this; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * 获取对象原始数据 如果不存在指定字段返回null |
||
| 246 | * @access public |
||
| 247 | * @param string $name 字段名 留空获取全部 |
||
| 248 | * @return mixed |
||
| 249 | */ |
||
| 250 | public function getOrigin($name = null) |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * 获取对象原始数据 如果不存在指定字段返回false |
||
| 260 | * @access public |
||
| 261 | * @param string $name 字段名 留空获取全部 |
||
| 262 | * @return mixed |
||
| 263 | * @throws InvalidArgumentException |
||
| 264 | */ |
||
| 265 | public function getData($name = null) |
||
| 266 | { |
||
| 267 | if (is_null($name)) { |
||
| 268 | return $this->data; |
||
| 269 | } elseif (array_key_exists($name, $this->data)) { |
||
| 270 | return $this->data[$name]; |
||
| 271 | } elseif (array_key_exists($name, $this->relation)) { |
||
| 272 | return $this->relation[$name]; |
||
| 273 | } |
||
| 274 | throw new InvalidArgumentException('property not exists:' . static::class . '->' . $name); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * 获取变化的数据 并排除只读数据 |
||
| 279 | * @access public |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | public function getChangedData() |
||
| 283 | { |
||
| 284 | if ($this->force) { |
||
| 285 | $data = $this->data; |
||
| 286 | } else { |
||
| 287 | $data = array_udiff_assoc($this->data, $this->origin, function ($a, $b) { |
||
| 288 | if ((empty($a) || empty($b)) && $a !== $b) { |
||
| 289 | return 1; |
||
| 290 | } |
||
| 291 | |||
| 292 | return is_object($a) || $a != $b ? 1 : 0; |
||
| 293 | }); |
||
| 294 | } |
||
| 295 | |||
| 296 | if (!empty($this->readonly)) { |
||
| 297 | // 只读字段不允许更新 |
||
| 298 | foreach ($this->readonly as $key => $field) { |
||
| 299 | if (isset($data[$field])) { |
||
| 300 | unset($data[$field]); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | return $data; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * 修改器 设置数据对象值 |
||
| 310 | * @access public |
||
| 311 | * @param string $name 属性名 |
||
| 312 | * @param mixed $value 属性值 |
||
| 313 | * @param array $data 数据 |
||
| 314 | * @return void |
||
| 315 | */ |
||
| 316 | public function setAttr($name, $value, $data = []) |
||
| 317 | { |
||
| 318 | if (isset($this->set[$name])) { |
||
| 319 | return; |
||
| 320 | } |
||
| 321 | |||
| 322 | if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime])) { |
||
| 323 | // 自动写入的时间戳字段 |
||
| 324 | $value = $this->autoWriteTimestamp($name); |
||
| 325 | } else { |
||
| 326 | // 检测修改器 |
||
| 327 | $method = 'set' . Loader::parseName($name, 1) . 'Attr'; |
||
| 328 | |||
| 329 | if (method_exists($this, $method)) { |
||
| 330 | $value = $this->$method($value, array_merge($this->data, $data)); |
||
| 331 | |||
| 332 | $this->set[$name] = true; |
||
| 333 | } elseif (isset($this->type[$name])) { |
||
| 334 | // 类型转换 |
||
| 335 | $value = $this->writeTransform($value, $this->type[$name]); |
||
| 336 | } |
||
| 337 | } |
||
| 338 | |||
| 339 | // 设置数据对象属性 |
||
| 340 | $this->data[$name] = $value; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * 是否需要自动写入时间字段 |
||
| 345 | * @access public |
||
| 346 | * @param bool $auto |
||
| 347 | * @return $this |
||
| 348 | */ |
||
| 349 | public function isAutoWriteTimestamp($auto) |
||
| 350 | { |
||
| 351 | $this->autoWriteTimestamp = $auto; |
||
| 352 | |||
| 353 | return $this; |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * 自动写入时间戳 |
||
| 358 | * @access protected |
||
| 359 | * @param string $name 时间戳字段 |
||
| 360 | * @return mixed |
||
| 361 | */ |
||
| 362 | protected function autoWriteTimestamp($name) |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * 数据写入 类型转换 |
||
| 400 | * @access protected |
||
| 401 | * @param mixed $value 值 |
||
| 402 | * @param string|array $type 要转换的类型 |
||
| 403 | * @return mixed |
||
| 404 | */ |
||
| 405 | protected function writeTransform($value, $type) |
||
| 406 | { |
||
| 407 | if (is_null($value)) { |
||
| 408 | return; |
||
| 409 | } |
||
| 410 | |||
| 411 | if ($value instanceof Expression) { |
||
| 412 | return $value; |
||
| 413 | } |
||
| 414 | |||
| 415 | if (is_array($type)) { |
||
| 416 | list($type, $param) = $type; |
||
| 417 | } elseif (strpos($type, ':')) { |
||
| 418 | list($type, $param) = explode(':', $type, 2); |
||
| 419 | } |
||
| 420 | |||
| 421 | switch ($type) { |
||
| 422 | case 'integer': |
||
|
1 ignored issue
–
show
|
|||
| 423 | $value = (int) $value; |
||
| 424 | break; |
||
| 425 | case 'float': |
||
|
1 ignored issue
–
show
|
|||
| 426 | if (empty($param)) { |
||
|
1 ignored issue
–
show
|
|||
| 427 | $value = (float) $value; |
||
| 428 | } else { |
||
|
1 ignored issue
–
show
|
|||
| 429 | $value = (float) number_format($value, $param, '.', ''); |
||
| 430 | } |
||
|
1 ignored issue
–
show
|
|||
| 431 | break; |
||
| 432 | case 'boolean': |
||
|
1 ignored issue
–
show
|
|||
| 433 | $value = (bool) $value; |
||
| 434 | break; |
||
| 435 | case 'timestamp': |
||
|
1 ignored issue
–
show
|
|||
| 436 | if (!is_numeric($value)) { |
||
|
1 ignored issue
–
show
|
|||
| 437 | $value = strtotime($value); |
||
| 438 | } |
||
|
1 ignored issue
–
show
|
|||
| 439 | break; |
||
| 440 | case 'datetime': |
||
|
1 ignored issue
–
show
|
|||
| 441 | $format = !empty($param) ? $param : $this->dateFormat; |
||
| 442 | $value = is_numeric($value) ? $value : strtotime($value); |
||
| 443 | $value = $this->formatDateTime($format, $value); |
||
| 444 | break; |
||
| 445 | case 'object': |
||
|
1 ignored issue
–
show
|
|||
| 446 | if (is_object($value)) { |
||
|
1 ignored issue
–
show
|
|||
| 447 | $value = json_encode($value, JSON_FORCE_OBJECT); |
||
| 448 | } |
||
|
1 ignored issue
–
show
|
|||
| 449 | break; |
||
| 450 | case 'array': |
||
|
1 ignored issue
–
show
|
|||
| 451 | $value = (array) $value; |
||
| 452 | case 'json': |
||
|
1 ignored issue
–
show
|
|||
| 453 | $option = !empty($param) ? (int) $param : JSON_UNESCAPED_UNICODE; |
||
| 454 | $value = json_encode($value, $option); |
||
| 455 | break; |
||
| 456 | case 'serialize': |
||
|
1 ignored issue
–
show
|
|||
| 457 | $value = serialize($value); |
||
| 458 | break; |
||
| 459 | } |
||
| 460 | |||
| 461 | return $value; |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * 获取器 获取数据对象的值 |
||
| 466 | * @access public |
||
| 467 | * @param string $name 名称 |
||
| 468 | * @param array $item 数据 |
||
| 469 | * @return mixed |
||
| 470 | * @throws InvalidArgumentException |
||
| 471 | */ |
||
| 472 | public function getAttr($name, &$item = null) |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * 获取关联属性值 |
||
| 523 | * @access protected |
||
| 524 | * @param string $name 属性名 |
||
| 525 | * @param array $item 数据 |
||
| 526 | * @return mixed |
||
| 527 | */ |
||
| 528 | protected function getRelationAttribute($name, &$item) |
||
| 529 | { |
||
| 530 | $relation = $this->isRelationAttr($name); |
||
| 531 | |||
| 532 | if ($relation) { |
||
| 533 | $modelRelation = $this->$relation(); |
||
| 534 | if ($modelRelation instanceof Relation) { |
||
| 535 | $value = $this->getRelationData($modelRelation); |
||
| 536 | |||
| 537 | if ($item && method_exists($modelRelation, 'getBindAttr') && $bindAttr = $modelRelation->getBindAttr()) { |
||
| 538 | |||
| 539 | foreach ($bindAttr as $key => $attr) { |
||
| 540 | $key = is_numeric($key) ? $attr : $key; |
||
| 541 | |||
| 542 | if (isset($item[$key])) { |
||
| 543 | throw new Exception('bind attr has exists:' . $key); |
||
| 544 | } else { |
||
| 545 | $item[$key] = $value ? $value->getAttr($attr) : null; |
||
| 546 | } |
||
| 547 | } |
||
| 548 | |||
| 549 | return false; |
||
| 550 | } |
||
| 551 | |||
| 552 | // 保存关联对象值 |
||
| 553 | $this->relation[$name] = $value; |
||
| 554 | |||
| 555 | return $value; |
||
| 556 | } |
||
| 557 | } |
||
| 558 | |||
| 559 | throw new InvalidArgumentException('property not exists:' . static::class . '->' . $name); |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * 数据读取 类型转换 |
||
| 564 | * @access protected |
||
| 565 | * @param mixed $value 值 |
||
| 566 | * @param string|array $type 要转换的类型 |
||
| 567 | * @return mixed |
||
| 568 | */ |
||
| 569 | protected function readTransform($value, $type) |
||
| 570 | { |
||
| 571 | if (is_null($value)) { |
||
| 572 | return; |
||
| 573 | } |
||
| 574 | |||
| 575 | if (is_array($type)) { |
||
| 576 | list($type, $param) = $type; |
||
| 577 | } elseif (strpos($type, ':')) { |
||
| 578 | list($type, $param) = explode(':', $type, 2); |
||
| 579 | } |
||
| 580 | |||
| 581 | switch ($type) { |
||
| 582 | case 'integer': |
||
|
1 ignored issue
–
show
|
|||
| 583 | $value = (int) $value; |
||
| 584 | break; |
||
| 585 | case 'float': |
||
|
1 ignored issue
–
show
|
|||
| 586 | if (empty($param)) { |
||
|
1 ignored issue
–
show
|
|||
| 587 | $value = (float) $value; |
||
| 588 | } else { |
||
|
1 ignored issue
–
show
|
|||
| 589 | $value = (float) number_format($value, $param, '.', ''); |
||
| 590 | } |
||
|
1 ignored issue
–
show
|
|||
| 591 | break; |
||
| 592 | case 'boolean': |
||
|
1 ignored issue
–
show
|
|||
| 593 | $value = (bool) $value; |
||
| 594 | break; |
||
| 595 | case 'timestamp': |
||
|
1 ignored issue
–
show
|
|||
| 596 | if (!is_null($value)) { |
||
|
1 ignored issue
–
show
|
|||
| 597 | $format = !empty($param) ? $param : $this->dateFormat; |
||
| 598 | $value = $this->formatDateTime($format, $value, true); |
||
| 599 | } |
||
|
1 ignored issue
–
show
|
|||
| 600 | break; |
||
| 601 | case 'datetime': |
||
|
1 ignored issue
–
show
|
|||
| 602 | if (!is_null($value)) { |
||
|
1 ignored issue
–
show
|
|||
| 603 | $format = !empty($param) ? $param : $this->dateFormat; |
||
| 604 | $value = $this->formatDateTime($format, $value); |
||
| 605 | } |
||
|
1 ignored issue
–
show
|
|||
| 606 | break; |
||
| 607 | case 'json': |
||
|
1 ignored issue
–
show
|
|||
| 608 | $value = json_decode($value, true); |
||
| 609 | break; |
||
| 610 | case 'array': |
||
|
1 ignored issue
–
show
|
|||
| 611 | $value = empty($value) ? [] : json_decode($value, true); |
||
| 612 | break; |
||
| 613 | case 'object': |
||
|
1 ignored issue
–
show
|
|||
| 614 | $value = empty($value) ? new \stdClass() : json_decode($value); |
||
| 615 | break; |
||
| 616 | case 'serialize': |
||
|
1 ignored issue
–
show
|
|||
| 617 | try { |
||
|
1 ignored issue
–
show
|
|||
| 618 | $value = unserialize($value); |
||
| 619 | } catch (\Exception $e) { |
||
|
1 ignored issue
–
show
|
|||
| 620 | $value = null; |
||
| 621 | } |
||
|
1 ignored issue
–
show
|
|||
| 622 | break; |
||
| 623 | default: |
||
|
1 ignored issue
–
show
|
|||
| 624 | if (false !== strpos($type, '\\')) { |
||
|
1 ignored issue
–
show
|
|||
| 625 | // 对象类型 |
||
| 626 | $value = new $type($value); |
||
| 627 | } |
||
|
1 ignored issue
–
show
|
|||
| 628 | } |
||
| 629 | |||
| 630 | return $value; |
||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * 设置数据字段获取器 |
||
| 635 | * @access public |
||
| 636 | * @param string|array $name 字段名 |
||
| 637 | * @param callable $callback 闭包获取器 |
||
| 638 | * @return $this |
||
| 639 | */ |
||
| 640 | public function withAttribute($name, $callback = null) |
||
| 655 | } |
||
| 656 | } |
||
| 657 |