|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebStream\Database; |
|
3
|
|
|
|
|
4
|
|
|
use WebStream\DI\Injector; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* EntityManager |
|
8
|
|
|
* @author Ryuichi TANAKA. |
|
9
|
|
|
* @since 2015/01/11 |
|
10
|
|
|
* @version 0.7 |
|
11
|
|
|
*/ |
|
12
|
|
|
class EntityManager |
|
13
|
|
|
{ |
|
14
|
|
|
use Injector; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string エンティティクラスパス |
|
18
|
|
|
*/ |
|
19
|
|
|
private $classpath; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array<string> カラムメタ情報 |
|
23
|
|
|
*/ |
|
24
|
|
|
private $columnMeta; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* コンストラクタ |
|
28
|
|
|
* @param string エンティティクラスパス |
|
|
|
|
|
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($classpath) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->classpath = $classpath; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* カラムメタ情報を設定する |
|
37
|
|
|
* @param string カラムメタ情報 |
|
|
|
|
|
|
38
|
|
|
*/ |
|
39
|
|
|
public function setColumnMeta(array $columnMeta) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->columnMeta = $columnMeta; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* 列データをエンティティに変換して返却する |
|
46
|
|
|
* @param array<string> 列データ |
|
|
|
|
|
|
47
|
|
|
* @return object 列データエンティティ |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getEntity($row) |
|
50
|
|
|
{ |
|
51
|
|
|
$instance = new $this->classpath(); |
|
52
|
|
|
$propertyMap = null; |
|
53
|
|
|
|
|
54
|
|
|
// EntityPropertyを使っている場合はリフレクションを使用しない |
|
55
|
|
|
if (!array_key_exists("WebStream\Database\EntityProperty", class_uses($instance))) { |
|
56
|
|
|
$propertyMap = []; |
|
57
|
|
|
$refClass = new \ReflectionClass($instance); |
|
58
|
|
|
$properties = $refClass->getProperties(); |
|
59
|
|
|
foreach ($properties as $property) { |
|
60
|
|
|
if ($property->isPrivate() || $property->isProtected()) { |
|
61
|
|
|
$property->setAccessible(true); |
|
62
|
|
|
} |
|
63
|
|
|
$propertyMap[strtolower($property->getName())] = $property; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
foreach ($row as $col => $value) { |
|
68
|
|
|
switch ($this->columnMeta[$col]) { |
|
69
|
|
|
case 'LONG': // mysql:int |
|
70
|
|
|
case 'SHORT': // mysql:smallint |
|
71
|
|
|
case 'int4': // postgres:int |
|
72
|
|
|
case 'int2': // postgres:smallint |
|
73
|
|
|
case 'integer': // sqlite:int |
|
74
|
|
|
case 'smallint': // sqlite:smallint |
|
75
|
|
|
$value = intval($value); |
|
76
|
|
|
break; |
|
77
|
|
|
case 'LONGLONG': // mysql:bigint |
|
78
|
|
|
case 'int8': // postgres:bigint |
|
79
|
|
|
case 'bigint': // sqlite:bigint |
|
80
|
|
|
$value = doubleval($value); |
|
81
|
|
|
break; |
|
82
|
|
|
case 'DATETIME': // mysql:datetime |
|
83
|
|
|
case 'DATE': // mysql:date |
|
84
|
|
|
case 'timestamp': // postgres:timestamp, sqlite:timestamp |
|
85
|
|
|
case 'date': // postgres:date, sqlite:date |
|
86
|
|
|
$value = new \DateTime($value); |
|
87
|
|
|
break; |
|
88
|
|
|
default: // string |
|
89
|
|
|
break; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$col = strtolower(preg_replace_callback('/_([a-zA-Z])/', function ($matches) { |
|
93
|
|
|
return ucfirst($matches[1]); |
|
94
|
|
|
}, $col)); |
|
95
|
|
|
|
|
96
|
|
|
if ($propertyMap === null) { |
|
97
|
|
|
$instance->{$col} = $value; |
|
98
|
|
|
} else { |
|
99
|
|
|
if (array_key_exists($col, $propertyMap)) { |
|
100
|
|
|
$propertyMap[$col]->setValue($instance, $value); |
|
101
|
|
|
} else { |
|
102
|
|
|
$this->logger->error("Column '$col' is failed mapping in " . $this->classpath); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $instance; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|