|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PSFS\base\types\traits\Api; |
| 4 | + |
| 5 | +use Propel\Runtime\ActiveQuery\ModelCriteria; |
| 6 | +use Propel\Runtime\Map\ColumnMap; |
| 7 | +use Propel\Runtime\Map\TableMap; |
| 8 | +use PSFS\base\config\Config; |
| 9 | +use PSFS\base\exception\ApiException; |
| 10 | +use PSFS\base\Request; |
| 11 | +use PSFS\base\types\Api; |
| 12 | + |
| 13 | +trait MutationExtraColumnsTrait |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @throws ApiException |
| 17 | + */ |
| 18 | + protected function addPkToList() |
| 19 | + { |
| 20 | + foreach ($this->getPkDbName() as $extraColumn => $columnName) { |
| 21 | + $this->extraColumns[$extraColumn] = $columnName; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @param TableMap $tableMap |
| 27 | + */ |
| 28 | + private function addClassListName(TableMap $tableMap) |
| 29 | + { |
| 30 | + $segments = []; |
| 31 | + foreach ($tableMap->getPrimaryKeys() as $pk) { |
| 32 | + $segments[] = $pk->getFullyQualifiedName(); |
| 33 | + } |
| 34 | + |
| 35 | + $this->extraColumns[$this->buildClassListNameExpression((string)$tableMap->getPhpName(), $segments)] = Api::API_LIST_NAME_FIELD; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param array<int, string> $segments |
| 40 | + */ |
| 41 | + private function buildClassListNameExpression(string $phpName, array $segments): string |
| 42 | + { |
| 43 | + if (count($segments) === 0) { |
| 44 | + return 'CONCAT("' . $phpName . ' #")'; |
| 45 | + } |
| 46 | + |
| 47 | + return 'CONCAT("' . $phpName . ' #", ' . implode(', "|", ', $segments) . ')'; |
| 48 | + } |
| 49 | + |
| 50 | + protected function addDefaultListField() |
| 51 | + { |
| 52 | + if (!in_array(Api::API_LIST_NAME_FIELD, array_values($this->extraColumns), true)) { |
| 53 | + $tableMap = $this->getTableMap(); |
| 54 | + $column = $this->resolveDefaultListColumn($tableMap); |
| 55 | + if (null !== $column) { |
| 56 | + $this->extraColumns[$column->getFullyQualifiedName()] = Api::API_LIST_NAME_FIELD; |
| 57 | + } else { |
| 58 | + $this->addClassListName($tableMap); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param ModelCriteria $query |
| 65 | + * @param mixed $action |
| 66 | + * @throws ApiException |
| 67 | + */ |
| 68 | + private function addExtraColumns(ModelCriteria &$query, $action) |
| 69 | + { |
| 70 | + $this->prepareLegacyListExtraColumns((string)$action); |
| 71 | + if (empty($this->extraColumns)) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + $fields = $this->resolveRequestedExtraFields(); |
| 76 | + foreach ($this->extraColumns as $expression => $columnName) { |
| 77 | + if ($this->shouldIncludeExtraColumn($columnName, $fields)) { |
| 78 | + $query->withColumn($expression, $columnName); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private function prepareLegacyListExtraColumns(string $action): void |
| 84 | + { |
| 85 | + if (Api::API_ACTION_LIST !== $action) { |
| 86 | + return; |
| 87 | + } |
| 88 | + // Legacy tokens kept for compatibility (`__name__`, `__pk`). |
| 89 | + // Planned future cleanup can remove them behind a versioned contract switch. |
| 90 | + $this->addDefaultListField(); |
| 91 | + $this->addPkToList(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @param array<int, string> $fields |
| 96 | + */ |
| 97 | + private function shouldIncludeExtraColumn(string $columnName, array $fields): bool |
| 98 | + { |
| 99 | + return empty($fields) || in_array($columnName, $fields, true); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @return array<string, string> |
| 104 | + */ |
| 105 | + protected function parseExtraColumns() |
| 106 | + { |
| 107 | + $columns = []; |
| 108 | + foreach ($this->extraColumns as $columnName) { |
| 109 | + $columns[$columnName] = strtolower($columnName); |
| 110 | + } |
| 111 | + return $columns; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @return array<int, string> |
| 116 | + */ |
| 117 | + protected function resolveRequestedExtraFields(): array |
| 118 | + { |
| 119 | + if (Config::getParam('api.extrafields.compat', true)) { |
| 120 | + return array_values($this->extraColumns); |
| 121 | + } |
| 122 | + return $this->normalizeRequestedExtraFields( |
| 123 | + (string)(Request::getInstance()->getQuery(Api::API_FIELDS_RESULT_FIELD) ?? '') |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @return array<int, string> |
| 129 | + */ |
| 130 | + private function normalizeRequestedExtraFields(string $returnFields): array |
| 131 | + { |
| 132 | + $fields = explode(',', $returnFields); |
| 133 | + $fields[] = self::API_MODEL_KEY_FIELD; |
| 134 | + $fields = array_values(array_filter(array_map('trim', $fields), static fn(string $field): bool => $field !== '')); |
| 135 | + |
| 136 | + return array_values(array_unique($fields)); |
| 137 | + } |
| 138 | + |
| 139 | + private function resolveDefaultListColumn(TableMap $tableMap): ?ColumnMap |
| 140 | + { |
| 141 | + foreach (['NAME', 'TITLE', 'LABEL'] as $columnName) { |
| 142 | + if ($tableMap->hasColumn($columnName)) { |
| 143 | + return $tableMap->getColumn($columnName); |
| 144 | + } |
| 145 | + } |
| 146 | + return null; |
| 147 | + } |
| 148 | +} |
0 commit comments