|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Simples\Persistence; |
| 4 | + |
| 5 | +use Simples\Error\SimplesRunTimeError; |
| 6 | +use Simples\Kernel\App; |
| 7 | + |
| 8 | +/** |
| 9 | + * Class Engine |
| 10 | + * @package Simples\Persistence |
| 11 | + * |
| 12 | + * @method $this source (string $source) |
| 13 | + * @method $this relation (array $relations) |
| 14 | + * @method $this fields (array $fields) |
| 15 | + * @method $this filter (array $filter) |
| 16 | + * @method $this order (array $order) |
| 17 | + * @method $this group (array $group) |
| 18 | + * @method $this having (array $having) |
| 19 | + * @method $this limit (array $limit) |
| 20 | + * |
| 21 | + * @method $this log (bool $active) |
| 22 | + */ |
| 23 | +class Engine |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var Driver |
| 27 | + */ |
| 28 | + private $driver; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var array |
| 32 | + */ |
| 33 | + private $clausules = []; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var array|mixed |
| 37 | + */ |
| 38 | + private $settings = []; |
| 39 | + |
| 40 | + /** |
| 41 | + * Engine constructor. |
| 42 | + * @param $id |
| 43 | + */ |
| 44 | + public function __construct($id) |
| 45 | + { |
| 46 | + $this->settings = off(App::config('database'), $id); |
| 47 | + if ($this->settings) { |
| 48 | + $this->driver = Factory::create($this->settings); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param $name |
| 54 | + * @param $arguments |
| 55 | + * @return $this |
| 56 | + */ |
| 57 | + public function __call($name, $arguments) |
| 58 | + { |
| 59 | + $clausule = $arguments[0]; |
| 60 | + if (count($arguments) > 1) { |
| 61 | + $clausule = $arguments; |
| 62 | + } |
| 63 | + $name = strtolower($name); |
| 64 | + |
| 65 | + $this->clausules[$name] = $clausule; |
| 66 | + if (is_null($clausule)) { |
| 67 | + unset($this->clausules[$name]); |
| 68 | + } |
| 69 | + |
| 70 | + return $this; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @return Driver |
| 75 | + * @throws SimplesRunTimeError |
| 76 | + */ |
| 77 | + protected function driver(): Driver |
| 78 | + { |
| 79 | + if ($this->driver) { |
| 80 | + return $this->driver; |
| 81 | + } |
| 82 | + throw new SimplesRunTimeError("Cant use the driver", $this->settings); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @param array $values |
| 87 | + * @return string |
| 88 | + */ |
| 89 | + final public function register($values): string |
| 90 | + { |
| 91 | + return $this->driver()->create($this->clausules, $values); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @param $values |
| 96 | + * @return array |
| 97 | + */ |
| 98 | + final public function recover($values = []): array |
| 99 | + { |
| 100 | + return $this->driver()->read($this->clausules, $values); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @param $values |
| 105 | + * @param $filters |
| 106 | + * @return int |
| 107 | + */ |
| 108 | + final public function change($values, $filters = []): int |
| 109 | + { |
| 110 | + return $this->driver()->update($this->clausules, $values, $filters); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @param $filters |
| 115 | + * @return int |
| 116 | + */ |
| 117 | + final public function remove($filters): int |
| 118 | + { |
| 119 | + return $this->driver()->destroy($this->clausules, $filters); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @return Driver |
| 124 | + */ |
| 125 | + public function getDriver(): Driver |
| 126 | + { |
| 127 | + return $this->driver; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @return array |
| 132 | + */ |
| 133 | + public function getClausules(): array |
| 134 | + { |
| 135 | + return $this->clausules; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @return array|mixed |
| 140 | + */ |
| 141 | + public function getSettings() |
| 142 | + { |
| 143 | + return $this->settings; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Clear the clausules changes |
| 148 | + */ |
| 149 | + public function reset() |
| 150 | + { |
| 151 | + $this->clausules = []; |
| 152 | + } |
| 153 | +} |
0 commit comments