|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of EC-CUBE |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* http://www.lockon.co.jp/ |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software; you can redistribute it and/or |
|
10
|
|
|
* modify it under the terms of the GNU General Public License |
|
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
12
|
|
|
* of the License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with this program; if not, write to the Free Software |
|
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace Eccube\Util; |
|
25
|
|
|
|
|
26
|
|
|
use Doctrine\ORM\EntityNotFoundException; |
|
27
|
|
|
use Doctrine\ORM\Proxy\Proxy; |
|
28
|
|
|
|
|
29
|
|
|
class EntityUtil |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
|
|
|
|
|
33
|
|
|
* LAZY loading したエンティティの有無をチェックする. |
|
34
|
|
|
* |
|
35
|
|
|
* 削除済みのエンティティを LAZY loading した場合、 soft_delete filter で |
|
36
|
|
|
* フィルタリングされてしまい、正常に取得することができない. |
|
37
|
|
|
* しかし、 Proxy オブジェクトとして取得されるため、この関数を使用して |
|
38
|
|
|
* 有無をチェックする. |
|
39
|
|
|
* この関数を使用せず、該当のオブジェクトのプロパティを取得しようとすると、 |
|
40
|
|
|
* EntityNotFoundException がスローされてしまう. |
|
41
|
|
|
* |
|
42
|
|
|
* @param $entity LAZY loading したエンティティ |
|
|
|
|
|
|
43
|
|
|
* @return bool エンティティが削除済みの場合 true |
|
44
|
|
|
* @see https://github.com/EC-CUBE/ec-cube/pull/602#issuecomment-125431246 |
|
45
|
|
|
*/ |
|
46
|
200 |
|
public static function isEmpty($entity) |
|
47
|
|
|
{ |
|
48
|
200 |
|
if ($entity instanceof Proxy) { |
|
49
|
|
|
try { |
|
50
|
42 |
|
$entity->__load(); |
|
51
|
2 |
|
} catch (EntityNotFoundException $e) { |
|
52
|
2 |
|
return true; |
|
53
|
|
|
} |
|
54
|
40 |
|
return false; |
|
|
|
|
|
|
55
|
|
|
} else { |
|
56
|
198 |
|
return empty($entity); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
|
|
|
|
|
61
|
|
|
* LAZY loading したエンティティの有無をチェックする. |
|
62
|
|
|
* |
|
63
|
|
|
* EntityUtil::isEmpty() の逆の結果を返します. |
|
64
|
|
|
* |
|
65
|
|
|
* @param $entity |
|
|
|
|
|
|
66
|
|
|
* @return bool |
|
67
|
|
|
* @see EntityUtil::isEmpty() |
|
68
|
|
|
*/ |
|
69
|
2 |
|
public static function isNotEmpty($entity) |
|
70
|
|
|
{ |
|
71
|
2 |
|
return !self::isEmpty($entity); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* エンティティのプロパティを配列で返す. |
|
76
|
|
|
* |
|
77
|
|
|
* このメソッドはエンティティの内容をログ出力する際などに使用する. |
|
78
|
|
|
* AbstractEntity::toArray() と異なり再帰処理しない. |
|
79
|
|
|
* プロパティの値がオブジェクトの場合は、クラス名を出力する. |
|
80
|
|
|
* |
|
81
|
|
|
* @param object $entity 対象のエンティティ |
|
82
|
|
|
* @return array エンティティのプロパティの配列 |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public static function dumpToArray($entity) |
|
85
|
|
|
{ |
|
86
|
1 |
|
$objReflect = new \ReflectionClass($entity); |
|
87
|
1 |
|
$arrProperties = $objReflect->getProperties(); |
|
88
|
1 |
|
$arrResults = array(); |
|
89
|
1 |
|
foreach ($arrProperties as $objProperty) { |
|
90
|
1 |
|
$objProperty->setAccessible(true); |
|
91
|
1 |
|
$name = $objProperty->getName(); |
|
92
|
1 |
|
$value = $objProperty->getValue($entity); |
|
93
|
1 |
|
$arrResults[$name] = is_object($value) ? get_class($value) : $value; |
|
94
|
|
|
} |
|
95
|
1 |
|
return $arrResults; |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|