Source
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/**
* A helper class for storing relations between objects, and mapping related objects.
*/
class CRelationMap {
/**
* A hash with IDs of base objects as keys and hashes of related object IDs as values.
*
* @var array
*/
protected $map = [];
/**
* A hash of related object IDs.
*
* @var array
*/
protected $relatedIds = [];
/**
* Adds a new relation.
*
* @param string $baseObjectId
* @param string $relatedObjectId
*/
public function addRelation($baseObjectId, $relatedObjectId) {
$this->map[$baseObjectId][$relatedObjectId] = $relatedObjectId;
$this->relatedIds[$relatedObjectId] = $relatedObjectId;
}
/**
* Returns the IDs of all related objects.
*
* @return array
*/
public function getRelatedIds() {
return array_values($this->relatedIds);
}
/**
* Maps multiple related objects to the base objects and adds them under the $name property. Each base object will
* have an array of related objects.
*
* @param array $baseObjects a hash of base objects with IDs as keys
* @param array $relatedObjects a hash of related objects with IDs as keys
* @param string $name the name of the property under which the related objects will be added
* @param int $limit maximum number of related objects for each base objects