EntityFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createEntities() 0 6 1
A createEntity() 0 7 1
A applyProperties() 0 11 3
1
<?php
2
/*
3
 * This file is part of the slince/smartqq package.
4
 *
5
 * (c) Slince <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Slince\SmartQQ;
12
13
class EntityFactory
14
{
15
    /**
16
     * 创建多个实体对象
17
     *
18
     * @param string $entityClass
19
     * @param array  $dataArray
20
     *
21
     * @return array
22
     */
23
    public static function createEntities($entityClass, $dataArray)
24
    {
25
        return array_map(function ($data) use ($entityClass) {
26
            return static::createEntity($entityClass, $data);
27
        }, $dataArray);
28
    }
29
30
    /**
31
     * 创建实体对象
32
     *
33
     * @param string $entityClass
34
     * @param array  $data
35
     *
36
     * @return object
37
     */
38
    public static function createEntity($entityClass, $data)
39
    {
40
        $entity = new $entityClass();
41
        static::applyProperties($entity, $data);
42
43
        return $entity;
44
    }
45
46
    /**
47
     * 设置属性参数.
48
     *
49
     * @param object $entityInstance
50
     * @param array  $data
51
     *
52
     * @return object
53
     */
54
    protected static function applyProperties($entityInstance, $data)
55
    {
56
        foreach ($data as $property => $value) {
57
            $funcName = 'set'.ucfirst($property);
58
            if (method_exists($entityInstance, $funcName)) {
59
                $entityInstance->$funcName($value);
60
            }
61
        }
62
63
        return $entityInstance;
64
    }
65
}
66