|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace wcf\system\form\builder\data\processor; |
| 4 | + |
| 5 | +use BadMethodCallException; |
| 6 | +use InvalidArgumentException; |
| 7 | +use wcf\data\IStorableObject; |
| 8 | +use wcf\system\form\builder\container\IFormContainer; |
| 9 | +use wcf\system\form\builder\field\IFormField; |
| 10 | +use wcf\system\form\builder\IFormDocument; |
| 11 | +use wcf\system\form\builder\IFormNode; |
| 12 | + |
| 13 | +/** |
| 14 | + * data processor putting the given container's children data into an array |
| 15 | + * |
| 16 | + * @author Florian Gail |
| 17 | + * @copyright Florian Gail; 2018 - 2022; <https://www.mysterycode.de> |
| 18 | + */ |
| 19 | +class MCGroupedFormDataProcessor extends AbstractFormDataProcessor |
| 20 | +{ |
| 21 | + /** |
| 22 | + * processor id primarily used for error messages |
| 23 | + * |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + protected string $id; |
| 27 | + |
| 28 | + /** |
| 29 | + * Initializes a new PrefixedFormDataProcessor object. |
| 30 | + * |
| 31 | + * @param string $nodeId processor id primarily used for error messages, does not have to be unique |
| 32 | + * |
| 33 | + * @throws InvalidArgumentException if either id or processor callable are invalid |
| 34 | + */ |
| 35 | + public function __construct(string $nodeId) |
| 36 | + { |
| 37 | + if (\preg_match('~^[a-z][A-z\d-]*$~', $nodeId) !== 1) { |
| 38 | + throw new InvalidArgumentException("Invalid id '{$nodeId}' given."); |
| 39 | + } |
| 40 | + |
| 41 | + $this->id = $nodeId; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @inheritDoc |
| 46 | + */ |
| 47 | + public function processFormData(IFormDocument $document, array $parameters): array |
| 48 | + { |
| 49 | + $container = $document->getNodeById($this->id); |
| 50 | + if ($container === null || !$container->hasChildren()) { |
| 51 | + if (ENABLE_DEBUG_MODE) { |
| 52 | + throw new BadMethodCallException("Cannot find node with id '{$this->id}'."); |
| 53 | + } |
| 54 | + |
| 55 | + return $parameters; |
| 56 | + } |
| 57 | + |
| 58 | + $this->processNode($container, $parameters); |
| 59 | + |
| 60 | + return $parameters; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param IFormNode $node |
| 65 | + * @param array $parameters |
| 66 | + */ |
| 67 | + protected function processNode(IFormNode $node, array &$parameters): void |
| 68 | + { |
| 69 | + if ($node instanceof IFormContainer) { |
| 70 | + foreach ($node->children() as $childNode) { |
| 71 | + $this->processNode($childNode, $parameters[$node->getId()]); |
| 72 | + } |
| 73 | + |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if (!($node instanceof IFormField)) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + unset($parameters['data'][$id]); |
| 82 | + $parameters[$node->getId()] = $node->getValue(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @inheritDoc |
| 87 | + */ |
| 88 | + public function processObjectData(IFormDocument $document, array $data, IStorableObject $object): array |
| 89 | + { |
| 90 | + $container = $document->getNodeById($this->id); |
| 91 | + if ($container === null || !$container->hasChildren()) { |
| 92 | + if (ENABLE_DEBUG_MODE) { |
| 93 | + throw new BadMethodCallException("Cannot find node with id '{$this->id}'."); |
| 94 | + } |
| 95 | + |
| 96 | + return $data; |
| 97 | + } |
| 98 | + |
| 99 | + if (isset($data[$container->getId()])) { |
| 100 | + $this->processNodeObject($container, $data[$container->getId()], $data[$container->getId()]); |
| 101 | + unset($data[$container->getId()]); |
| 102 | + } |
| 103 | + |
| 104 | + return $data; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @param IFormNode $node |
| 109 | + * @param array $data |
| 110 | + * @param array $tmpData |
| 111 | + */ |
| 112 | + protected function processNodeObject(IFormNode $node, array &$data, array $tmpData): void |
| 113 | + { |
| 114 | + if (!isset($tmpData[$node->getId()])) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + if ($node instanceof IFormContainer) { |
| 119 | + foreach ($node->children() as $childNode) { |
| 120 | + $this->processNodeObject($childNode, $data, $tmpData[$childNode->getId()]); |
| 121 | + } |
| 122 | + |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + if (!($node instanceof IFormField)) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + $data[$node->getId()] = $tmpData[$node->getId()]; |
| 131 | + } |
| 132 | +} |
0 commit comments