1<?php 2/* 3** Zabbix 4** Copyright (C) 2001-2021 Zabbix SIA 5** 6** This program is free software; you can redistribute it and/or modify 7** it under the terms of the GNU General Public License as published by 8** the Free Software Foundation; either version 2 of the License, or 9** (at your option) any later version. 10** 11** This program is distributed in the hope that it will be useful, 12** but WITHOUT ANY WARRANTY; without even the implied warranty of 13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14** GNU General Public License for more details. 15** 16** You should have received a copy of the GNU General Public License 17** along with this program; if not, write to the Free Software 18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19**/ 20 21 22/** 23 * A helper class for storing relations between objects, and mapping related objects. 24 */ 25class CRelationMap { 26 27 /** 28 * A hash with IDs of base objects as keys and hashes of related object IDs as values. 29 * 30 * @var array 31 */ 32 protected $map = []; 33 34 /** 35 * A hash of related object IDs. 36 * 37 * @var array 38 */ 39 protected $relatedIds = []; 40 41 /** 42 * Adds a new relation. 43 * 44 * @param string $baseObjectId 45 * @param string $relatedObjectId 46 */ 47 public function addRelation($baseObjectId, $relatedObjectId) { 48 $this->map[$baseObjectId][$relatedObjectId] = $relatedObjectId; 49 $this->relatedIds[$relatedObjectId] = $relatedObjectId; 50 } 51 52 /** 53 * Returns the IDs of all related objects. 54 * 55 * @return array 56 */ 57 public function getRelatedIds() { 58 return array_values($this->relatedIds); 59 } 60 61 /** 62 * Maps multiple related objects to the base objects and adds them under the $name property. Each base object will 63 * have an array of related objects. 64 * 65 * @param array $baseObjects a hash of base objects with IDs as keys 66 * @param array $relatedObjects a hash of related objects with IDs as keys 67 * @param string $name the name of the property under which the related objects will be added 68 * @param int $limit maximum number of related objects for each base objects 69 * 70 * @return array 71 */ 72 public function mapMany(array $baseObjects, array $relatedObjects, $name, $limit = null) { 73 foreach ($baseObjects as $baseObjectId => &$baseObject) { 74 $baseObject[$name] = []; 75 76 if (isset($this->map[$baseObjectId]) && $this->map[$baseObjectId]) { 77 $matchingRelatedObjects = array_values(array_intersect_key($relatedObjects, $this->map[$baseObjectId])); 78 79 if ($matchingRelatedObjects) { 80 if ($limit) { 81 $matchingRelatedObjects = array_slice($matchingRelatedObjects, 0, $limit); 82 } 83 84 $baseObject[$name] = $matchingRelatedObjects; 85 } 86 } 87 } 88 unset($baseObject); 89 90 return $baseObjects; 91 } 92 93 /** 94 * Maps multiple related objects to the base objects and adds them under the $name property. 95 * Each base object will have only one related object. 96 * 97 * @param array $baseObjects a hash of base objects with IDs as keys 98 * @param array $relatedObjects a hash of related objects with IDs as keys 99 * @param string $name the name of the property under which the related object will be added 100 * 101 * @return array 102 */ 103 public function mapOne(array $baseObjects, array $relatedObjects, $name) { 104 foreach ($baseObjects as $baseObjectId => &$baseObject) { 105 $matchingRelatedObject = []; 106 107 if (isset($this->map[$baseObjectId])) { 108 $matchingRelatedId = reset($this->map[$baseObjectId]); 109 110 if (isset($relatedObjects[$matchingRelatedId])) { 111 $matchingRelatedObject = $relatedObjects[$matchingRelatedId]; 112 } 113 } 114 115 $baseObject[$name] = $matchingRelatedObject; 116 } 117 unset($baseObject); 118 119 return $baseObjects; 120 } 121} 122