extractGroupId($data); } /** * Returns a valid representation of the model. * * @param array $data Input data. * * @return array Data structure representing the model. * * @overrides Model::decode. */ protected function decode(array $data): array { return [ 'id' => (int) $data['id'], 'name' => $data['name'], 'groupId' => $this->extractGroupId($data), 'backgroundURL' => $this->extractBackgroundUrl($data), 'backgroundColor' => $this->extractBackgroundColor($data), 'isFavorite' => $this->extractFavorite($data), 'width' => (int) $data['width'], 'height' => (int) $data['height'], ]; } /** * Extract a group Id value. * * @param array $data Unknown input data structure. * * @return integer Valid identifier of a group. * * @throws \InvalidArgumentException When a valid group Id can't be found. */ private function extractGroupId(array $data): int { $groupId = static::parseIntOr( static::issetInArray($data, ['id_group', 'groupId']), null ); if ($groupId === null || $groupId < 0) { throw new \InvalidArgumentException( 'the group Id property is required and should be integer' ); } return $groupId; } /** * Extract a image url value. * * @param array $data Unknown input data structure. * * @return mixed String representing the image url (not empty) or null. */ private function extractBackgroundUrl(array $data) { return static::notEmptyStringOr( static::issetInArray($data, ['background', 'backgroundURL']), null ); } /** * Extract a background color value. * * @param array $data Unknown input data structure. * * @return mixed String representing the color (not empty) or null. */ private function extractBackgroundColor(array $data) { return static::notEmptyStringOr( static::issetInArray( $data, [ 'backgroundColor', 'background_color', ] ), null ); } /** * Extract the "is favorite" switch value. * * @param array $data Unknown input data structure. * * @return boolean If the item is favorite or not. */ private function extractFavorite(array $data): bool { return static::parseBool( static::issetInArray($data, ['is_favourite', 'isFavorite']) ); } /** * Obtain a container data structure from the database using a filter. * * @param array $filter Filter of the Visual Console. * * @return self A Visual Console Container instance. * @throws \Exception When the data cannot be retrieved from the DB. * * @override Model::fetchDataFromDB. */ protected static function fetchDataFromDB(array $filter) { // Due to this DB call, this function cannot be unit tested without // a proper mock. $row = \db_get_row_filter('tlayout', $filter); if ($row === false) { throw new \Exception('error fetching the data from the DB'); } return $row; } /** * Obtain a item's class. * * @param integer $type Type of the item of the Visual Console. * * @return mixed A reference to the item's class. */ public static function getItemClass(int $type) { switch ($type) { case STATIC_GRAPH: return Items\StaticGraph::class; case MODULE_GRAPH: return Items\ModuleGraph::class; case SIMPLE_VALUE: case SIMPLE_VALUE_MAX: case SIMPLE_VALUE_MIN: case SIMPLE_VALUE_AVG: return Items\SimpleValue::class; case PERCENTILE_BAR: case PERCENTILE_BUBBLE: case CIRCULAR_PROGRESS_BAR: case CIRCULAR_INTERIOR_PROGRESS_BAR: return Items\Percentile::class; case LABEL: return Items\Label::class; case ICON: return Items\Icon::class; // Enterprise item. case SERVICE: return \class_exists('Service') ? Service::class : Item::class; case GROUP_ITEM: return Items\Group::class; case BOX_ITEM: return Items\Box::class; case LINE_ITEM: return Items\Line::class; case AUTO_SLA_GRAPH: return Items\EventsHistory::class; case DONUT_GRAPH: return Items\DonutGraph::class; case BARS_GRAPH: return Items\BarsGraph::class; case CLOCK: return Items\Clock::class; case COLOR_CLOUD: return Items\ColorCloud::class; default: return Item::class; } } /** * Obtain a list of items which belong to the Visual Console. * * @param integer $layoutId Identifier of the Visual Console. * * @return array A list of items. * @throws \Exception When the data cannot be retrieved from the DB. */ public static function getItemsFromDB(int $layoutId): array { $filter = ['id_layout' => $layoutId]; $fields = [ 'id', 'type', ]; $rows = \db_get_all_rows_filter('tlayout_data', $filter, $fields); if ($rows === false) { $rows = []; // TODO: throw new \Exception('error fetching the data from the DB');. } $items = []; foreach ($rows as $data) { $itemId = (int) $data['id']; $itemType = (int) $data['type']; $class = static::getItemClass($itemType); \array_push($items, $class::fromDBWithId($itemId)); } return $items; } }