Compare commits
28 Commits
7c727d93e9
...
1.0.0-rc5
Author | SHA1 | Date | |
---|---|---|---|
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 |
@ -1,14 +1,156 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Common\Alias;
|
namespace ProVM\Common\Alias;
|
||||||
|
|
||||||
|
use \Model as BaseModel;
|
||||||
|
use ProVM\Common\Define\Model as ModelInterface;
|
||||||
use ProVM\Common\Factory\Model as ModelFactory;
|
use ProVM\Common\Factory\Model as ModelFactory;
|
||||||
|
|
||||||
interface Model {
|
abstract class Model extends BaseModel implements ModelInterface {
|
||||||
public function getTable(): string;
|
const SELF_KEY = 'self_key';
|
||||||
public function setFactory(ModelFactory $factory): Model;
|
const PARENT_KEY = 'parent_key';
|
||||||
public function parentOf(string $child_model_class, array $relation_definitions): array;
|
const CHILD_KEY = 'child_key';
|
||||||
public function childOf(string $parent_model_class, array $relation_definitions): Model;
|
const SIBLING_KEY = 'sibling_key';
|
||||||
public function siblingOf(string $sibling_model_class, string $connecting_table, array $relation_definitions): array;
|
const SELF_CONNECT_KEY = 'self_connect_key';
|
||||||
|
const SIBLING_CONNECT_KEY = 'sibling_connect_key';
|
||||||
|
|
||||||
public function toArray(): array;
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function parseInput($input): array {
|
||||||
|
return array_intersect_key((array) $input, array_combine(static::$fields, static::$fields));
|
||||||
|
}
|
||||||
|
public static function add(ModelFactory $factory, $input): ?ModelInterface {
|
||||||
|
$data = static::parseInput($input);
|
||||||
|
$class = get_called_class();
|
||||||
|
if (method_exists($class, 'find')) {
|
||||||
|
$obj = static::find($factory, $input);
|
||||||
|
} else {
|
||||||
|
$where = $data;
|
||||||
|
array_walk($where, function(&$item, $key) {
|
||||||
|
$item = [$key, $item];
|
||||||
|
});
|
||||||
|
$where = array_values($where);
|
||||||
|
$obj = $factory->find($class)->where($where)->one();
|
||||||
|
}
|
||||||
|
if ($obj === null) {
|
||||||
|
$obj = $factory->create($class, $data);
|
||||||
|
}
|
||||||
|
return $obj;
|
||||||
|
}
|
||||||
|
public function edit($input): bool {
|
||||||
|
$data = static::parseInput($input);
|
||||||
|
foreach (static::$fields as $field) {
|
||||||
|
if ($this->{$field} != $data[$field]) {
|
||||||
|
$this->{$field} = $data[$field];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this->save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,125 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Common\Define;
|
namespace ProVM\Common\Define;
|
||||||
|
|
||||||
use \Model as BaseModel;
|
|
||||||
use ProVM\Common\Alias\Model as ModelInterface;
|
|
||||||
use ProVM\Common\Factory\Model as ModelFactory;
|
use ProVM\Common\Factory\Model as ModelFactory;
|
||||||
|
|
||||||
abstract class Model extends BaseModel implements ModelInterface {
|
interface Model {
|
||||||
const SELF_KEY = 'self_key';
|
public function getTable(): string;
|
||||||
const PARENT_KEY = 'parent_key';
|
public function setFactory(ModelFactory $factory): Model;
|
||||||
const CHILD_KEY = 'child_key';
|
public function parentOf(string $child_model_class, array $relation_definitions): ?array;
|
||||||
const SIBLING_KEY = 'sibling_key';
|
public function childOf(string $parent_model_class, array $relation_definitions): ?Model;
|
||||||
const SELF_CONNECT_KEY = 'self_connect_key';
|
public function siblingOf(string $sibling_model_class, string $connecting_table, array $relation_definitions): ?array;
|
||||||
const SIBLING_CONNECT_KEY = 'sibling_connect_key';
|
|
||||||
|
|
||||||
public function getTable(): string {
|
public function toArray(): array;
|
||||||
return static::$_table;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected $factory;
|
public static function add(ModelFactory $factory, $input): ?Model;
|
||||||
public function setFactory(ModelFactory $factory): ModelInterface {
|
public function edit($input): bool;
|
||||||
$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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
13
common/Define/Model/Date.php
Normal file
13
common/Define/Model/Date.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?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');
|
||||||
|
}
|
||||||
|
}
|
13
common/Define/Model/DateTime.php
Normal file
13
common/Define/Model/DateTime.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?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');
|
||||||
|
}
|
||||||
|
}
|
13
common/Define/Model/Time.php
Normal file
13
common/Define/Model/Time.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?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');
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,7 @@ namespace ProVM\Common\Factory;
|
|||||||
|
|
||||||
use ORM;
|
use ORM;
|
||||||
use Model as BaseModel;
|
use Model as BaseModel;
|
||||||
use ProVM\Common\Alias\Model as ModelInterface;
|
use ProVM\Common\Define\Model as ModelInterface;
|
||||||
|
|
||||||
class Model {
|
class Model {
|
||||||
public function reset(): Model {
|
public function reset(): Model {
|
||||||
@ -29,11 +29,13 @@ class Model {
|
|||||||
$model = $model->where([[$f, $v]]);
|
$model = $model->where([[$f, $v]]);
|
||||||
}
|
}
|
||||||
$model = $model->one();
|
$model = $model->one();
|
||||||
if ($model !== false) {
|
if ($model !== null) {
|
||||||
return $model;
|
return $model;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return BaseModel::factory($model_class)->create($data);
|
$model = BaseModel::factory($model_class)->create($data);
|
||||||
|
$model->setFactory($this);
|
||||||
|
return $model;
|
||||||
}
|
}
|
||||||
protected $class;
|
protected $class;
|
||||||
public function find(string $model_class): Model {
|
public function find(string $model_class): Model {
|
||||||
@ -77,9 +79,9 @@ class Model {
|
|||||||
'table' => $j['table'] ?? $j[0],
|
'table' => $j['table'] ?? $j[0],
|
||||||
'from' => $j['from'] ?? $j[1],
|
'from' => $j['from'] ?? $j[1],
|
||||||
'to' => $j['to'] ?? $j[2],
|
'to' => $j['to'] ?? $j[2],
|
||||||
'sym' => $j['sym'] ?? ($j[3] ?? '='),
|
'symb' => $j['symb'] ?? ($j[3] ?? '='),
|
||||||
'alias' => $j['alias'] ?? '',
|
'alias' => $j['alias'] ?? '',
|
||||||
'type' => strtolower($j['type']) ?? '',
|
'type' => strtolower($j['type'] ?? ''),
|
||||||
'params' => $j['params'] ?? ''
|
'params' => $j['params'] ?? ''
|
||||||
];
|
];
|
||||||
$this->joins []= $join;
|
$this->joins []= $join;
|
||||||
@ -95,8 +97,8 @@ class Model {
|
|||||||
$cond = (object) [
|
$cond = (object) [
|
||||||
'column' => $c['column'] ?? $c[0],
|
'column' => $c['column'] ?? $c[0],
|
||||||
'value' => $c['value'] ?? $c[1],
|
'value' => $c['value'] ?? $c[1],
|
||||||
'sym' => strtolower($c['sym'] ?? ($c[2] ?? '=')),
|
'symb' => strtolower($c['symb'] ?? ($c[2] ?? '=')),
|
||||||
'type' => strtolower($c['type']) ?? ''
|
'type' => strtolower($c['type'] ?? '')
|
||||||
];
|
];
|
||||||
$this->conditions []= $cond;
|
$this->conditions []= $cond;
|
||||||
}
|
}
|
||||||
@ -152,7 +154,7 @@ class Model {
|
|||||||
return $orm;
|
return $orm;
|
||||||
}
|
}
|
||||||
foreach ($this->columns as $col) {
|
foreach ($this->columns as $col) {
|
||||||
$orm = $orm->select(trim(implode('.', $col), '.'));
|
$orm = $orm->select(trim(implode('.', (array) $col), '.'));
|
||||||
}
|
}
|
||||||
return $orm;
|
return $orm;
|
||||||
}
|
}
|
||||||
@ -190,7 +192,7 @@ class Model {
|
|||||||
}
|
}
|
||||||
if ($join->type == 'raw') {
|
if ($join->type == 'raw') {
|
||||||
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to], $join->alias, $join->params);
|
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to], $join->alias, $join->params);
|
||||||
} elseif ($join->alias === '') {
|
} elseif ($join->alias !== '') {
|
||||||
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to], $join->alias);
|
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to], $join->alias);
|
||||||
} else {
|
} else {
|
||||||
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to]);
|
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to]);
|
||||||
@ -204,7 +206,7 @@ class Model {
|
|||||||
}
|
}
|
||||||
foreach ($this->conditions as $cond) {
|
foreach ($this->conditions as $cond) {
|
||||||
$method = 'where';
|
$method = 'where';
|
||||||
switch ($cond->sym) {
|
switch ($cond->symb) {
|
||||||
case '<':
|
case '<':
|
||||||
$method = 'whereLt';
|
$method = 'whereLt';
|
||||||
break;
|
break;
|
||||||
@ -342,29 +344,32 @@ class Model {
|
|||||||
return $orm->offset($this->offset);
|
return $orm->offset($this->offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function one($id = null): ModelInterface {
|
public function one($id = null): ?ModelInterface {
|
||||||
$result = $this->build()->findOne($id);
|
$result = $this->build()->findOne($id);
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
$result->setFactory($this);
|
$result->setFactory($this);
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
public function many(): array {
|
public function many(): ?array {
|
||||||
$results = $this->build()->findMany();
|
$results = $this->build()->findMany();
|
||||||
if (!$results) {
|
if (!$results) {
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
foreach ($results as &$r) {
|
foreach ($results as &$r) {
|
||||||
$r->setFactory($this);
|
$r->setFactory($this);
|
||||||
}
|
}
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
public function array(): array {
|
public function array(): ?array {
|
||||||
$results = $this->build()->findArray();
|
$results = $this->many();
|
||||||
if (!$results) {
|
if (!$results or $results === null) {
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
array_walk($results, function(&$item) {
|
||||||
|
$item = $item->toArray();
|
||||||
|
});
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Common\Form;
|
namespace ProVM\Common\Form;
|
||||||
|
|
||||||
use ProVM\Common\Define\Model as BaseModel;
|
use ProVM\Common\Alias\Model as BaseModel;
|
||||||
|
|
||||||
abstract class Model extends BaseModel {}
|
abstract class Model extends BaseModel {}
|
||||||
|
Reference in New Issue
Block a user