|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Elbformat\FieldHelperBundle\FieldHelper; |
| 6 | + |
| 7 | +use Elbformat\FieldHelperBundle\Exception\FieldNotFoundException; |
| 8 | +use Elbformat\FieldHelperBundle\Exception\InvalidFieldTypeException; |
| 9 | +use eZ\Publish\API\Repository\Values\Content\Content; |
| 10 | +use eZ\Publish\API\Repository\Values\Content\ContentStruct; |
| 11 | +use EzSystems\EzPlatformMatrixFieldtype\FieldType\Value; |
| 12 | + |
| 13 | +/** |
| 14 | + * @author Hannes Giesenow <hannes.giesenow@elbformat.de> |
| 15 | + */ |
| 16 | +class MatrixFieldHelper extends AbstractFieldHelper |
| 17 | +{ |
| 18 | + /** |
| 19 | + * Return a row-based 2-dimensional array. |
| 20 | + * [ |
| 21 | + * [A1,B1,C1], |
| 22 | + * [A2,B2,C2], |
| 23 | + * ] |
| 24 | + * |
| 25 | + * @return string[][] |
| 26 | + * |
| 27 | + * @throws FieldNotFoundException |
| 28 | + * @throws InvalidFieldTypeException |
| 29 | + */ |
| 30 | + public function getArray(Content $content, string $fieldName): array |
| 31 | + { |
| 32 | + $rows = $this->getValue($content, $fieldName)->getRows(); |
| 33 | + $result = []; |
| 34 | + /** @var Value\Row $row */ |
| 35 | + foreach ($rows as $row) { |
| 36 | + /** @var string[] $cells */ |
| 37 | + $cells = $row->getCells(); |
| 38 | + $result[] = array_values($cells); |
| 39 | + } |
| 40 | + |
| 41 | + return $result; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Return a key-value based array structure of the table. Each row has the headlines as key. |
| 46 | + * [ |
| 47 | + * [A1 => A2, B1 => B2, C1 => C2], |
| 48 | + * [A1 => A3, B1 => B3, C1 => C3], |
| 49 | + * ] |
| 50 | + * |
| 51 | + * @return mixed[][] |
| 52 | + * |
| 53 | + * @throws FieldNotFoundException |
| 54 | + * @throws InvalidFieldTypeException |
| 55 | + */ |
| 56 | + public function getAssoc(Content $content, string $fieldName): array |
| 57 | + { |
| 58 | + $rows = $this->getArray($content, $fieldName); |
| 59 | + $headlines = $this->getHeadlineIds($content, $fieldName); |
| 60 | + $numHeadlines = count($headlines); |
| 61 | + |
| 62 | + $result = []; |
| 63 | + /** @var Value\Row $row */ |
| 64 | + foreach ($rows as $cells) { |
| 65 | + $rowData = []; |
| 66 | + for ($i = 0; $i < $numHeadlines; $i++) { |
| 67 | + $rowData[$headlines[$i]] = $cells[$i]; |
| 68 | + } |
| 69 | + $result[] = $rowData; |
| 70 | + } |
| 71 | + |
| 72 | + return $result; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Return a key-value list for 2-column tables. |
| 77 | + * [A1 => B1, A2 => B2, A3 => B3] |
| 78 | + * |
| 79 | + * @return mixed[] |
| 80 | + */ |
| 81 | + public function getKeyValue(Content $content, string $fieldName, ?string $key = null, ?string $val = null): array |
| 82 | + { |
| 83 | + $rows = $this->getValue($content, $fieldName)->getRows(); |
| 84 | + if (null === $key) { |
| 85 | + $key = $this->getHeadlineIds($content, $fieldName)[0] ?? ''; |
| 86 | + } |
| 87 | + if (null === $val) { |
| 88 | + $val = $this->getHeadlineIds($content, $fieldName)[1] ?? ''; |
| 89 | + } |
| 90 | + |
| 91 | + $result = []; |
| 92 | + /** @var Value\Row $row */ |
| 93 | + foreach ($rows as $row) { |
| 94 | + /** @var string[] $cells */ |
| 95 | + $cells = $row->getCells(); |
| 96 | + $result[$cells[$key]] = $cells[$val] ?? ''; |
| 97 | + } |
| 98 | + |
| 99 | + return $result; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Return a list for 1-column tables. |
| 104 | + * [A1,A2,A3] |
| 105 | + * |
| 106 | + * @return string[] |
| 107 | + */ |
| 108 | + public function getList(Content $content, string $fieldName, string $columnName = null): array |
| 109 | + { |
| 110 | + if (null === $columnName) { |
| 111 | + $columnName = $this->getHeadlineIds($content, $fieldName)[0] ?? ''; |
| 112 | + } |
| 113 | + $rows = $this->getValue($content, $fieldName)->getRows(); |
| 114 | + $result = []; |
| 115 | + /** @var Value\Row $row */ |
| 116 | + foreach ($rows as $row) { |
| 117 | + $cells = $row->getCells(); |
| 118 | + $result[] = (string) $cells[$columnName]; |
| 119 | + } |
| 120 | + |
| 121 | + return $result; |
| 122 | + } |
| 123 | + |
| 124 | + public function isEmpty(Content $content, string $fieldName): bool |
| 125 | + { |
| 126 | + return $this->getValue($content, $fieldName)->getRows()->count() <= 0; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @param array<array<string,string>> $rowsWithCols |
| 131 | + */ |
| 132 | + public function updateAssoc(ContentStruct $struct, string $fieldName, array $rowsWithCols, ?Content $content = null): bool |
| 133 | + { |
| 134 | + // No changes |
| 135 | + if (null !== $content) { |
| 136 | + $current = $this->getArray($content, $fieldName); |
| 137 | + if ($this->arrayEquals($current, $rowsWithCols)) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // Convert to rows |
| 143 | + $rows = []; |
| 144 | + foreach ($rowsWithCols as $cols) { |
| 145 | + $rows[] = new Value\Row($cols); |
| 146 | + } |
| 147 | + |
| 148 | + $struct->setField($fieldName, $rows); |
| 149 | + |
| 150 | + return true; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * @param string[] $values |
| 155 | + */ |
| 156 | + public function updateList(ContentStruct $struct, string $fieldName, array $values, string $columnName, ?Content $content = null): bool |
| 157 | + { |
| 158 | + // No changes |
| 159 | + if (null !== $content) { |
| 160 | + $field = $this->getList($content, $fieldName, $columnName); |
| 161 | + if ($this->isListEqual($field, $values)) { |
| 162 | + return false; |
| 163 | + } |
| 164 | + } |
| 165 | + // Convert to rows |
| 166 | + $rows = []; |
| 167 | + foreach ($values as $value) { |
| 168 | + $rows[] = new Value\Row([$columnName => $value]); |
| 169 | + } |
| 170 | + |
| 171 | + $struct->setField($fieldName, $rows); |
| 172 | + |
| 173 | + return true; |
| 174 | + } |
| 175 | + |
| 176 | + protected function getValue(Content $content, string $fieldName): Value |
| 177 | + { |
| 178 | + $field = $this->getField($content, $fieldName); |
| 179 | + if (!$field->value instanceof Value) { |
| 180 | + throw InvalidFieldTypeException::fromActualAndExpected($field->value, [Value::class]); |
| 181 | + } |
| 182 | + |
| 183 | + return $field->value; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * @param string[] $list1 |
| 188 | + * @param string[] $list2 |
| 189 | + */ |
| 190 | + protected function isListEqual(array $list1, array $list2): bool |
| 191 | + { |
| 192 | + if (\count($list1) !== \count($list2)) { |
| 193 | + return false; |
| 194 | + } |
| 195 | + for ($i = 0, $iMax = \count($list1); $i < $iMax; ++$i) { |
| 196 | + if ($list1[$i] !== $list2[$i]) { |
| 197 | + return false; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + return true; |
| 202 | + } |
| 203 | + |
| 204 | + /** @return string[] */ |
| 205 | + protected function getHeadlineIds(Content $content, string $fieldName): array |
| 206 | + { |
| 207 | + $fieldDef = $content->getContentType()->getFieldDefinition($fieldName); |
| 208 | + if (null === $fieldDef) { |
| 209 | + return []; |
| 210 | + } |
| 211 | + |
| 212 | + $colIds = []; |
| 213 | + /** @var array[] $columns */ |
| 214 | + $columns = $fieldDef->fieldSettings['columns']; |
| 215 | + foreach ($columns as $colDef) { |
| 216 | + $colIds[] = (string) $colDef['identifier']; |
| 217 | + } |
| 218 | + |
| 219 | + return $colIds; |
| 220 | + } |
| 221 | + |
| 222 | + protected function arrayEquals(array $arr1, array $arr2): bool |
| 223 | + { |
| 224 | + return $this->arrayContains($arr1, $arr2) && $this->arrayContains($arr2, $arr1); |
| 225 | + } |
| 226 | + |
| 227 | + protected function arrayContains(array $arr1, array $arr2): bool |
| 228 | + { |
| 229 | + return 1 > count( |
| 230 | + array_udiff($arr1, $arr2, function ($v1, $v2): int { |
| 231 | + if (is_array($v1) && is_array($v2)) { |
| 232 | + return !$this->arrayContains($v1, $v2) ? 1 : 0; |
| 233 | + } |
| 234 | + if (is_array($v1) || is_array($v2)) { |
| 235 | + return 1; |
| 236 | + } |
| 237 | + |
| 238 | + return $v1 !== $v2 ? 1 : 0; |
| 239 | + }) |
| 240 | + ); |
| 241 | + } |
| 242 | +} |
0 commit comments