Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
3087a48c43 | |||
3aed13e6a0 | |||
43f545516d | |||
4d64143dbc | |||
b757ed19b2 | |||
1dc21d8fb7 | |||
9dc71e4d77 | |||
65bec43b45 | |||
c6806a1c62 | |||
5f3f6b72e5 | |||
6fd19a11be | |||
a1466143b5 |
@ -1,14 +1,125 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Alias;
|
||||
|
||||
use \Model as BaseModel;
|
||||
use ProVM\Common\Alias\Model as ModelInterface;
|
||||
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;
|
||||
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 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();
|
||||
}
|
||||
}
|
||||
|
@ -1,125 +1,14 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Define;
|
||||
|
||||
use \Model as BaseModel;
|
||||
use ProVM\Common\Alias\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';
|
||||
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 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();
|
||||
}
|
||||
public function toArray(): array;
|
||||
}
|
||||
|
@ -29,11 +29,13 @@ class Model {
|
||||
$model = $model->where([[$f, $v]]);
|
||||
}
|
||||
$model = $model->one();
|
||||
if ($model !== false) {
|
||||
if ($model !== null) {
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
return BaseModel::factory($model_class)->create($data);
|
||||
$model = BaseModel::factory($model_class)->create($data);
|
||||
$model->setFactory($this);
|
||||
return $model;
|
||||
}
|
||||
protected $class;
|
||||
public function find(string $model_class): Model {
|
||||
@ -342,28 +344,28 @@ class Model {
|
||||
return $orm->offset($this->offset);
|
||||
}
|
||||
|
||||
public function one($id = null): ModelInterface {
|
||||
public function one($id = null): ?ModelInterface {
|
||||
$result = $this->build()->findOne($id);
|
||||
if (!$result) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
$result->setFactory($this);
|
||||
return $result;
|
||||
}
|
||||
public function many(): array {
|
||||
public function many(): ?array {
|
||||
$results = $this->build()->findMany();
|
||||
if (!$results) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
foreach ($results as &$r) {
|
||||
$r->setFactory($this);
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
public function array(): array {
|
||||
public function array(): ?array {
|
||||
$results = $this->build()->findArray();
|
||||
if (!$results) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
Reference in New Issue
Block a user