src/Eccube/Util/EntityUtil.php line 68

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Util;
  13. use Doctrine\ORM\EntityNotFoundException;
  14. use Doctrine\ORM\Proxy\Proxy;
  15. class EntityUtil
  16. {
  17.     /**
  18.      * LAZY loading したエンティティの有無をチェックする.
  19.      *
  20.      * 削除済みのエンティティを LAZY loading した場合、 soft_delete filter で
  21.      * フィルタリングされてしまい、正常に取得することができない.
  22.      * しかし、 Proxy オブジェクトとして取得されるため、この関数を使用して
  23.      * 有無をチェックする.
  24.      * この関数を使用せず、該当のオブジェクトのプロパティを取得しようとすると、
  25.      * EntityNotFoundException がスローされてしまう.
  26.      *
  27.      * @param $entity LAZY loading したエンティティ
  28.      *
  29.      * @return bool エンティティが削除済みの場合 true
  30.      *
  31.      * @see https://github.com/EC-CUBE/ec-cube/pull/602#issuecomment-125431246
  32.      * @deprecated
  33.      */
  34.     public static function isEmpty($entity)
  35.     {
  36.         @trigger_error('The '.__METHOD__.' method is deprecated.'E_USER_DEPRECATED);
  37.         if ($entity instanceof Proxy) {
  38.             try {
  39.                 $entity->__load();
  40.             } catch (EntityNotFoundException $e) {
  41.                 return true;
  42.             }
  43.             return false;
  44.         } else {
  45.             return empty($entity);
  46.         }
  47.     }
  48.     /**
  49.      * LAZY loading したエンティティの有無をチェックする.
  50.      *
  51.      * EntityUtil::isEmpty() の逆の結果を返します.
  52.      *
  53.      * @param $entity
  54.      *
  55.      * @return bool
  56.      *
  57.      * @see EntityUtil::isEmpty()
  58.      * @deprecated
  59.      */
  60.     public static function isNotEmpty($entity)
  61.     {
  62.         @trigger_error('The '.__METHOD__.' method is deprecated.'E_USER_DEPRECATED);
  63.         return !self::isEmpty($entity);
  64.     }
  65.     /**
  66.      * エンティティのプロパティを配列で返す.
  67.      *
  68.      * このメソッドはエンティティの内容をログ出力する際などに使用する.
  69.      * AbstractEntity::toArray() と異なり再帰処理しない.
  70.      * プロパティの値がオブジェクトの場合は、クラス名を出力する.
  71.      *
  72.      * @param object $entity 対象のエンティティ
  73.      *
  74.      * @return array エンティティのプロパティの配列
  75.      */
  76.     public static function dumpToArray($entity)
  77.     {
  78.         $objReflect = new \ReflectionClass($entity);
  79.         $arrProperties $objReflect->getProperties();
  80.         $arrResults = [];
  81.         foreach ($arrProperties as $objProperty) {
  82.             $objProperty->setAccessible(true);
  83.             $name $objProperty->getName();
  84.             $value $objProperty->getValue($entity);
  85.             $arrResults[$name] = is_object($value) ? get_class($value) : $value;
  86.         }
  87.         return $arrResults;
  88.     }
  89. }