From bef1b08d0e561dd1b484a4bdf1e63b2ae2499465 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Tue, 21 Jan 2020 10:58:21 -0300 Subject: [PATCH] Relaciones --- common/Alias/Model.php | 2 +- src/RelationNode.php | 54 ++++++++++++++++++++++++++++++++++++++++++ src/Relationship.php | 33 ++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/RelationNode.php create mode 100644 src/Relationship.php diff --git a/common/Alias/Model.php b/common/Alias/Model.php index 574813a..db14079 100644 --- a/common/Alias/Model.php +++ b/common/Alias/Model.php @@ -7,7 +7,7 @@ use \Model as BaseModel; use Aldarien\Common\Definition\Model as ModelInterface; use Aldarien\Common\Factory\Model as ModelFactory; -class Model extends BaseModel implements ModelInterface { +abstract class Model extends BaseModel implements ModelInterface { const CHILD_KEY = 'child_key'; const PARENT_KEY = 'parent_key'; const SELF_KEY = 'self_key'; diff --git a/src/RelationNode.php b/src/RelationNode.php new file mode 100644 index 0000000..293d27f --- /dev/null +++ b/src/RelationNode.php @@ -0,0 +1,54 @@ +join = 'join'; + $this->relation = '='; + } + public function setSource(string $class_name, string $key): RelationNode { + return $this->set('source', $class_name, $key); + } + public function setDestination(string $class_name, string $key): RelationNode { + return $this->set('destination', $class_name, $key); + } + public function setJoin(string $join): RelationNode { + $valid = ['join', 'inner join', 'outer join', 'left join', 'left outer join', 'right join', 'right outer join']; + if (\array_search(strtolower($join), $valid) !== false) { + $this->join = strtolower($join); + } + return $this; + } + public function setRelation(string $relation): RelationNode { + $valid = ['=', '<=', '<', '>=', '>', 'like']; + if (\array_search(strtolower($relation), $valid) !== false) { + $this->relation = $relation; + } + return $this; + } + + protected function set(string $type, string $class_name, string $key): RelationNode { + $this->$type = (object) [ + 'class' => $class_name, + 'table' => Model::getTable($class_name), + 'key' => $key + ]; + return $this; + } + + public function build(ModelFactory $factory): ModelFactory { + $source = implode('.', [$this->source->table, $this->source->key]); + $destination = implode('.', [$this->destination->table, $this->destination->key]); + $method = str_replace([' '], ['_'], strtolower($this->join)); + $output = $factory->{$method}([$this->destination->table, [$destination, $source, $this->relation]]); + return $output; + } +} diff --git a/src/Relationship.php b/src/Relationship.php new file mode 100644 index 0000000..707025c --- /dev/null +++ b/src/Relationship.php @@ -0,0 +1,33 @@ +setStart($class_name); + } + public function setStart(string $class_name) { + $this->start = (object) [ + 'class' => $class_name, + 'table' => Model::getTable($class_name) + ]; + } + public function addNode(RelationNode $node) { + if ($this->nodes === null) { + $this->nodes = []; + } + $this->nodes []= $node; + } + public function build(ModelFactory $factory): ModelFactory { + $output = $factory->find($this->start->class); + foreach ($this->nodes as $node) { + $output = $node->build($factory); + } + return $output; + } +}