extractGroupId($data); } /** * Returns a valid data structure. * * @param mixed $data * * @return array */ 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'], ]; } private function extractGroupId(array $data): int { if (isset($data['id_group']) === true && \is_numeric($data['id_group']) === true && $data['id_group'] >= 0 ) { return $data['id_group']; } else if (isset($data['groupId']) === true && \is_numeric($data['groupId']) === true && $data['groupId'] >= 0 ) { return $data['groupId']; } throw new \InvalidArgumentException( 'the group Id property is required and should be integer' ); } private function extractBackgroundUrl(array $data) { $background = Model::notEmptyStringOr( Model::issetInArray($data, ['background', 'backgroundURL']), null ); return $background; } private function extractBackgroundColor(array $data) { $backgroundColor = Model::notEmptyStringOr( Model::issetInArray($data, ['backgroundColor', 'background_color']), null ); return $backgroundColor; } private function extractFavorite(array $data): bool { $favorite = Model::parseBool( Model::issetInArray($data, ['is_favourite', 'isFavorite']), null ); return $favorite; } /** * 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. */ public static function fetchDataFromDB(array $filter): self { // 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 a new instance. return new self($row); } }