Completed
Pull Request — master (#1632)
by Kentaro
32:45
created

EntityUtil::dumpToArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
crap 12
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
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
30
{
31
32
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$entity" missing
Loading history...
33
     * LAZY loading したエンティティの有無をチェックする.
34
     *
35
     * 削除済みのエンティティを LAZY loading した場合、 soft_delete filter で
36
     * フィルタリングされてしまい、正常に取得することができない.
37
     * しかし、 Proxy オブジェクトとして取得されるため、この関数を使用して
38
     * 有無をチェックする.
39
     * この関数を使用せず、該当のオブジェクトのプロパティを取得しようとすると、
40
     * EntityNotFoundException がスローされてしまう.
41
     *
42
     * @param $entity LAZY loading したエンティティ
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
43
     * @return bool エンティティが削除済みの場合 true
44
     * @see https://github.com/EC-CUBE/ec-cube/pull/602#issuecomment-125431246
45
     */
46 6
    public static function isEmpty($entity)
47
    {
48
        if ($entity instanceof Proxy) {
49
            try {
50
                $entity->__load();
51
            } catch (EntityNotFoundException $e) {
52
                return true;
53
            }
54
            return false;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
55
        } else {
56 6
            return empty($entity);
57
        }
58
    }
59
60
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$entity" missing
Loading history...
61
     * LAZY loading したエンティティの有無をチェックする.
62
     *
63
     * EntityUtil::isEmpty() の逆の結果を返します.
64
     *
65
     * @param $entity
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
66
     * @return bool
67
     * @see EntityUtil::isEmpty()
68
     */
69
    public static function isNotEmpty($entity)
70
    {
71
        return !self::isEmpty($entity);
72
    }
73
74
    /**
75
     * エンティティのプロパティを配列で返す.
76
     *
77
     * このメソッドはエンティティの内容をログ出力する際などに使用する.
78
     * AbstractEntity::toArray() と異なり再帰処理しない.
79
     * プロパティの値がオブジェクトの場合は、クラス名を出力する.
80
     *
81
     * @param object $entity 対象のエンティティ
82
     * @return array エンティティのプロパティの配列
83
     */
84
    public static function dumpToArray($entity)
85
    {
86
        $objReflect = new \ReflectionClass($entity);
87
        $arrProperties = $objReflect->getProperties();
88
        $arrResults = array();
89
        foreach ($arrProperties as $objProperty) {
90
            $objProperty->setAccessible(true);
91
            $name = $objProperty->getName();
92
            $value = $objProperty->getValue($entity);
93
            $arrResults[$name] = is_object($value) ? get_class($value) : $value;
94
        }
95
        return $arrResults;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
96
    }
97
}
98