bundles/Factory/SupportBundle/Contract/DataMapper/AbstractDataMapper.php line 33

Open in your IDE?
  1. <?php
  2. namespace Factory\SupportBundle\Contract\DataMapper;
  3. use Factory\SupportBundle\Contract\DataMapper\ListDataMapper;
  4. abstract class AbstractDataMapper
  5. {
  6.     /**
  7.      * @var \Pimcore\Model\DataObject\Concrete
  8.      */
  9.     protected $resource;
  10.     protected static $translator;
  11.     public function __construct($resource)
  12.     {
  13.         $this->resource $resource;
  14.     }
  15.     public static function setTransaltor($translator)
  16.     {
  17.         self::$translator $translator;
  18.     }
  19.     protected function trans($text)
  20.     {
  21.         return self::$translator->trans($text);
  22.     }
  23.     abstract public function toArray($request);
  24.     public function all($request)
  25.     {
  26.         if (empty($this->resource)) {
  27.             return $this->resource;
  28.         }
  29.         $result = [];
  30.         $data $this->toArray($request);
  31.         if (empty($data)) {
  32.             return $data;
  33.         }
  34.         foreach ($data as $fieldName => $value) {
  35.             if ($value instanceof self) {
  36.                 $result[$fieldName] = $value->all($request);
  37.                 continue;
  38.             }
  39.             $result[$fieldName] = $value;
  40.         }
  41.         return $result;
  42.     }
  43.     public static function list(array $list)
  44.     {
  45.         $arguments func_get_args();
  46.         array_shift($arguments);
  47.         return new ListDataMapper($list, static::class, $arguments);
  48.     }
  49.     public function __call($name$arguments)
  50.     {
  51.         if ($this->resource === null) {
  52.             $dataMapper = static::class;
  53.             throw new \Exception("Try call method {$method} on null in {$dataMapper}");
  54.         }
  55.         try {
  56.             return call_user_func_array([$this->resource$name], $arguments);
  57.         } catch (\Throwable $th) {
  58.             $dataMapper = static::class;
  59.             $class get_class($this->resource);
  60.             $method $name;
  61.             throw new \Exception("Exception occures in class {$dataMapper} while try to call {$class}::{$method}. ".$th->getMessage());
  62.         }
  63.     }
  64.     public function __get($name)
  65.     {
  66.         return $this->resource->{$name};
  67.     }
  68. }