(mixed $filters, array $pages)
| 11 | /** Pure validation and merging; viewer values never modify stored report options. */ |
| 12 | class PanoramaFilterService { |
| 13 | public static function normalize(mixed $filters, array $pages): array { |
| 14 | if (is_string($filters)) { |
| 15 | $filters = json_decode($filters, true); |
| 16 | } |
| 17 | if (!is_array($filters) || !array_is_list($filters)) { |
| 18 | throw new InvalidArgumentException('Invalid panorama filters'); |
| 19 | } |
| 20 | $reports = []; |
| 21 | foreach ($pages as $page) { |
| 22 | foreach ($page['reports'] ?? [] as $report) { |
| 23 | if ((int)($report['type'] ?? -1) === 0) $reports[(int)$report['value']] = true; |
| 24 | } |
| 25 | } |
| 26 | $ids = $targets = []; |
| 27 | $result = []; |
| 28 | foreach ($filters as $filter) { |
| 29 | if (!is_array($filter) || !is_string($filter['id'] ?? null) |
| 30 | || !preg_match('/^[a-zA-Z0-9_-]{1,80}$/D', $filter['id']) || isset($ids[$filter['id']]) |
| 31 | || !is_string($filter['label'] ?? null) || trim($filter['label']) === '' |
| 32 | || !is_bool($filter['enabled'] ?? null) |
| 33 | || (array_key_exists('showInHeader', $filter) && !is_bool($filter['showInHeader'])) |
| 34 | || !in_array($filter['operator'] ?? null, ['EQ', 'GT', 'LT', 'LIKE', 'NOTLIKE', 'IN', 'NOTIN'], true) |
| 35 | || !is_array($filter['mappings'] ?? null) || !array_is_list($filter['mappings']) |
| 36 | || (isset($filter['defaultValue']) && !is_string($filter['defaultValue']))) { |
| 37 | throw new InvalidArgumentException('Invalid panorama variable'); |
| 38 | } |
| 39 | $ids[$filter['id']] = true; |
| 40 | $mappings = []; |
| 41 | foreach ($filter['mappings'] as $mapping) { |
| 42 | if (!is_array($mapping) || !is_int($mapping['reportId'] ?? null) |
| 43 | || !isset($reports[$mapping['reportId']]) |
| 44 | || !is_string($mapping['dimension'] ?? null) || $mapping['dimension'] === '' |
| 45 | || !is_string($mapping['dimensionLabel'] ?? null)) { |
| 46 | throw new InvalidArgumentException('Invalid panorama dimension mapping'); |
| 47 | } |
| 48 | $key = $mapping['reportId'] . ':' . $mapping['dimension']; |
| 49 | if ($filter['enabled'] && isset($targets[$key])) { |
| 50 | throw new InvalidArgumentException('A dimension can only belong to one enabled variable'); |
| 51 | } |
| 52 | if ($filter['enabled']) $targets[$key] = true; |
| 53 | $mappings[] = array_intersect_key($mapping, array_flip(['reportId', 'dimension', 'dimensionLabel'])); |
| 54 | } |
| 55 | if ($filter['enabled'] && !$mappings) throw new InvalidArgumentException('Select a report dimension'); |
| 56 | $result[] = [ |
| 57 | 'id' => $filter['id'], 'label' => trim($filter['label']), 'enabled' => $filter['enabled'], |
| 58 | 'showInHeader' => $filter['showInHeader'] ?? true, |
| 59 | 'operator' => $filter['operator'], 'defaultValue' => $filter['defaultValue'] ?? null, |
| 60 | 'mappings' => $mappings, |
| 61 | ]; |
| 62 | } |
| 63 | return $result; |
| 64 | } |
| 65 | |
| 66 | public static function apply(array $metadata, array $filters, mixed $values): array { |
| 67 | if (is_string($values)) $values = json_decode($values, true); |
no outgoing calls
no test coverage detected