48 Commits

Author SHA1 Message Date
2a06507876 Merge branch 'develop' into release 2022-10-11 12:17:39 -03:00
e4cec2e2f2 trigger_error -> error_log 2022-10-11 12:17:30 -03:00
faf615e79d Merge branch 'develop' into release 2022-10-11 11:41:05 -03:00
8a03d9e675 Reorder and cleanup of Repository 2022-10-11 11:39:25 -03:00
b8409182d7 Merge branch 'develop' into release 2022-10-10 22:19:18 -03:00
100df73916 Remove Mapping, simplifying Repository 2022-10-10 22:19:09 -03:00
e576ef5054 Merge branch 'develop' into release 2022-10-10 21:48:01 -03:00
5c1f424c76 Added Mapping functionality 2022-10-10 21:47:48 -03:00
b19654bc70 Merge branch 'develop' into release 2022-10-10 17:32:48 -03:00
fe67519682 Added column = property handling for mapping 2022-10-10 17:32:34 -03:00
0af6be2c8e Merge branch 'develop' into release 2022-10-10 16:46:57 -03:00
0a9bed1735 Fixes 2022-10-10 16:46:44 -03:00
ba3c70a70e Merge branch 'develop' into release 2022-10-10 16:37:30 -03:00
422d9a6dbd Improve mapping 2022-10-10 16:36:47 -03:00
bc006f3e01 Merge branch 'develop' into release 2022-09-12 21:49:23 -03:00
23c56f9511 Change ResultSet::getFirst definition 2022-09-12 21:49:11 -03:00
4ccc38ffac Merge branch 'develop' into release 2022-09-12 17:39:15 -03:00
ef1603bc4b FIX: Check for missing getters and setters 2022-09-12 17:39:00 -03:00
8531658899 FIX 2022-09-12 17:34:05 -03:00
f19385ccc1 FIX: use getMethod instead of string 2022-09-12 17:33:45 -03:00
02f8bb0b4f FIX 2022-09-12 17:29:32 -03:00
d3771d8844 FIX: condition in old format 2022-09-12 17:29:04 -03:00
10e87b71a3 Update in Repository 2022-09-12 17:23:55 -03:00
aff0d4333d Added update to Repository 2022-09-12 17:23:29 -03:00
51124218d5 Check if value is set before serializing 2022-09-09 17:47:46 -04:00
37c5a79d5a Made model jsonserializable and added functionality into repository 2022-09-09 15:57:01 -04:00
806b8e7b93 Changed model to be jsonserialiazable and it's factory into repository
Added model, create and required/optional to repository
2022-09-09 15:55:45 -04:00
d12f3f7897 Implement delete 2022-09-09 13:15:54 -04:00
3811e8224b Define and Implement delete 2022-09-09 13:15:27 -04:00
9834eb70a4 Use full class name without having to specify namespace beforehand 2022-09-09 13:15:11 -04:00
7fc7de7390 Added Factory implementation 2022-09-09 10:33:19 -04:00
55275d2d75 Factory Implementation 2022-09-09 10:31:38 -04:00
3bc54fb9d1 Second version 2022-09-08 21:42:32 -04:00
230faaf3cc Abstracts 2022-09-08 21:35:33 -04:00
7f33475e35 Interfaces 2022-09-08 21:35:25 -04:00
8beb07819c Dependencies 2022-09-08 21:24:50 -04:00
91886eca0f Ignores 2022-09-08 21:24:41 -04:00
2d44c5d057 Cleanup 2022-09-08 21:24:29 -04:00
123d46d33c Fixed Model joins 2021-12-20 23:34:52 -03:00
fbd5b350bd FIX: sym -> symb, cast object to array, and use alias when alias IS NOT empty 2021-12-20 23:33:02 -03:00
65c224c636 Merge branch 'develop' into master 2021-08-01 20:51:44 -04:00
2bf938a9b7 FIX: array_walk returns bool and tertiary operator 2021-08-01 20:50:42 -04:00
6cd26a88ea Merge branch 'develop' into master 2021-08-01 20:34:04 -04:00
293b5af3ff FIX: self -> static 2021-08-01 20:31:49 -04:00
022ba575b7 Merge branch 'develop' into master 2021-06-04 21:15:26 -04:00
643c3e714f FIX: Find where in add 2021-05-23 21:27:13 -04:00
db2864395c FIX: Factory for find 2021-05-23 21:07:48 -04:00
1cd06d5fe6 Add and Edit Model 2021-05-23 21:03:10 -04:00
15 changed files with 790 additions and 584 deletions

7
.gitignore vendored
View File

@ -1,3 +1,6 @@
# Composer
/vendor/
composer.lock
**/vendor/
**/*.lock
# PHPStorm
**/.idea/

View File

@ -1,125 +0,0 @@
<?php
namespace ProVM\Common\Alias;
use \Model as BaseModel;
use ProVM\Common\Define\Model as ModelInterface;
use ProVM\Common\Factory\Model as ModelFactory;
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(): string {
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_model_class())->getTable();
return $this->factory
->find($parent_model_class)
->where([
[
$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_model_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();
}
}

View File

@ -1,14 +0,0 @@
<?php
namespace ProVM\Common\Define;
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;
}

View File

@ -1,13 +0,0 @@
<?php
namespace ProVM\Common\Define\Model;
use Carbon\Carbon;
trait Date {
public function date(\DateTime $date = null) {
if ($date === null) {
return Carbon::parse($this->date);
}
$this->date = $data->format('Y-m-d');
}
}

View File

@ -1,13 +0,0 @@
<?php
namespace ProVM\Common\Define\Model;
use Carbon\Carbon;
trait DateTime {
public function dateTime(\DateTime $date_time = null) {
if ($date_time === null) {
return Carbon::parse($this->date_time);
}
$this->date_time = $date_time->format('c');
}
}

View File

@ -1,13 +0,0 @@
<?php
namespace ProVM\Common\Define\Model;
use Carbon\Carbon;
trait Time {
public function time(\DateTime $time = null) {
if ($time === null) {
return Carbon::parse($this->time);
}
$this->time = $time->format('H:i:s e');
}
}

View File

@ -1,390 +0,0 @@
<?php
namespace ProVM\Common\Factory;
use ORM;
use Model as BaseModel;
use ProVM\Common\Define\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;
}
public function create(string $model_class, array $data = null): ModelInterface {
if ($data !== null) {
$model = $this->find($model_class);
foreach ($data as $f => $v) {
$model = $model->where([[$f, $v]]);
}
$model = $model->one();
if ($model !== null) {
return $model;
}
}
$model = BaseModel::factory($model_class)->create($data);
$model->setFactory($this);
return $model;
}
protected $class;
public function find(string $model_class): Model {
if (!class_exists($model_class)) {
throw new \InvalidArgumentException('El modelo ' . $model_class . ' no existe.');
}
$this->reset();
$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' => $c['column'] ?? $c[0],
'value' => $c['value'] ?? $c[1],
'sym' => strtolower($c['sym'] ?? ($c[2] ?? '=')),
'type' => strtolower($c['type']) ?? ''
];
$this->conditions []= $cond;
}
return $this;
}
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($id = null): ?ModelInterface {
$result = $this->build()->findOne($id);
if (!$result) {
return null;
}
$result->setFactory($this);
return $result;
}
public function many(): ?array {
$results = $this->build()->findMany();
if (!$results) {
return null;
}
foreach ($results as &$r) {
$r->setFactory($this);
}
return $results;
}
public function array(): ?array {
$results = $this->build()->findArray();
if (!$results) {
return null;
}
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;
}
}

View File

@ -1,6 +0,0 @@
<?php
namespace ProVM\Common\Form;
use ProVM\Common\Alias\Model as BaseModel;
abstract class Model extends BaseModel {}

View File

@ -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"
}
}
}
}

89
src/Alias/Model.php Normal file
View File

@ -0,0 +1,89 @@
<?php
namespace ProVM\Alias;
use ProVM\Concept\Model\Repository;
use ProVM\Concept\Model as ModelInterface;
abstract class Model implements ModelInterface
{
protected Repository $repository;
public function setRepository(Repository $repository): ModelInterface
{
$this->repository = $repository;
return $this;
}
public function getRepository(): Repository
{
return $this->repository;
}
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->getRepository()->save($this);
}
}
public function edit(array $data): void
{
foreach ($data as $key => $val) {
$m = 'set' . ucwords($key);
$this->{$m}($val);
}
$this->isDirty();
}
public function delete(): void
{
$this->getRepository()->delete($this);
}
public function jsonSerialize(): mixed
{
$obj = new \ReflectionObject($this);
$methods = $obj->getMethods();
$output = [];
foreach ($methods as $method) {
if (!str_contains($method->getName(), 'get')) {
continue;
}
if ($method->getName() === 'getRepository') {
continue;
}
$p = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', str_replace('get', '', $method->getName())));
if (!isset($this->{$p})) {
continue;
}
$output[$p] = $this->{$method->getName()}();
}
return $output;
}
}

View File

@ -0,0 +1,339 @@
<?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;
protected QueryBuilder $builder;
protected Factory $factory;
protected string $model;
protected string $table;
protected array $columns;
protected array $required_columns;
protected array $optional_columns;
protected array $properties;
public function getConnection(): Connection
{
return $this->connection;
}
public function getQueryBuilder(): QueryBuilder
{
return $this->builder;
}
public function getFactory(): Factory
{
return $this->factory;
}
public function getModel(): string
{
return $this->model;
}
public function getTable(): string
{
return $this->table;
}
public function getColumns(): array
{
return $this->columns ?? array_merge($this->getRequiredColumns(), $this->getOptionalColumns());
}
public function getRequiredColumns(): array
{
if (isset($this->optional_columns) and !isset($this->required_columns)) {
return array_diff($this->getColumns(), $this->getOptionalColumns());
}
return $this->required_columns ?? $this->getColumns();
}
public function getOptionalColumns(): array
{
if (isset($this->required_columns) and !isset($this->optional_columns)) {
return array_diff($this->getColumns(), $this->getRequiredColumns());
}
return $this->optional_columns ?? [];
}
public function getProperties(): array
{
return $this->properties ?? $this->getColumns();
}
public function setConnection(Connection $connection): Repository
{
$this->connection = $connection;
return $this;
}
public function setQueryBuilder(QueryBuilder $builder): RepositoryInterface
{
$this->builder = $builder;
return $this;
}
public function setFactory(Factory $factory): RepositoryInterface
{
$this->factory = $factory;
return $this;
}
public function setModel(string $model_class): RepositoryInterface
{
$this->model = $model_class;
return $this;
}
public function setTable(string $table): RepositoryInterface
{
$this->table = $table;
return $this;
}
public function setColumns(array $columns): RepositoryInterface
{
foreach ($columns as $column) {
$this->addColumn($column);
}
return $this;
}
public function setRequiredColumns(array $columns): RepositoryInterface
{
foreach ($columns as $item) {
$this->addRequiredColumn($item);
}
return $this;
}
public function setOptionalColumns(array $columns): RepositoryInterface
{
foreach ($columns as $item) {
$this->addOptionalColumn($item);
}
return $this;
}
public function setProperties(array $properties): RepositoryInterface
{
foreach ($properties as $property) {
$this->addProperty($property);
}
return $this;
}
public function addColumn(string $column): RepositoryInterface
{
$this->columns []= $column;
return $this;
}
public function addRequiredColumn(string $column): RepositoryInterface
{
$this->required_columns []= $column;
return $this;
}
public function addOptionalColumn(string $column): RepositoryInterface
{
$this->optional_columns []= $column;
return $this;
}
public function addProperty(string $property): RepositoryInterface
{
$this->properties []= $property;
return $this;
}
public function getNewModel(): Model
{
$class = $this->getModel();
return (new $class())
->setRepository($this);
}
public function getMethod(string $property, bool $get = true): string
{
$m = str_replace(' ', '', ucwords(str_replace('_', ' ', $property)));
if ($get) {
return "get{$m}";
}
return "set{$m}";
}
public function getProperty(string $method): string
{
$parts = preg_split('/(?=[A-Z])/', $method);
if (in_array(strtolower($parts[0]), ['get', 'set'])) {
array_shift($parts);
}
return strtolower(implode('_', $parts));
}
protected function compareProp(string $property, ?array $data = null): bool
{
if ($data === null) {
return in_array($property, $this->getRequiredColumns());
}
return in_array($property, $this->getOptionalColumns()) and key_exists($property, $data);
}
protected function compareProperty(string $property, ?array $data = null, ?string &$tmp_property = null): bool
{
$tmp_property = $property;
if ($this->compareProp($tmp_property, $data)) {
return true;
}
$tmp_property = "{$property}_id";
return $this->compareProp($tmp_property, $data);
}
protected function validateDefaultProperty(mixed $value): bool
{
$defaults = [
0,
'',
null
];
return in_array($value, $defaults);
}
protected function editModel(Model $model, string $getter, string $setter, mixed $value, bool $optional = false): Model
{
try {
$val = $model->{$getter}();
if ($optional and $this->validateDefaultProperty($val)) {
$model->{$setter}($value);
}
} catch (\Error|\Exception $e) {
$model->{$setter}($value);
}
return $model;
}
public function mapModel(Model $model, array $data): Model
{
/**
* Default Model map
* It is assumed that the Model properties are named like the table columns
*/
foreach ($this->getProperties() as $property) {
$getter = $this->getMethod($property);
$setter = $this->getMethod($property, false);
if ($this->compareProperty($property, tmp_property: $tmp)) {
$model = $this->editModel($model, $getter, $setter, $data[$tmp]);
continue;
}
if (!method_exists($model, $setter)) {
continue;
}
if ($this->compareProperty($property, $data, $tmp)) {
$model = $this->editModel($model, $getter, $setter, $data[$tmp], true);
}
try {
$value = $model->{$getter}();
if ($this->validateDefaultProperty($value)) {
error_log("Notice: {$property} has default value in " . get_called_class());
}
} catch (\Error | \Exception $e) {
error_log("Warning: Missing {$property} in data for " . get_called_class() . "::fillData");
}
}
return $model;
}
public function mapTable(Model $model, array $data): array
{
/**
* Default table map
* It is assumed that the table columns are named the same as the Model properties
*/
foreach ($this->getRequiredColumns() as $column) {
$m = $this->getMethod($column);
if (!method_exists($model, $m)) {
error_log("Warning: Missing getter for {$column} in " . get_called_class() . "::mapArray");
continue;
}
if (!isset($data[$column])) {
$data[$column] = $model->{$m}();
}
}
foreach ($this->getOptionalColumns() as $column) {
$m = $this->getMethod($column);
if (!method_exists($model, $m)) {
continue;
}
if (!isset($data[$column])) {
$data[$column] = $model->{$m}();
}
}
return $data;
}
public function load(array $data): Model
{
return $this->mapModel($this->getNewModel()
->setId($data['id']), $data);
}
public function save(Model $model): void
{
if (!$model->isDirty() and !$model->isNew()) {
return;
}
if (!$model->isNew()) {
$this->update($model);
return;
}
$values = array_replace(array_flip($this->getColumns()), $this->mapTable($model, []));
$cols = array_fill(0, count($values), '?');
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($this->getColumns())->values($cols);
$this->getConnection()->execute($query, array_values($values));
}
public function update(Model $model): void
{
if (!$model->isDirty() and !$model->isNew()) {
return;
}
$values = $this->mapTable($model, []);
$cols = array_map(function($column) {
return "{$column} = ?";
}, $values);
$values = array_values($values);
$values []= $model->getId();
$query = $this->getQueryBuilder()->update($this->getTable())->set($cols)->where(['id = ?']);
$this->getConnection()->execute($query, $values);
}
public function create(array $data): Model
{
return $this->mapModel($this->getNewModel()
->setNew(), $data);
}
public function edit(Model $model, array $data): Model
{
foreach ($this->getColumns() as $col) {
if (isset($data[$col])) {
$m = $this->getMethod($col, false);
if (!method_exists($model, $m)) {
continue;
}
$model->{$m}($data[$col]);
}
}
return $model;
}
public function delete(Model $model): void
{
$query = $this->getQueryBuilder()->delete($this->getTable())->where(['id = ?']);
$this->getConnection()->execute($query, [$model->getId()]);
}
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])->getFirstAsArray());
}
public function fetchAll(): array
{
$query = $this->getQueryBuilder()
->select()
->from($this->getTable());
return array_map([$this, 'load'], $this->getConnection()->query($query)->getAsArray());
}
}

20
src/Concept/Model.php Normal file
View File

@ -0,0 +1,20 @@
<?php
namespace ProVM\Concept;
use JsonSerializable;
use ProVM\Concept\Model\Repository;
interface Model extends JsonSerializable
{
public function setRepository(Repository $factory): Model;
public function getRepository(): Repository;
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;
public function delete(): void;
}

View File

@ -0,0 +1,48 @@
<?php
namespace ProVM\Concept\Model;
use ProVM\Concept\Model;
use Psr\Container\ContainerInterface;
interface Factory
{
/**
* @param ContainerInterface $container
* @return Factory
*/
public function setContainer(ContainerInterface $container): Factory;
/**
* @return ContainerInterface
*/
public function getContainer(): ContainerInterface;
/**
* Get Repository by class
* @param string $repository_class
* @return Repository
*/
public function get(string $repository_class): Repository;
/**
* Get Repository by Model class
* @param string $model_class
* @return Repository
*/
public function getByModel(string $model_class): Repository;
/**
* Fetch Model by Id
* @param string $model_class
* @param int $id
* @return Model
*/
public function fetchById(string $model_class, int $id): Model;
/**
* Fetch all Models
* @param string $model_class
* @return array
*/
public function fetchAll(string $model_class): array;
}

View File

@ -0,0 +1,218 @@
<?php
namespace ProVM\Concept\Model;
use ProVM\Concept\Database\Connection;
use ProVM\Concept\Database\QueryBuilder;
use ProVM\Concept\Model;
interface Repository
{
/**
* @return Connection
*/
public function getConnection(): Connection;
/**
* @return QueryBuilder
*/
public function getQueryBuilder(): QueryBuilder;
/**
* @return Factory
*/
public function getFactory(): Factory;
/**
* @return string
*/
public function getModel(): string;
/**
* @return string
*/
public function getTable(): string;
/**
* Get table columns
* @return array
*/
public function getColumns(): array;
/**
* Get required columns
* @return array
*/
public function getRequiredColumns(): array;
/**
* Get optional columns
* @return array
*/
public function getOptionalColumns(): array;
/**
* Get Model properties
* @return array
*/
public function getProperties(): array;
/**
* @param Connection $connection
* @return Repository
*/
public function setConnection(Connection $connection): Repository;
/**
* @param QueryBuilder $builder
* @return Repository
*/
public function setQueryBuilder(QueryBuilder $builder): Repository;
/**
* @param Factory $factory
* @return Repository
*/
public function setFactory(Factory $factory): Repository;
/**
* @param string $model_class
* @return Repository
*/
public function setModel(string $model_class): Repository;
/**
* @param string $table
* @return Repository
*/
public function setTable(string $table): Repository;
/**
* Set columns in table
* @param array $columns
* @return Repository
*/
public function setColumns(array $columns): Repository;
/**
* Set required columns
* Optional
* @param array $columns
* @return Repository
*/
public function setRequiredColumns(array $columns): Repository;
/**
* Set optional columns
* @param array $columns
* @return Repository
*/
public function setOptionalColumns(array $columns): Repository;
/**
* Set Model properties
* @param array $properties
* @return Repository
*/
public function setProperties(array $properties): Repository;
/**
* @param string $column
* @return Repository
*/
public function addColumn(string $column): Repository;
/**
* @param string $column
* @return Repository
*/
public function addRequiredColumn(string $column): Repository;
/**
* @param string $column
* @return Repository
*/
public function addOptionalColumn(string $column): Repository;
/**
* @param string $property
* @param $value
* @return Repository
*/
public function addProperty(string $property): Repository;
/**
* Set up the Repository
* SHOULD CALL
* setTable
* setColumns [setRequired, setOptional]
* setProperties
* setMapping
* setModel
* @return Repository
*/
public function setup(): Repository;
/**
* Get clean empty Model
* @return Model
*/
public function getNewModel(): Model;
/**
* Get Model method
* @param string $property
* @param bool $get
* @return string
*/
public function getMethod(string $property, bool $get = true): string;
/**
* @param string $method
* @return string
*/
public function getProperty(string $method): string;
/**
* Fill empty Model with data
* @param Model $model
* @param array $data
* @return Model
*/
public function mapModel(Model $model, array $data): Model;
/**
* Fill data array with Model values. Accepts a preset data array
* @param Model $model
* @param array $data
* @return array
*/
public function mapTable(Model $model, array $data): array;
/**
* Transform result array to Model
* @param array $data
* @return Model
*/
public function load(array $data): Model;
/**
* Save Model to table
* @param Model $model
* @return void
*/
public function save(Model $model): void;
/**
* Update table value with Model
* @param Model $model
* @return void
*/
public function update(Model $model): void;
/**
* Create new Model with data
* @param array $data
* @return Model
*/
public function create(array $data): Model;
/**
* Edit Model with data
* @param Model $model
* @param array $data
* @return Model
*/
public function edit(Model $model, array $data): Model;
/**
* Delete Model from table
* @param Model $model
* @return void
*/
public function delete(Model $model): void;
/**
* Fetch Model by Id
* @param int $id
* @return Model
*/
public function fetchById(int $id): Model;
/**
* Fetch all Models
* @return array
*/
public function fetchAll(): array;
}

View File

@ -0,0 +1,44 @@
<?php
namespace ProVM\Implement\Model;
use ProVM\Concept\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;
}
public function get(string $repository_class): Repository
{
return $this->getContainer()->get($repository_class);
}
public function getByModel(string $model_class): Repository
{
$class = str_replace('Model', 'Repository', $model_class);
return $this->get($class);
}
public function fetchById(string $model_class, int $id): Model
{
return $this->getByModel($model_class)->fetchById($id);
}
public function fetchAll(string $model_class): array
{
return $this->getByModel($model_class)->fetchAll();
}
}