Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.15 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: marek
  5.  * Date: 26. 11. 2015
  6.  * Time: 17:49
  7.  */
  8.  
  9. namespace Application\Service\Database;
  10.  
  11. use Application\Service\Database\Exception\ORMException;
  12. use Application\Support\Collection\LangableEntityCollection;
  13. use Application\Support\Str;
  14.  
  15. class ORM extends Builder
  16. {
  17.     protected $table;
  18.  
  19.     protected $entity;
  20.  
  21.     protected $langable;
  22.  
  23.     protected $map;
  24.  
  25.     protected $langField;
  26.  
  27.     protected $mainIndex;
  28.  
  29.     public function __construct()
  30.     {
  31.         $config = container()->make('config');
  32.  
  33.         if (!isset($this->map) || !($map = $config->get('orm/' . $this->map))) {
  34.             throw new ORMException('Cannot read model map');
  35.         }
  36.  
  37.         $this->readMap($map);
  38.  
  39.         parent::__construct();
  40.     }
  41.  
  42.     public function get(array $fields = array())
  43.     {
  44.         $query = $this->make()
  45.             ->select()
  46.             ->from($this->table);
  47.  
  48.         foreach ($fields as $field => $value) {
  49.             $query = $query->where('{:' . $field . ':}', $value);
  50.         }
  51.  
  52.         return $query;
  53.     }
  54.  
  55.     public function find($id)
  56.     {
  57.         return $this->get(array($this->mainIndex => $id))->getOne();
  58.     }
  59.  
  60.     public function all()
  61.     {
  62.         return $this->get()->getCollection();
  63.     }
  64.  
  65.     public function getOne()
  66.     {
  67.         if (!($result = parent::getResult())) {
  68.             throw new ORMException('Cannot cast empty result to collection');
  69.         }
  70.  
  71.         if (!db_num_rows($result)) {
  72.             return false;
  73.         }
  74.  
  75.         if ($this->langable) {
  76.             $collection = new LangableEntityCollection;
  77.             $previousIndex = null;
  78.  
  79.             while ($data = db_fetch_assoc($result)) {
  80.                 if ($previousIndex !== null && $data[$this->mainIndex] != $previousIndex) {
  81.                     return $collection;
  82.                 }
  83.  
  84.                 if ($collection->exists($data[$this->langField])) {
  85.                     throw new ORMException('Multiple results with the same language exist');
  86.                 }
  87.  
  88.                 $collection->addEntity($this->createEntityFromArray($data), $data[$this->langField]);
  89.  
  90.                 $previousIndex = $data[$this->mainIndex];
  91.             }
  92.  
  93.             return $collection;
  94.         }
  95.  
  96.         return $this->createEntityFromArray(db_fetch_assoc($result));
  97.     }
  98.  
  99.     public function getCollection()
  100.     {
  101.         if (!($result = parent::getResult())) {
  102.             throw new ORMException('Cannot cast empty result to collection');
  103.         }
  104.  
  105.         if (!db_num_rows($result)) {
  106.             return false;
  107.         }
  108.  
  109.         $previousIndex = null;
  110.  
  111.         $outputCollection = array();
  112.         $i = 0;
  113.  
  114.         while ($data = db_fetch_assoc($result)) {
  115.             if ($this->langable) {
  116.                 if ($data[$this->mainIndex] != $previousIndex) {
  117.                     $outputCollection[++$i] = new LangableEntityCollection;
  118.                 }
  119.  
  120.                 $outputCollection[$i]->addEntity($this->createEntityFromArray($data), $data[$this->langField]);
  121.             } else {
  122.                 $outputCollection[++$i] = $this->createEntityFromArray($data);
  123.             }
  124.  
  125.             $previousIndex = $data[$this->mainIndex];
  126.         }
  127.  
  128.         return $outputCollection;
  129.     }
  130.  
  131.     public function getSql()
  132.     {
  133.         $sql = parent::getSql();
  134.  
  135.         $toFind = array_values($this->map);
  136.  
  137.         array_map(function($element) {
  138.             return '{:' . $element . ':}';
  139.         }, $toFind);
  140.  
  141.         $toReplace = array_keys($this->map);
  142.  
  143.         $sql = str_replace($toFind, $toReplace, $sql);
  144.     }
  145.  
  146.     protected function readMap(array $config)
  147.     {
  148.         if (!isset($config['meta']) ||
  149.             !isset($config['meta']['table']) ||
  150.             !isset($config['meta']['entity']) ||
  151.             !isset($config['meta']['main_index']))
  152.         {
  153.             throw new ORMException('Model is not fully configured');
  154.         }
  155.  
  156.         $table = $config['meta']['table'];
  157.         $entity = $config['meta']['entity'];
  158.         $langable = $config['meta']['langable'] ?: false;
  159.         $langField = $config['meta']['lang_field'] ?: null;
  160.         $mainIndex = $config['meta']['main_index'];
  161.         $map = $config['map'];
  162.  
  163.         if ($langField === null && $langable) {
  164.             throw new ORMException('Lang field must be filled in on langable entities');
  165.         }
  166.  
  167.         $this->table = $table;
  168.         $this->entity = $entity;
  169.         $this->langable = $langable;
  170.         $this->langField = $langField;
  171.         $this->mainIndex = $mainIndex;
  172.         $this->map = $map;
  173.     }
  174.  
  175.     protected function createEntity()
  176.     {
  177.         return new $this->entity;
  178.     }
  179.  
  180.     protected function createEntityFromArray($data)
  181.     {
  182.         $entity = $this->createEntity();
  183.  
  184.         foreach ($this->map as $realField => $snakeCaseAttribute) {
  185.             $setter = 'set' . Str::studly($snakeCaseAttribute);
  186.  
  187.             if (method_exists($entity, $setter) && isset($data[$realField])) {
  188.                 call_user_func_array(array($entity, $setter), array($data[$realField]));
  189.             }
  190.         }
  191.  
  192.         return $entity;
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement