From f66ab8dae2f3d0418759f81a3a64dc9517a62449 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:17:31 +0200 Subject: Adding upstream version 1.10.2. Signed-off-by: Daniel Baumann --- library/Director/Objects/IcingaRelatedObject.php | 211 +++++++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 library/Director/Objects/IcingaRelatedObject.php (limited to 'library/Director/Objects/IcingaRelatedObject.php') diff --git a/library/Director/Objects/IcingaRelatedObject.php b/library/Director/Objects/IcingaRelatedObject.php new file mode 100644 index 0000000..d35bcb0 --- /dev/null +++ b/library/Director/Objects/IcingaRelatedObject.php @@ -0,0 +1,211 @@ +owner = $owner; + $this->key = $key; + $this->idKey = $key . '_id'; + } + + /** + * Set a specific id + * + * @param $id int + * + * @return self + */ + public function setId($id) + { + if (! is_int($id)) { + throw new ProgrammingError( + 'An id must be an integer' + ); + } + + if ($this->object !== null) { + if ($this->object->id === $id) { + return $this; + } else { + $this->object = null; + } + } + + if ($this->object === null) { + $this->name = null; + } + + $this->id = $id; + $this->owner->set($this->getRealPropertyName(), $id); + + return $this; + } + + /** + * Return the related objects id + * + * @return int + */ + public function getId() + { + if ($this->id === null) { + $this->id = $this->getObject()->id; + } + + return $this->id; + } + + /** + * Lazy-load the related object + * + * @return IcingaObject + */ + public function getObject() + { + // TODO: This is unfinished + + if ($this->object === null) { + $class = $this->getClassName(); + + if ($this->name === null) { + if ($id = $this->getId()) { + } + } else { + $this->object = $class::load($this->name, $this->owner->getConnection()); + } + } + return $this->object; + } + + /** + * The real property name pointing to this relation, e.g. 'host_id' + * + * @return string + */ + public function getRealPropertyName() + { + return $this->key . '_id'; + } + + /** + * Full related class name + * + * @return string + */ + public function getClassName() + { + if ($this->className === null) { + $this->className = __NAMESPACE__ . '\\' . $this->getShortClassName(); + } + + return $this->className; + } + + /** + * Related class name relative to Icinga\Module\Director\Objects + * + * @return string + */ + public function getShortClassName() + { + return $this->owner->getRelationObjectClass($this->key); + } + + /** + * Set a related property + * + * This might be a string or an object + * @param $related string|IcingaObject + * @throws ProgrammingError + * + * return self + */ + public function set($related) + { + if (is_string($related)) { + $this->name = $related; + } elseif (is_object($related)) { + $className = $this->getClassName(); + if ($related instanceof $className) { + $this->object = $related; + $this->name = $object->object_name; + $this->id = $object->id; + } else { + throw new ProgrammingError( + 'Trying to set a related "%s" while expecting "%s"', + get_class($related), + $this->getShortClassName() + ); + } + } else { + throw new ProgrammingError( + 'Related object can be name or object, got: %s', + var_export($related, 1) + ); + } + + return $this; + } + + /** + * Get the name of the related object + * + * @return string + */ + public function getName() + { + if ($this->name === null) { + return $this->owner->{$this->key}; + } else { + return $this->name; + } + } + + /** + * Conservative constructor to avoid issued with PHP GC + */ + public function __destruct() + { + unset($this->owner); + } +} -- cgit v1.2.3