| 64 | } |
| 65 | |
| 66 | public static function apply(array $metadata, array $filters, mixed $values): array { |
| 67 | if (is_string($values)) $values = json_decode($values, true); |
| 68 | if (!is_array($values)) throw new InvalidArgumentException('Invalid panorama values'); |
| 69 | $enabled = []; |
| 70 | foreach ($filters as $filter) { |
| 71 | if ($filter['enabled']) $enabled[$filter['id']] = $filter; |
| 72 | } |
| 73 | $options = json_decode($metadata['filteroptions'] ?? '', true) ?: []; |
| 74 | $conditions = []; |
| 75 | foreach ($options['filter'] ?? [] as $key => $condition) { |
| 76 | $condition['dimension'] = $condition['dimension'] ?? $key; |
| 77 | $conditions[] = $condition; |
| 78 | } |
| 79 | foreach ($values as $id => $value) { |
| 80 | if (!isset($enabled[$id])) { |
| 81 | throw new InvalidArgumentException('Unknown or disabled panorama variable'); |
| 82 | } |
| 83 | if ($value === null || $value === '') continue; |
| 84 | // Older viewers send a value alone; newer viewers send report-style conditions. |
| 85 | $selections = is_string($value) ? [['option' => $enabled[$id]['operator'], 'value' => $value]] : $value; |
| 86 | if (!is_array($selections) || !array_is_list($selections)) { |
| 87 | throw new InvalidArgumentException('Invalid panorama conditions'); |
| 88 | } |
| 89 | foreach ($selections as $selection) { |
| 90 | if (!is_array($selection) || !in_array($selection['option'] ?? null, ['EQ', 'GT', 'LT', 'LIKE', 'NOTLIKE', 'IN', 'NOTIN'], true) |
| 91 | || !is_string($selection['value'] ?? null)) { |
| 92 | throw new InvalidArgumentException('Invalid panorama condition'); |
| 93 | } |
| 94 | } |
| 95 | $selections = array_values(array_filter($selections, static fn($selection) => $selection['value'] !== '')); |
| 96 | if (!$selections) continue; |
| 97 | foreach ($enabled[$id]['mappings'] as $mapping) { |
| 98 | if ($mapping['reportId'] !== (int)$metadata['id']) continue; |
| 99 | $dimension = $mapping['dimension']; |
| 100 | $conditions = array_values(array_filter($conditions, |
| 101 | static fn($condition) => (string)$condition['dimension'] !== $dimension)); |
| 102 | foreach ($selections as $selection) { |
| 103 | $conditions[] = ['dimension' => $dimension, 'option' => $selection['option'], 'value' => $selection['value']]; |
| 104 | } |
| 105 | $metadata['panoramaMappings'][] = $mapping; |
| 106 | } |
| 107 | } |
| 108 | if (!empty($metadata['panoramaMappings'])) { |
| 109 | $options['filter'] = $conditions; |
| 110 | $metadata['filteroptions'] = json_encode($options); |
| 111 | } |
| 112 | return $metadata; |
| 113 | } |
| 114 | |
| 115 | public static function validateDimensions(array $mappings, array $dimensions): void { |
| 116 | foreach ($mappings as $mapping) { |