Compare commits
58 Commits
Author | SHA1 | Date | |
---|---|---|---|
81ad81f7bf | |||
7fc7de7390 | |||
55275d2d75 | |||
12065b376b | |||
3bc54fb9d1 | |||
230faaf3cc | |||
7f33475e35 | |||
8beb07819c | |||
91886eca0f | |||
2d44c5d057 | |||
123d46d33c | |||
fbd5b350bd | |||
65c224c636 | |||
2bf938a9b7 | |||
6cd26a88ea | |||
293b5af3ff | |||
022ba575b7 | |||
643c3e714f | |||
db2864395c | |||
1cd06d5fe6 | |||
c913f65b91 | |||
244d8cc414 | |||
c8a7781c88 | |||
630c971b45 | |||
1505539e61 | |||
c441d41a02 | |||
3087a48c43 | |||
3aed13e6a0 | |||
43f545516d | |||
4d64143dbc | |||
b757ed19b2 | |||
1dc21d8fb7 | |||
9dc71e4d77 | |||
65bec43b45 | |||
c6806a1c62 | |||
5f3f6b72e5 | |||
6fd19a11be | |||
a1466143b5 | |||
7c727d93e9 | |||
a8d548c0c4 | |||
e02b8c4063 | |||
f03df583d7 | |||
89d1db7a7e | |||
17453427a2 | |||
8dc0a27fd9 | |||
5b1a61cd3b | |||
ae172b902c | |||
d3cb68c5ca | |||
7f81b987c9 | |||
0a46604e0c | |||
af801e769f | |||
e9bee7fa48 | |||
c40baaad3f | |||
ac019aac3f | |||
a82fdce64b | |||
86ffb0b84c | |||
8126b1f67d | |||
7899b9f6c8 |
7
.gitignore
vendored
7
.gitignore
vendored
@ -1,3 +1,6 @@
|
||||
# Composer
|
||||
/vendor/
|
||||
composer.lock
|
||||
**/vendor/
|
||||
**/*.lock
|
||||
|
||||
# PHPStorm
|
||||
**/.idea/
|
||||
|
@ -1,14 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Alias;
|
||||
|
||||
use ProVM\Common\Factory\Model as ModelFactory;
|
||||
|
||||
interface Model {
|
||||
public function getTable(): string;
|
||||
public function setFactory(ModelFactory $factory): Model;
|
||||
public function parentOf(string $child_model_class, array $relation_definitions): array;
|
||||
public function childOf(string $parent_model_class, array $relation_definitions): Model;
|
||||
public function siblingOf(string $sibling_model_class, string $connecting_table, array $relation_definitions): array;
|
||||
|
||||
public function toArray(): array;
|
||||
}
|
@ -1,124 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Define;
|
||||
|
||||
use \Model as BaseModel;
|
||||
use ProVM\Common\Alias\Model as ModelInterface;
|
||||
|
||||
abstract class Model extends BaseModel implements ModelInterface {
|
||||
const SELF_KEY = 'self_key';
|
||||
const PARENT_KEY = 'parent_key';
|
||||
const CHILD_KEY = 'child_key';
|
||||
const SIBLING_KEY = 'sibling_key';
|
||||
const SELF_CONNECT_KEY = 'self_connect_key';
|
||||
const SIBLING_CONNECT_KEY = 'sibling_connect_key';
|
||||
|
||||
public function getTable() {
|
||||
return static::$_table;
|
||||
}
|
||||
|
||||
protected $factory;
|
||||
public function setFactory(ModelFactory $factory): ModelInterface {
|
||||
$this->factory = $factory;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function checkDefinitions(array $definitions, array $required, array $default) {
|
||||
foreach ($default as $key => $value) {
|
||||
if (!isset($definitions[$key])) {
|
||||
$definitions[$key] = $value;
|
||||
}
|
||||
}
|
||||
foreach ($required as $definition) {
|
||||
if (!isset($definitions[$definition])) {
|
||||
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
|
||||
throw new \InvalidArgumentException($definition . ' is required for ' . $trace[1]['function'] . ' in ' . get_called_class());
|
||||
}
|
||||
}
|
||||
return $definitions;
|
||||
}
|
||||
public function parentOf(string $child_model_class, array $relation_definitions): array {
|
||||
$relation_definitions = $this->checkDefinitions($relation_definitions, [
|
||||
Model::SELF_KEY,
|
||||
Model::CHILD_KEY
|
||||
], [
|
||||
Model::SELF_KEY => 'id'
|
||||
]);
|
||||
return $this->factory
|
||||
->find($child_model_class)
|
||||
->where([
|
||||
[
|
||||
$relation_definitions[Model::CHILD_KEY],
|
||||
$this->{$relation_definitions[Model::SELF_KEY]}
|
||||
]
|
||||
])
|
||||
->many();
|
||||
}
|
||||
public function childOf(string $parent_model_class, array $relation_definitions): ModelInterface {
|
||||
$relation_definitions = $this->checkDefinitions($relation_definitions, [
|
||||
Model::SELF_KEY,
|
||||
Model::PARENT_KEY
|
||||
], [
|
||||
Model::PARENT_KEY => 'id'
|
||||
]);
|
||||
$parent_table = (new $parent_class())->getTable();
|
||||
return $this->factory
|
||||
->find($parent_model_class)
|
||||
->wherer([
|
||||
[
|
||||
$relation_definitions[Model::PARENT_KEY],
|
||||
$this->{$relation_definitions[Model::SELF_KEY]}
|
||||
]
|
||||
])
|
||||
->one();
|
||||
}
|
||||
public function siblingOf(string $sibling_model_class, string $connecting_table, array $relation_definitions): array {
|
||||
$relation_definitions = $this->checkDefinitions($relation_definitions, [
|
||||
Model::SELF_KEY,
|
||||
Model::SIBLING_KEY,
|
||||
Model::SELF_CONNECT_KEY,
|
||||
Model::SIBLING_CONNECT_KEY
|
||||
], [
|
||||
Model::SELF_KEY => 'id',
|
||||
Model::SIBLING_KEY => 'id'
|
||||
]);
|
||||
$sibling_table = (new $sibling_class())->getTable();
|
||||
return $this->find($sibling_model_class)
|
||||
->select([
|
||||
[
|
||||
$sibling_table,
|
||||
'*'
|
||||
],
|
||||
[
|
||||
$connecting_table,
|
||||
'*'
|
||||
]
|
||||
])
|
||||
->join([
|
||||
[
|
||||
$connecting_table,
|
||||
implode('.', [
|
||||
$connecting_table,
|
||||
$relation_definitions[Model::SIBLING_CONNECT_KEY]
|
||||
]),
|
||||
implode('.', [
|
||||
$sibling_table,
|
||||
$relation_definitions[Model::SIBLING_KEY]
|
||||
])
|
||||
]
|
||||
])
|
||||
->where([
|
||||
[
|
||||
implode('.', [
|
||||
$connecting_table,
|
||||
$relation_definitions[Model::SELF_CONNECT_KEY]
|
||||
]),
|
||||
$this->{$relation_definitions[Model::SELF_KEY]}
|
||||
]
|
||||
])
|
||||
->many();
|
||||
}
|
||||
|
||||
public function toArray(): array {
|
||||
return $this->asArray();
|
||||
}
|
||||
}
|
@ -1,365 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Factory;
|
||||
|
||||
use ORM;
|
||||
use Model as BaseModel;
|
||||
use ProVM\Common\Alias\Model as ModelInterface;
|
||||
|
||||
class Model {
|
||||
public function reset(): Model {
|
||||
$reset = [
|
||||
'class',
|
||||
'columns',
|
||||
'joins',
|
||||
'conditions',
|
||||
'grouping',
|
||||
'ordering',
|
||||
'limit',
|
||||
'offset'
|
||||
];
|
||||
foreach ($reset as $p) {
|
||||
$this->{$p} = null;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
protected $class;
|
||||
public function find(string $model_class): Model {
|
||||
if (!class_exists($model_class)) {
|
||||
throw new \InvalidArgumentException('El modelo ' . $model_class . ' no existe.');
|
||||
}
|
||||
$this->class = $model_class;
|
||||
return $this;
|
||||
}
|
||||
protected $columns;
|
||||
public function select($columns): Model {
|
||||
if ($this->columns === null) {
|
||||
$this->columns = [];
|
||||
}
|
||||
if (!is_array($columns)) {
|
||||
$columns = [$columns];
|
||||
}
|
||||
foreach ($columns as $c) {
|
||||
$col = (object) [
|
||||
'table' => '',
|
||||
'column' => $c
|
||||
];
|
||||
if (is_array($c)) {
|
||||
$col = (object) [
|
||||
'table' => $c['table'] ?? $c[0],
|
||||
'column' => $c['column'] ?? $c[1]
|
||||
];
|
||||
}
|
||||
$this->columns []= $col;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
protected $joins;
|
||||
public function join(array $joins): Model {
|
||||
if ($this->joins === null) {
|
||||
$this->joins = [];
|
||||
}
|
||||
foreach ($joins as $j) {
|
||||
$join = (object) [
|
||||
'table' => $j['table'] ?? $j[0],
|
||||
'from' => $j['from'] ?? $j[1],
|
||||
'to' => $j['to'] ?? $j[2],
|
||||
'sym' => $j['sym'] ?? ($j[3] ?? '='),
|
||||
'alias' => $j['alias'] ?? '',
|
||||
'type' => strtolower($j['type']) ?? '',
|
||||
'params' => $j['params'] ?? ''
|
||||
];
|
||||
$this->joins []= $join;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
protected $conditions;
|
||||
public function where(array $conditions): Model {
|
||||
if ($this->conditions === null) {
|
||||
$this->conditions = [];
|
||||
}
|
||||
foreach ($conditions as $c) {
|
||||
$cond = (object) [
|
||||
'column' => $j['column'] ?? $j[0],
|
||||
'value' => $j['value'] ?? $j[1],
|
||||
'sym' => strtolower($j['sym'] ?? ($j[2] ?? '=')),
|
||||
'type' => strtolower($j['type']) ?? ''
|
||||
];
|
||||
$this->conditions []= $cond;
|
||||
}
|
||||
return $this->conditions;
|
||||
}
|
||||
protected $grouping;
|
||||
public function group($groups): Model {
|
||||
if ($this->grouping === null) {
|
||||
$this->grouping = [];
|
||||
}
|
||||
if (!is_array($groups)) {
|
||||
$groups = [$groups];
|
||||
}
|
||||
foreach ($groups as $g) {
|
||||
$this->grouping []= $g;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
protected $ordering;
|
||||
public function order($orders): Model {
|
||||
if ($this->ordering === null) {
|
||||
$this->ordering = [];
|
||||
}
|
||||
if (!is_array($orders)) {
|
||||
$orders = [$orders];
|
||||
}
|
||||
foreach ($orders as $o) {
|
||||
$order = (object) [
|
||||
'column' => $o,
|
||||
'direction' => 'asc'
|
||||
];
|
||||
if (is_array($o)) {
|
||||
$order = (object) [
|
||||
'column' => $o['column'] ?? $o[0],
|
||||
'direction' => strtolower($o['direction']) ?? $o[1]
|
||||
];
|
||||
}
|
||||
$this->ordering []= $order;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
protected $limit;
|
||||
public function limit(int $limit): Model {
|
||||
$this->limit = $limit;
|
||||
}
|
||||
protected $offset;
|
||||
public function offset(int $offset): Model {
|
||||
$this->offset = $offset;
|
||||
}
|
||||
|
||||
protected function parseSelect(ORM $orm): ORM {
|
||||
if ($this->columns === null) {
|
||||
return $orm;
|
||||
}
|
||||
foreach ($this->columns as $col) {
|
||||
$orm = $orm->select(trim(implode('.', $col), '.'));
|
||||
}
|
||||
return $orm;
|
||||
}
|
||||
protected function parseJoin(ORM $orm): ORM {
|
||||
if ($this->joins === null) {
|
||||
return $orm;
|
||||
}
|
||||
foreach ($this->joins as $join) {
|
||||
$method = 'join';
|
||||
switch ($join->type) {
|
||||
case 'raw':
|
||||
$method = 'rawJoin';
|
||||
break;
|
||||
case 'inner':
|
||||
$method = 'innerJoin';
|
||||
break;
|
||||
case 'left':
|
||||
case 'left outer':
|
||||
case 'left_outer':
|
||||
case 'leftouter':
|
||||
$method = 'leftOuterJoin';
|
||||
break;
|
||||
case 'right':
|
||||
case 'right outer':
|
||||
case 'right_outer':
|
||||
case 'rightouter':
|
||||
$method = 'rightOuterJoin';
|
||||
break;
|
||||
case 'full':
|
||||
case 'full outer':
|
||||
case 'full_outer':
|
||||
case 'fullouter':
|
||||
$method = 'fullOuterJoin';
|
||||
break;
|
||||
}
|
||||
if ($join->type == 'raw') {
|
||||
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to], $join->alias, $join->params);
|
||||
} elseif ($join->alias === '') {
|
||||
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to], $join->alias);
|
||||
} else {
|
||||
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to]);
|
||||
}
|
||||
}
|
||||
return $orm;
|
||||
}
|
||||
protected function parseWhere(ORM $orm): ORM {
|
||||
if ($this->conditions === null) {
|
||||
return $orm;
|
||||
}
|
||||
foreach ($this->conditions as $cond) {
|
||||
$method = 'where';
|
||||
switch ($cond->sym) {
|
||||
case '<':
|
||||
$method = 'whereLt';
|
||||
break;
|
||||
case '<=':
|
||||
$method = 'whereLte';
|
||||
break;
|
||||
case '>':
|
||||
$method = 'whereGt';
|
||||
break;
|
||||
case '>=':
|
||||
$method = 'whereGte';
|
||||
break;
|
||||
case '!=':
|
||||
$method = 'whereNotEqual';
|
||||
break;
|
||||
case 'like':
|
||||
$method = 'whereLike';
|
||||
break;
|
||||
case 'not like':
|
||||
case 'not_like':
|
||||
case 'notlike':
|
||||
$method = 'whereNotLike';
|
||||
break;
|
||||
}
|
||||
switch ($cond->type) {
|
||||
case 'equal':
|
||||
$method = 'whereEqual';
|
||||
break;
|
||||
case 'not equal':
|
||||
case 'not_equal':
|
||||
case 'notequal':
|
||||
$method = 'whereNotEqual';
|
||||
break;
|
||||
case 'less':
|
||||
case 'less than':
|
||||
case 'less_than':
|
||||
case 'lessthan':
|
||||
case 'lt':
|
||||
$method = 'whereLt';
|
||||
break;
|
||||
case 'less equal':
|
||||
case 'less_equal':
|
||||
case 'lessequal':
|
||||
case 'less than equal':
|
||||
case 'less_than_equal':
|
||||
case 'lessthanequal':
|
||||
case 'lte':
|
||||
$method = 'whereLte';
|
||||
break;
|
||||
case 'greater':
|
||||
case 'greater than':
|
||||
case 'greater_than':
|
||||
case 'greaterthan':
|
||||
case 'gt':
|
||||
$method = 'whereGt';
|
||||
break;
|
||||
case 'greater equal':
|
||||
case 'greater_equal':
|
||||
case 'greaterequal':
|
||||
case 'greater than equal':
|
||||
case 'greater_than_equal':
|
||||
case 'greaterthanequal':
|
||||
case 'gte':
|
||||
$method = 'whereGte';
|
||||
break;
|
||||
case 'like':
|
||||
$method = 'whereLike';
|
||||
break;
|
||||
case 'not like':
|
||||
case 'not_like':
|
||||
case 'notlike':
|
||||
$method = 'whereNotLike';
|
||||
break;
|
||||
case 'in':
|
||||
$method = 'whereIn';
|
||||
break;
|
||||
case 'not in':
|
||||
case 'not_in':
|
||||
case 'notin':
|
||||
$method = 'whereNotIn';
|
||||
break;
|
||||
case 'raw':
|
||||
$method = 'rawWhere';
|
||||
break;
|
||||
}
|
||||
$orm = $orm->{$method}($cond->column, $cond->value);
|
||||
}
|
||||
return $orm;
|
||||
}
|
||||
protected function parseGroup(ORM $orm): ORM {
|
||||
if ($this->grouping === null) {
|
||||
return $orm;
|
||||
}
|
||||
foreach ($this->grouping as $group) {
|
||||
if (strpos($group, '(') !== false) {
|
||||
$orm = $orm->groupByExpr($group);
|
||||
} else {
|
||||
$orm = $orm->groupBy($group);
|
||||
}
|
||||
}
|
||||
return $orm;
|
||||
}
|
||||
protected function parseOrder(ORM $orm): ORM {
|
||||
if ($this->ordering === null) {
|
||||
return $orm;
|
||||
}
|
||||
foreach ($this->ordering as $order) {
|
||||
if (strpos($order->column, '(') !== false) {
|
||||
$orm = $orm->orderByExpr($order->column);
|
||||
continue;
|
||||
}
|
||||
switch ($order->direction) {
|
||||
case 'asc':
|
||||
case 'ascending':
|
||||
$orm = $orm->orderByAsc($order->column);
|
||||
break;
|
||||
case 'desc':
|
||||
case 'descending':
|
||||
$orm = $orm->orderByDesc($order->column);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $orm;
|
||||
}
|
||||
protected function parseLimit(ORM $orm): ORM {
|
||||
if ($this->limit === null) {
|
||||
return $orm;
|
||||
}
|
||||
return $orm->limit($this->limit);
|
||||
}
|
||||
protected function parseOffset(ORM $orm): ORM {
|
||||
if ($this->offset === null) {
|
||||
return $orm;
|
||||
}
|
||||
return $orm->offset($this->offset);
|
||||
}
|
||||
|
||||
public function one(): ModelInterface {
|
||||
$result = $this->build()->findOne();
|
||||
$result->setFactory($this);
|
||||
return $result;
|
||||
}
|
||||
public function many(): array {
|
||||
$results = $this->build()->findMany();
|
||||
foreach ($results as &$r) {
|
||||
$r->setFactory($this);
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
public function array(): array {
|
||||
$results = $this->build()->findArray();
|
||||
return $results;
|
||||
}
|
||||
|
||||
protected function build(): ORM {
|
||||
$orm = BaseModel::factory($this->class);
|
||||
$methods = [
|
||||
'select',
|
||||
'join',
|
||||
'where',
|
||||
'group',
|
||||
'order',
|
||||
'limit',
|
||||
'offset'
|
||||
];
|
||||
foreach ($methods as $m) {
|
||||
$method = 'parse' . ucfirst($m);
|
||||
$orm = $this->{$method}($orm);
|
||||
}
|
||||
return $orm;
|
||||
}
|
||||
}
|
@ -1,13 +1,14 @@
|
||||
{
|
||||
"name": "provm/models",
|
||||
"description": "Model handling using j4mie/paris",
|
||||
"type": "library",
|
||||
"description": "Model with Repository for Database Mapping",
|
||||
"type": "project",
|
||||
"require": {
|
||||
"j4mie/paris": "^1.5"
|
||||
"provm/database": "^2",
|
||||
"provm/query_builder": "^1"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9.2",
|
||||
"kint-php/kint": "^3.3"
|
||||
"phpunit/phpunit": "^9",
|
||||
"kint-php/kint": "^4"
|
||||
},
|
||||
"license": "prioprietary",
|
||||
"authors": [
|
||||
@ -17,8 +18,26 @@
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"ProVM\\Common\\": "common"
|
||||
}
|
||||
"psr-4": {
|
||||
"ProVM\\": "src/"
|
||||
}
|
||||
},
|
||||
"repositories": [
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://git.provm.cl/ProVM/database.git"
|
||||
},
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://git.provm.cl/ProVM/query_builder.git"
|
||||
}
|
||||
],
|
||||
"config": {
|
||||
"http-basic": {
|
||||
"git.provm.cl": {
|
||||
"username": "aldarien",
|
||||
"password": "$&At'GQmd@Ul;=j@'2d#@N&H"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
64
src/Alias/Model.php
Normal file
64
src/Alias/Model.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
namespace ProVM\Alias;
|
||||
|
||||
use ProVM\Concept\Model\Factory;
|
||||
use ProVM\Concept\Model as ModelInterface;
|
||||
|
||||
abstract class Model implements ModelInterface
|
||||
{
|
||||
protected Factory $factory;
|
||||
public function setFactory(Factory $factory): ModelInterface
|
||||
{
|
||||
$this->factory = $factory;
|
||||
return $this;
|
||||
}
|
||||
public function getFactory(): Factory
|
||||
{
|
||||
return $this->factory;
|
||||
}
|
||||
protected int $id;
|
||||
public function setId(int $id): ModelInterface
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
protected bool $new;
|
||||
public function setNew(): ModelInterface
|
||||
{
|
||||
$this->new = true;
|
||||
return $this;
|
||||
}
|
||||
public function isNew(): bool
|
||||
{
|
||||
return $this->new ?? false;
|
||||
}
|
||||
protected bool $dirty;
|
||||
public function setDirty(): ModelInterface
|
||||
{
|
||||
$this->dirty = true;
|
||||
return $this;
|
||||
}
|
||||
public function isDirty(): bool
|
||||
{
|
||||
return $this->dirty ?? $this->isNew();
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
if ($this->isDirty()) {
|
||||
$this->getFactory()->get(get_class($this))->save($this);
|
||||
}
|
||||
}
|
||||
public function edit(array $data): void
|
||||
{
|
||||
foreach ($data as $key => $val) {
|
||||
$m = 'set' . ucwords($key);
|
||||
$this->{$m}($val);
|
||||
}
|
||||
$this->isDirty();
|
||||
}
|
||||
}
|
119
src/Alias/Model/Repository.php
Normal file
119
src/Alias/Model/Repository.php
Normal file
@ -0,0 +1,119 @@
|
||||
<?php
|
||||
namespace ProVM\Alias\Model;
|
||||
|
||||
use ProVM\Concept\Database\Connection;
|
||||
use ProVM\Concept\Database\QueryBuilder;
|
||||
use ProVM\Concept\Model;
|
||||
use ProVM\Concept\Model\Factory;
|
||||
use ProVM\Concept\Model\Repository as RepositoryInterface;
|
||||
|
||||
abstract class Repository implements RepositoryInterface
|
||||
{
|
||||
public function __construct(Connection $connection, QueryBuilder $builder, Factory $factory)
|
||||
{
|
||||
$this->setConnection($connection)
|
||||
->setQueryBuilder($builder)
|
||||
->setFactory($factory)
|
||||
->setup();
|
||||
}
|
||||
|
||||
protected Connection $connection;
|
||||
public function setConnection(Connection $connection): Repository
|
||||
{
|
||||
$this->connection = $connection;
|
||||
return $this;
|
||||
}
|
||||
public function getConnection(): Connection
|
||||
{
|
||||
return $this->connection;
|
||||
}
|
||||
protected QueryBuilder $builder;
|
||||
public function setQueryBuilder(QueryBuilder $builder): RepositoryInterface
|
||||
{
|
||||
$this->builder = $builder;
|
||||
return $this;
|
||||
}
|
||||
public function getQueryBuilder(): QueryBuilder
|
||||
{
|
||||
return $this->builder;
|
||||
}
|
||||
protected Factory $factory;
|
||||
public function setFactory(Factory $factory): RepositoryInterface
|
||||
{
|
||||
$this->factory = $factory;
|
||||
return $this;
|
||||
}
|
||||
public function getFactory(): Factory
|
||||
{
|
||||
return $this->factory;
|
||||
}
|
||||
protected string $table;
|
||||
public function setTable(string $table): RepositoryInterface
|
||||
{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
public function getTable(): string
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
protected array $columns;
|
||||
public function setColumns(array $columns): RepositoryInterface
|
||||
{
|
||||
foreach ($columns as $column) {
|
||||
$this->addColumn($column);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function addColumn(string $column): RepositoryInterface
|
||||
{
|
||||
$this->columns []= $column;
|
||||
return $this;
|
||||
}
|
||||
public function getColumns(): array
|
||||
{
|
||||
return $this->columns;
|
||||
}
|
||||
|
||||
public function save(Model $model): void
|
||||
{
|
||||
if (!$model->isDirty() and !$model->isNew()) {
|
||||
return;
|
||||
}
|
||||
$cols = [];
|
||||
$values = [];
|
||||
foreach ($this->getColumns() as $column) {
|
||||
$m = 'get' . ucwords($column);
|
||||
$cols []= '?';
|
||||
$values []= $model->{$m}();
|
||||
}
|
||||
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($this->getColumns())->values($cols);
|
||||
$this->getConnection()->execute($query, $values);
|
||||
}
|
||||
public function edit(Model $model, array $data): Model
|
||||
{
|
||||
foreach ($this->getColumns() as $col) {
|
||||
if (isset($data[$col])) {
|
||||
$m = 'set' . ucwords($col);
|
||||
$model->{$m}($data[$col]);
|
||||
}
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
public function fetchById(int $id): Model
|
||||
{
|
||||
$query = $this->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where([['id', '?']])
|
||||
->limit(1);
|
||||
return $this->load($this->getConnection()->execute($query, [$id])->getAsArray()[0]);
|
||||
}
|
||||
public function fetchAll(): array
|
||||
{
|
||||
$query = $this->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable());
|
||||
return array_map([$this, 'load'], $this->getConnection()->query($query)->getAsArray());
|
||||
}
|
||||
}
|
18
src/Concept/Model.php
Normal file
18
src/Concept/Model.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace ProVM\Concept;
|
||||
|
||||
use ProVM\Concept\Model\Factory;
|
||||
|
||||
interface Model
|
||||
{
|
||||
public function setFactory(Factory $factory): Model;
|
||||
public function getFactory(): Factory;
|
||||
public function setId(int $id): Model;
|
||||
public function getId(): int;
|
||||
public function setNew(): Model;
|
||||
public function isNew(): bool;
|
||||
public function setDirty(): Model;
|
||||
public function isDirty(): bool;
|
||||
public function save(): void;
|
||||
public function edit(array $data): void;
|
||||
}
|
13
src/Concept/Model/Factory.php
Normal file
13
src/Concept/Model/Factory.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace ProVM\Concept\Model;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
interface Factory
|
||||
{
|
||||
public function setContainer(ContainerInterface $container): Factory;
|
||||
public function getContainer(): ContainerInterface;
|
||||
public function setNamespace(string $namespace): Factory;
|
||||
public function getNamespace(): string;
|
||||
public function get(string $repository_name): Repository;
|
||||
}
|
28
src/Concept/Model/Repository.php
Normal file
28
src/Concept/Model/Repository.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
namespace ProVM\Concept\Model;
|
||||
|
||||
use ProVM\Concept\Database\Connection;
|
||||
use ProVM\Concept\Database\QueryBuilder;
|
||||
use ProVM\Concept\Model;
|
||||
|
||||
interface Repository
|
||||
{
|
||||
public function setConnection(Connection $connection): Repository;
|
||||
public function getConnection(): Connection;
|
||||
public function setQueryBuilder(QueryBuilder $builder): Repository;
|
||||
public function getQueryBuilder(): QueryBuilder;
|
||||
public function setFactory(Factory $factory): Repository;
|
||||
public function getFactory(): Factory;
|
||||
public function setup(): Repository;
|
||||
public function setTable(string $table): Repository;
|
||||
public function getTable(): string;
|
||||
public function setColumns(array $columns): Repository;
|
||||
public function addColumn(string $column): Repository;
|
||||
public function getColumns(): array;
|
||||
public function load(array $data): Model;
|
||||
public function save(Model $model): void;
|
||||
public function edit(Model $model, array $data): Model;
|
||||
public function create(array $data): Model;
|
||||
public function fetchById(int $id): Model;
|
||||
public function fetchAll(): array;
|
||||
}
|
46
src/Implement/Model/Factory.php
Normal file
46
src/Implement/Model/Factory.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Model;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ProVM\Concept\Model\Factory as FactoryInterface;
|
||||
use ProVM\Concept\Model\Repository;
|
||||
|
||||
class Factory implements FactoryInterface
|
||||
{
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->setContainer($container);
|
||||
}
|
||||
|
||||
protected ContainerInterface $container;
|
||||
public function setContainer(ContainerInterface $container): FactoryInterface
|
||||
{
|
||||
$this->container = $container;
|
||||
return $this;
|
||||
}
|
||||
public function getContainer(): ContainerInterface
|
||||
{
|
||||
return $this->container;
|
||||
}
|
||||
protected string $namespace;
|
||||
public function setNamespace(string $namespace): FactoryInterface
|
||||
{
|
||||
$this->namespace = $namespace;
|
||||
return $this;
|
||||
}
|
||||
public function getNamespace(): string
|
||||
{
|
||||
return $this->namespace;
|
||||
}
|
||||
protected function buildRepository(string $repository_name): string
|
||||
{
|
||||
return implode("\\", [
|
||||
$this->getNamespace(),
|
||||
$repository_name
|
||||
]);
|
||||
}
|
||||
public function get(string $repository_name): Repository
|
||||
{
|
||||
return $this->getContainer()->get($this->buildRepository($repository_name));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user