summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/orm/src/Relation
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:38:04 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:38:04 +0000
commit1ff5c35de5dbd70a782875a91dd2232fd01b002b (patch)
tree77d9ce5e1bf78b3e6ef79f8f6e7861e2ced3c09b /vendor/ipl/orm/src/Relation
parentInitial commit. (diff)
downloadicinga-php-library-db00f2fd38bc3063de0d7b1412949bc50f248a1f.tar.xz
icinga-php-library-db00f2fd38bc3063de0d7b1412949bc50f248a1f.zip
Adding upstream version 0.10.1.upstream/0.10.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/ipl/orm/src/Relation')
-rw-r--r--vendor/ipl/orm/src/Relation/BelongsTo.php13
-rw-r--r--vendor/ipl/orm/src/Relation/BelongsToMany.php199
-rw-r--r--vendor/ipl/orm/src/Relation/HasMany.php13
-rw-r--r--vendor/ipl/orm/src/Relation/HasOne.php12
-rw-r--r--vendor/ipl/orm/src/Relation/Junction.php43
5 files changed, 280 insertions, 0 deletions
diff --git a/vendor/ipl/orm/src/Relation/BelongsTo.php b/vendor/ipl/orm/src/Relation/BelongsTo.php
new file mode 100644
index 0000000..1edcff3
--- /dev/null
+++ b/vendor/ipl/orm/src/Relation/BelongsTo.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace ipl\Orm\Relation;
+
+use ipl\Orm\Relation;
+
+/**
+ * Inverse of a one-to-one or one-to-many relationship
+ */
+class BelongsTo extends Relation
+{
+ protected $inverse = true;
+}
diff --git a/vendor/ipl/orm/src/Relation/BelongsToMany.php b/vendor/ipl/orm/src/Relation/BelongsToMany.php
new file mode 100644
index 0000000..132fe55
--- /dev/null
+++ b/vendor/ipl/orm/src/Relation/BelongsToMany.php
@@ -0,0 +1,199 @@
+<?php
+
+namespace ipl\Orm\Relation;
+
+use ipl\Orm\Model;
+use ipl\Orm\Relation;
+use ipl\Orm\Relations;
+
+use function ipl\Stdlib\get_php_type;
+
+/**
+ * Many-to-many relationship
+ */
+class BelongsToMany extends Relation
+{
+ protected $isOne = false;
+
+ /** @var string Name of the join table or junction model class */
+ protected $throughClass;
+
+ /** @var Model The junction model */
+ protected $through;
+
+ /** @var string|array Column name(s) of the target model's foreign key found in the join table */
+ protected $targetForeignKey;
+
+ /** @var string|array Candidate key column name(s) in the target table which references the target foreign key */
+ protected $targetCandidateKey;
+
+ /**
+ * Get the name of the join table or junction model class
+ *
+ * @return string
+ */
+ public function getThroughClass()
+ {
+ return $this->throughClass;
+ }
+
+ /**
+ * Set the join table name or junction model class
+ *
+ * @param string $through
+ *
+ * @return $this
+ */
+ public function through($through)
+ {
+ $this->throughClass = $through;
+
+ return $this;
+ }
+
+ /**
+ * Get the junction model
+ *
+ * @return Model|Junction
+ */
+ public function getThrough()
+ {
+ if ($this->through === null) {
+ $throughClass = $this->getThroughClass();
+
+ if (class_exists($throughClass)) {
+ $this->through = new $throughClass();
+ } else {
+ $this->through = (new Junction())
+ ->setTableName($throughClass);
+ }
+ }
+
+ return $this->through;
+ }
+
+ /**
+ * Set the junction model
+ *
+ * @param Model $through
+ *
+ * @return $this
+ */
+ public function setThrough($through)
+ {
+ $this->through = $through;
+
+ return $this;
+ }
+
+ /**
+ * Get the column name(s) of the target model's foreign key found in the join table
+ *
+ * @return string|array Array if the foreign key is compound, string otherwise
+ */
+ public function getTargetForeignKey()
+ {
+ return $this->targetForeignKey;
+ }
+
+ /**
+ * Set the column name(s) of the target model's foreign key found in the join table
+ *
+ * @param string|array $targetForeignKey Array if the foreign key is compound, string otherwise
+ *
+ * @return $this
+ */
+ public function setTargetForeignKey($targetForeignKey)
+ {
+ $this->targetForeignKey = $targetForeignKey;
+
+ return $this;
+ }
+
+ /**
+ * Get the candidate key column name(s) in the target table which references the target foreign key
+ *
+ * @return string|array Array if the foreign key is compound, string otherwise
+ */
+ public function getTargetCandidateKey()
+ {
+ return $this->targetCandidateKey;
+ }
+
+ /**
+ * Set the candidate key column name(s) in the target table which references the target foreign key
+ *
+ * @param string|array $targetCandidateKey Array if the foreign key is compound, string otherwise
+ *
+ * @return $this
+ */
+ public function setTargetCandidateKey($targetCandidateKey)
+ {
+ $this->targetCandidateKey = $targetCandidateKey;
+
+ return $this;
+ }
+
+ public function resolve()
+ {
+ $source = $this->getSource();
+
+ $possibleCandidateKey = [$this->getCandidateKey()];
+ $possibleForeignKey = [$this->getForeignKey()];
+
+ $target = $this->getTarget();
+
+ $possibleTargetCandidateKey = [$this->getTargetForeignKey() ?: static::getDefaultForeignKey($target)];
+ $possibleTargetForeignKey = [$this->getTargetCandidateKey() ?: static::getDefaultCandidateKey($target)];
+
+ $junction = $this->getThrough();
+
+ if (! $junction instanceof Junction) {
+ $relations = new Relations();
+ $junction->createRelations($relations);
+
+ if ($relations->has($source->getTableName())) {
+ $sourceRelation = $relations->get($source->getTableName());
+
+ $possibleCandidateKey[] = $sourceRelation->getForeignKey();
+ $possibleForeignKey[] = $sourceRelation->getCandidateKey();
+ }
+
+ if ($relations->has($target->getTableName())) {
+ $targetRelation = $relations->get($target->getTableName());
+
+ $possibleTargetCandidateKey[] = $targetRelation->getCandidateKey();
+ $possibleTargetForeignKey[] = $targetRelation->getForeignKey();
+ }
+ }
+
+ $toJunction = (new HasMany())
+ ->setName($junction->getTableName())
+ ->setSource($source)
+ ->setTarget($junction)
+ ->setCandidateKey($this->extractKey($possibleCandidateKey))
+ ->setForeignKey($this->extractKey($possibleForeignKey));
+
+ $toTarget = (new HasMany())
+ ->setName($this->getName())
+ ->setSource($junction)
+ ->setTarget($target)
+ ->setCandidateKey($this->extractKey($possibleTargetCandidateKey))
+ ->setForeignKey($this->extractKey($possibleTargetForeignKey));
+
+ foreach ($toJunction->resolve() as $k => $v) {
+ yield $k => $v;
+ }
+
+ foreach ($toTarget->resolve() as $k => $v) {
+ yield $k => $v;
+ }
+ }
+
+ protected function extractKey(array $possibleKey)
+ {
+ $filtered = array_filter($possibleKey);
+
+ return array_pop($filtered);
+ }
+}
diff --git a/vendor/ipl/orm/src/Relation/HasMany.php b/vendor/ipl/orm/src/Relation/HasMany.php
new file mode 100644
index 0000000..3e71e25
--- /dev/null
+++ b/vendor/ipl/orm/src/Relation/HasMany.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace ipl\Orm\Relation;
+
+use ipl\Orm\Relation;
+
+/**
+ * One-to-many relationship
+ */
+class HasMany extends Relation
+{
+ protected $isOne = false;
+}
diff --git a/vendor/ipl/orm/src/Relation/HasOne.php b/vendor/ipl/orm/src/Relation/HasOne.php
new file mode 100644
index 0000000..8f7a802
--- /dev/null
+++ b/vendor/ipl/orm/src/Relation/HasOne.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace ipl\Orm\Relation;
+
+use ipl\Orm\Relation;
+
+/**
+ * One-to-one relationship
+ */
+class HasOne extends Relation
+{
+}
diff --git a/vendor/ipl/orm/src/Relation/Junction.php b/vendor/ipl/orm/src/Relation/Junction.php
new file mode 100644
index 0000000..9e23bb2
--- /dev/null
+++ b/vendor/ipl/orm/src/Relation/Junction.php
@@ -0,0 +1,43 @@
+<?php
+
+namespace ipl\Orm\Relation;
+
+use ipl\Orm\Model;
+
+/**
+ * Junction model for many-to-many relations
+ */
+class Junction extends Model
+{
+ /** @var string */
+ protected $tableName;
+
+ public function getTableName()
+ {
+ return $this->tableName;
+ }
+
+ /**
+ * Set the table name
+ *
+ * @param string $tableName
+ *
+ * @return $this
+ */
+ public function setTableName($tableName)
+ {
+ $this->tableName = $tableName;
+
+ return $this;
+ }
+
+ public function getKeyName()
+ {
+ return null;
+ }
+
+ public function getColumns()
+ {
+ return null;
+ }
+}