Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: marek
- * Date: 26. 11. 2015
- * Time: 17:49
- */
- namespace Application\Service\Database;
- use Application\Service\Database\Exception\ORMException;
- use Application\Support\Collection\LangableEntityCollection;
- use Application\Support\Str;
- class ORM extends Builder
- {
- protected $table;
- protected $entity;
- protected $langable;
- protected $map;
- protected $langField;
- protected $mainIndex;
- public function __construct()
- {
- $config = container()->make('config');
- if (!isset($this->map) || !($map = $config->get('orm/' . $this->map))) {
- throw new ORMException('Cannot read model map');
- }
- $this->readMap($map);
- parent::__construct();
- }
- public function get(array $fields = array())
- {
- $query = $this->make()
- ->select()
- ->from($this->table);
- foreach ($fields as $field => $value) {
- $query = $query->where('{:' . $field . ':}', $value);
- }
- return $query;
- }
- public function find($id)
- {
- return $this->get(array($this->mainIndex => $id))->getOne();
- }
- public function all()
- {
- return $this->get()->getCollection();
- }
- public function getOne()
- {
- if (!($result = parent::getResult())) {
- throw new ORMException('Cannot cast empty result to collection');
- }
- if (!db_num_rows($result)) {
- return false;
- }
- if ($this->langable) {
- $collection = new LangableEntityCollection;
- $previousIndex = null;
- while ($data = db_fetch_assoc($result)) {
- if ($previousIndex !== null && $data[$this->mainIndex] != $previousIndex) {
- return $collection;
- }
- if ($collection->exists($data[$this->langField])) {
- throw new ORMException('Multiple results with the same language exist');
- }
- $collection->addEntity($this->createEntityFromArray($data), $data[$this->langField]);
- $previousIndex = $data[$this->mainIndex];
- }
- return $collection;
- }
- return $this->createEntityFromArray(db_fetch_assoc($result));
- }
- public function getCollection()
- {
- if (!($result = parent::getResult())) {
- throw new ORMException('Cannot cast empty result to collection');
- }
- if (!db_num_rows($result)) {
- return false;
- }
- $previousIndex = null;
- $outputCollection = array();
- $i = 0;
- while ($data = db_fetch_assoc($result)) {
- if ($this->langable) {
- if ($data[$this->mainIndex] != $previousIndex) {
- $outputCollection[++$i] = new LangableEntityCollection;
- }
- $outputCollection[$i]->addEntity($this->createEntityFromArray($data), $data[$this->langField]);
- } else {
- $outputCollection[++$i] = $this->createEntityFromArray($data);
- }
- $previousIndex = $data[$this->mainIndex];
- }
- return $outputCollection;
- }
- public function getSql()
- {
- $sql = parent::getSql();
- $toFind = array_values($this->map);
- array_map(function($element) {
- return '{:' . $element . ':}';
- }, $toFind);
- $toReplace = array_keys($this->map);
- $sql = str_replace($toFind, $toReplace, $sql);
- }
- protected function readMap(array $config)
- {
- if (!isset($config['meta']) ||
- !isset($config['meta']['table']) ||
- !isset($config['meta']['entity']) ||
- !isset($config['meta']['main_index']))
- {
- throw new ORMException('Model is not fully configured');
- }
- $table = $config['meta']['table'];
- $entity = $config['meta']['entity'];
- $langable = $config['meta']['langable'] ?: false;
- $langField = $config['meta']['lang_field'] ?: null;
- $mainIndex = $config['meta']['main_index'];
- $map = $config['map'];
- if ($langField === null && $langable) {
- throw new ORMException('Lang field must be filled in on langable entities');
- }
- $this->table = $table;
- $this->entity = $entity;
- $this->langable = $langable;
- $this->langField = $langField;
- $this->mainIndex = $mainIndex;
- $this->map = $map;
- }
- protected function createEntity()
- {
- return new $this->entity;
- }
- protected function createEntityFromArray($data)
- {
- $entity = $this->createEntity();
- foreach ($this->map as $realField => $snakeCaseAttribute) {
- $setter = 'set' . Str::studly($snakeCaseAttribute);
- if (method_exists($entity, $setter) && isset($data[$realField])) {
- call_user_func_array(array($entity, $setter), array($data[$realField]));
- }
- }
- return $entity;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement