From: Matthias Schmidt Date: Thu, 3 Jan 2019 17:07:31 +0000 (+0100) Subject: Move form builder’s data processors into separate namespace X-Git-Tag: 5.2.0_Alpha_1~383^2~2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=76d761f6a472b5cd1dc112dd118dfc2b42718caa;p=GitHub%2FWoltLab%2FWCF.git Move form builder’s data processors into separate namespace See #2509 --- diff --git a/wcfsetup/install/files/lib/acp/form/LanguageItemAddForm.class.php b/wcfsetup/install/files/lib/acp/form/LanguageItemAddForm.class.php index 177660c304..c33362caff 100644 --- a/wcfsetup/install/files/lib/acp/form/LanguageItemAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/LanguageItemAddForm.class.php @@ -5,8 +5,8 @@ use wcf\data\language\item\LanguageItemAction; use wcf\data\language\item\LanguageItemList; use wcf\form\AbstractFormBuilderForm; use wcf\system\form\builder\container\FormContainer; -use wcf\system\form\builder\field\data\CustomFormFieldDataProcessor; -use wcf\system\form\builder\field\data\VoidFormFieldDataProcessor; +use wcf\system\form\builder\field\data\processor\CustomFormFieldDataProcessor; +use wcf\system\form\builder\field\data\processor\VoidFormFieldDataProcessor; use wcf\system\form\builder\field\dependency\ValueFormFieldDependency; use wcf\system\form\builder\field\MultilineTextFormField; use wcf\system\form\builder\field\RadioButtonFormField; diff --git a/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php b/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php index 04646f84d7..005233e526 100644 --- a/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/FormDocument.class.php @@ -3,7 +3,7 @@ namespace wcf\system\form\builder; use wcf\data\IStorableObject; use wcf\system\form\builder\data\FormDataHandler; use wcf\system\form\builder\data\IFormDataHandler; -use wcf\system\form\builder\field\data\DefaultFormFieldDataProcessor; +use wcf\system\form\builder\field\data\processor\DefaultFormFieldDataProcessor; use wcf\system\form\builder\field\IFileFormField; use wcf\system\form\builder\field\IFormField; use wcf\system\WCF; diff --git a/wcfsetup/install/files/lib/system/form/builder/data/FormDataHandler.class.php b/wcfsetup/install/files/lib/system/form/builder/data/FormDataHandler.class.php index f75b4a40a7..ef11f01cf6 100644 --- a/wcfsetup/install/files/lib/system/form/builder/data/FormDataHandler.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/data/FormDataHandler.class.php @@ -1,6 +1,6 @@ - * @package WoltLabSuite\Core\System\Form\Builder\Field\Data - * @since 3.2 - */ -class CustomFormFieldDataProcessor implements IFormFieldDataProcessor { - /** - * processor id primarily used for error messages - * @var string - */ - protected $id; - - /** - * callable processing the data - * @var callable - */ - protected $processor; - - /** - * Initializes a new CustomFormFieldDataProcessor object. - * - * @param string $id processor id primarily used for error messages, does not have to be unique - * @param callable $processor processor callable - * - * @throws \InvalidArgumentException if either id or processor callable are invalid - */ - public function __construct($id, callable $processor) { - if (preg_match('~^[a-z][A-z0-9-]*$~', $id) !== 1) { - throw new \InvalidArgumentException("Invalid id '{$id}' given."); - } - - $this->id = $id; - - // validate processor function - $parameters = (new \ReflectionFunction($processor))->getParameters(); - if (count($parameters) !== 2) { - throw new \InvalidArgumentException( - "The processor function must expect two parameters, instead " . count($parameters) . - " parameter" . (count($parameters) !== 1 ? 's' : '') . " are expected." - ); - } - - /** @var \ReflectionClass $parameterClass */ - $parameterClass = $parameters[0]->getClass(); - if ($parameterClass === null || ($parameterClass->getName() !== IFormDocument::class && !is_subclass_of($parameterClass->getName(), IFormDocument::class))) { - throw new \InvalidArgumentException( - "The processor function's first parameter must be an instance of '" . IFormDocument::class . "', instead " . - ($parameterClass === null ? 'any' : "'" . $parameterClass->getName() . "'") . " parameter is expected." - ); - } - if (!$parameters[1]->isArray()) { - throw new \InvalidArgumentException("The processor function's second parameter must be an array."); - } - - $this->processor = $processor; - } - - /** - * @inheritDoc - */ - public function __invoke(IFormDocument $document, array $parameters) { - $parameters = call_user_func($this->processor, $document, $parameters); - - if (!is_array($parameters)) { - throw new \UnexpectedValueException("Field data processor '{$this->id}' does not return an array."); - } - - return $parameters; - } -} diff --git a/wcfsetup/install/files/lib/system/form/builder/field/data/DefaultFormFieldDataProcessor.class.php b/wcfsetup/install/files/lib/system/form/builder/field/data/DefaultFormFieldDataProcessor.class.php deleted file mode 100644 index 48fadfc927..0000000000 --- a/wcfsetup/install/files/lib/system/form/builder/field/data/DefaultFormFieldDataProcessor.class.php +++ /dev/null @@ -1,49 +0,0 @@ - - * @package WoltLabSuite\Core\System\Form\Builder\Field\Data - * @since 3.2 - */ -class DefaultFormFieldDataProcessor implements IFormFieldDataProcessor { - /** - * @inheritDoc - */ - public function __invoke(IFormDocument $document, array $parameters) { - $parameters['data'] = []; - - $this->getData($document, $parameters['data']); - - return $parameters; - } - - /** - * Fetches all data from the given node and stores it in the given array. - * - * @param IFormNode $node node whose data will be fetched - * @param array $data data storage - */ - protected function getData(IFormNode $node, array &$data) { - if ($node->checkDependencies()) { - if ($node instanceof IFormParentNode) { - foreach ($node as $childNode) { - $this->getData($childNode, $data); - } - } - else if ($node instanceof IFormField && $node->isAvailable() && $node->hasSaveValue()) { - $data[$node->getObjectProperty()] = $node->getSaveValue(); - } - } - } -} diff --git a/wcfsetup/install/files/lib/system/form/builder/field/data/IFormFieldDataProcessor.class.php b/wcfsetup/install/files/lib/system/form/builder/field/data/IFormFieldDataProcessor.class.php deleted file mode 100644 index 20683fe252..0000000000 --- a/wcfsetup/install/files/lib/system/form/builder/field/data/IFormFieldDataProcessor.class.php +++ /dev/null @@ -1,24 +0,0 @@ - - * @package WoltLabSuite\Core\System\Form\Builder\Field\Data - * @since 3.2 - */ -interface IFormFieldDataProcessor { - /** - * Processes the given parameters array and returns the processed version of it. - * - * @param IFormDocument $document documents whose field data is processed - * @param array $parameters parameters before processing - * @return array parameters after processing - */ - public function __invoke(IFormDocument $document, array $parameters); -} diff --git a/wcfsetup/install/files/lib/system/form/builder/field/data/VoidFormFieldDataProcessor.class.php b/wcfsetup/install/files/lib/system/form/builder/field/data/VoidFormFieldDataProcessor.class.php deleted file mode 100644 index 1059efbdca..0000000000 --- a/wcfsetup/install/files/lib/system/form/builder/field/data/VoidFormFieldDataProcessor.class.php +++ /dev/null @@ -1,53 +0,0 @@ - - * @package WoltLabSuite\Core\System\Form\Builder\Field\Data - * @since 3.2 - */ -class VoidFormFieldDataProcessor implements IFormFieldDataProcessor { - /** - * is `true` if the property is stored in the `data` array - * @var bool - */ - protected $isDataProperty; - - /** - * name of the voided property - * @var string - */ - protected $property; - - /** - * Initializes a new CustomFormFieldDataProcessor object. - * - * @param string $property name of the voided property - * @param bool $isDataProperty is `true` if the property is stored in the `data` array - */ - public function __construct($property, $isDataProperty = true) { - $this->property = $property; - $this->isDataProperty = $isDataProperty; - } - - /** - * @inheritDoc - */ - public function __invoke(IFormDocument $document, array $parameters) { - if ($this->isDataProperty) { - if (isset($parameters['data'][$this->property])) { - unset($parameters['data'][$this->property]); - } - } - else if (isset($parameters[$this->property])) { - unset($parameters[$this->property]); - } - - return $parameters; - } -} diff --git a/wcfsetup/install/files/lib/system/form/builder/field/data/processor/CustomFormFieldDataProcessor.class.php b/wcfsetup/install/files/lib/system/form/builder/field/data/processor/CustomFormFieldDataProcessor.class.php new file mode 100644 index 0000000000..6ffb6f3131 --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/data/processor/CustomFormFieldDataProcessor.class.php @@ -0,0 +1,78 @@ + + * @package WoltLabSuite\Core\System\Form\Builder\Field\Data\Processor + * @since 3.2 + */ +class CustomFormFieldDataProcessor implements IFormFieldDataProcessor { + /** + * processor id primarily used for error messages + * @var string + */ + protected $id; + + /** + * callable processing the data + * @var callable + */ + protected $processor; + + /** + * Initializes a new CustomFormFieldDataProcessor object. + * + * @param string $id processor id primarily used for error messages, does not have to be unique + * @param callable $processor processor callable + * + * @throws \InvalidArgumentException if either id or processor callable are invalid + */ + public function __construct($id, callable $processor) { + if (preg_match('~^[a-z][A-z0-9-]*$~', $id) !== 1) { + throw new \InvalidArgumentException("Invalid id '{$id}' given."); + } + + $this->id = $id; + + // validate processor function + $parameters = (new \ReflectionFunction($processor))->getParameters(); + if (count($parameters) !== 2) { + throw new \InvalidArgumentException( + "The processor function must expect two parameters, instead " . count($parameters) . + " parameter" . (count($parameters) !== 1 ? 's' : '') . " are expected." + ); + } + + /** @var \ReflectionClass $parameterClass */ + $parameterClass = $parameters[0]->getClass(); + if ($parameterClass === null || ($parameterClass->getName() !== IFormDocument::class && !is_subclass_of($parameterClass->getName(), IFormDocument::class))) { + throw new \InvalidArgumentException( + "The processor function's first parameter must be an instance of '" . IFormDocument::class . "', instead " . + ($parameterClass === null ? 'any' : "'" . $parameterClass->getName() . "'") . " parameter is expected." + ); + } + if (!$parameters[1]->isArray()) { + throw new \InvalidArgumentException("The processor function's second parameter must be an array."); + } + + $this->processor = $processor; + } + + /** + * @inheritDoc + */ + public function __invoke(IFormDocument $document, array $parameters) { + $parameters = call_user_func($this->processor, $document, $parameters); + + if (!is_array($parameters)) { + throw new \UnexpectedValueException("Field data processor '{$this->id}' does not return an array."); + } + + return $parameters; + } +} diff --git a/wcfsetup/install/files/lib/system/form/builder/field/data/processor/DefaultFormFieldDataProcessor.class.php b/wcfsetup/install/files/lib/system/form/builder/field/data/processor/DefaultFormFieldDataProcessor.class.php new file mode 100644 index 0000000000..c3b17eb584 --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/data/processor/DefaultFormFieldDataProcessor.class.php @@ -0,0 +1,49 @@ + + * @package WoltLabSuite\Core\System\Form\Builder\Field\Data\Processor + * @since 3.2 + */ +class DefaultFormFieldDataProcessor implements IFormFieldDataProcessor { + /** + * @inheritDoc + */ + public function __invoke(IFormDocument $document, array $parameters) { + $parameters['data'] = []; + + $this->getData($document, $parameters['data']); + + return $parameters; + } + + /** + * Fetches all data from the given node and stores it in the given array. + * + * @param IFormNode $node node whose data will be fetched + * @param array $data data storage + */ + protected function getData(IFormNode $node, array &$data) { + if ($node->checkDependencies()) { + if ($node instanceof IFormParentNode) { + foreach ($node as $childNode) { + $this->getData($childNode, $data); + } + } + else if ($node instanceof IFormField && $node->isAvailable() && $node->hasSaveValue()) { + $data[$node->getObjectProperty()] = $node->getSaveValue(); + } + } + } +} diff --git a/wcfsetup/install/files/lib/system/form/builder/field/data/processor/IFormFieldDataProcessor.class.php b/wcfsetup/install/files/lib/system/form/builder/field/data/processor/IFormFieldDataProcessor.class.php new file mode 100644 index 0000000000..a179fc9dc7 --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/data/processor/IFormFieldDataProcessor.class.php @@ -0,0 +1,24 @@ + + * @package WoltLabSuite\Core\System\Form\Builder\Field\Data\Processor + * @since 3.2 + */ +interface IFormFieldDataProcessor { + /** + * Processes the given parameters array and returns the processed version of it. + * + * @param IFormDocument $document documents whose field data is processed + * @param array $parameters parameters before processing + * @return array parameters after processing + */ + public function __invoke(IFormDocument $document, array $parameters); +} diff --git a/wcfsetup/install/files/lib/system/form/builder/field/data/processor/VoidFormFieldDataProcessor.class.php b/wcfsetup/install/files/lib/system/form/builder/field/data/processor/VoidFormFieldDataProcessor.class.php new file mode 100644 index 0000000000..50331c2e90 --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/data/processor/VoidFormFieldDataProcessor.class.php @@ -0,0 +1,53 @@ + + * @package WoltLabSuite\Core\System\Form\Builder\Field\Data\Processor + * @since 3.2 + */ +class VoidFormFieldDataProcessor implements IFormFieldDataProcessor { + /** + * is `true` if the property is stored in the `data` array + * @var bool + */ + protected $isDataProperty; + + /** + * name of the voided property + * @var string + */ + protected $property; + + /** + * Initializes a new CustomFormFieldDataProcessor object. + * + * @param string $property name of the voided property + * @param bool $isDataProperty is `true` if the property is stored in the `data` array + */ + public function __construct($property, $isDataProperty = true) { + $this->property = $property; + $this->isDataProperty = $isDataProperty; + } + + /** + * @inheritDoc + */ + public function __invoke(IFormDocument $document, array $parameters) { + if ($this->isDataProperty) { + if (isset($parameters['data'][$this->property])) { + unset($parameters['data'][$this->property]); + } + } + else if (isset($parameters[$this->property])) { + unset($parameters[$this->property]); + } + + return $parameters; + } +} diff --git a/wcfsetup/install/files/lib/system/form/builder/field/tag/TagFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/tag/TagFormField.class.php index 08cb22e195..4e419d211a 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/tag/TagFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/tag/TagFormField.class.php @@ -3,7 +3,7 @@ namespace wcf\system\form\builder\field\tag; use wcf\data\tag\Tag; use wcf\data\IStorableObject; use wcf\system\form\builder\field\AbstractFormField; -use wcf\system\form\builder\field\data\CustomFormFieldDataProcessor; +use wcf\system\form\builder\field\data\processor\CustomFormFieldDataProcessor; use wcf\system\form\builder\field\IObjectTypeFormField; use wcf\system\form\builder\field\TObjectTypeFormField; use wcf\system\form\builder\IFormDocument; diff --git a/wcfsetup/install/files/lib/system/package/plugin/AbstractOptionPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/AbstractOptionPackageInstallationPlugin.class.php index d8eee8223a..9721e4944d 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/AbstractOptionPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/AbstractOptionPackageInstallationPlugin.class.php @@ -11,7 +11,7 @@ use wcf\system\devtools\pip\TXmlGuiPackageInstallationPlugin; use wcf\system\exception\SystemException; use wcf\system\form\builder\container\IFormContainer; use wcf\system\form\builder\field\BooleanFormField; -use wcf\system\form\builder\field\data\CustomFormFieldDataProcessor; +use wcf\system\form\builder\field\data\processor\CustomFormFieldDataProcessor; use wcf\system\form\builder\field\dependency\ValueFormFieldDependency; use wcf\system\form\builder\field\IntegerFormField; use wcf\system\form\builder\field\MultilineTextFormField; diff --git a/wcfsetup/install/files/lib/system/package/plugin/BBCodePackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/BBCodePackageInstallationPlugin.class.php index d9265f2bf1..233278c4bb 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/BBCodePackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/BBCodePackageInstallationPlugin.class.php @@ -15,7 +15,7 @@ use wcf\system\form\builder\container\FormContainer; use wcf\system\form\builder\field\bbcode\BBCodeAttributesFormField; use wcf\system\form\builder\field\BooleanFormField; use wcf\system\form\builder\field\ClassNameFormField; -use wcf\system\form\builder\field\data\VoidFormFieldDataProcessor; +use wcf\system\form\builder\field\data\processor\VoidFormFieldDataProcessor; use wcf\system\form\builder\field\dependency\NonEmptyFormFieldDependency; use wcf\system\form\builder\field\dependency\ValueFormFieldDependency; use wcf\system\form\builder\field\IconFormField; diff --git a/wcfsetup/install/files/lib/system/package/plugin/MediaProviderPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/MediaProviderPackageInstallationPlugin.class.php index 16145a99d3..319c1ec6a3 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/MediaProviderPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/MediaProviderPackageInstallationPlugin.class.php @@ -8,7 +8,7 @@ use wcf\system\devtools\pip\IGuiPackageInstallationPlugin; use wcf\system\devtools\pip\TXmlGuiPackageInstallationPlugin; use wcf\system\form\builder\container\FormContainer; use wcf\system\form\builder\field\ClassNameFormField; -use wcf\system\form\builder\field\data\CustomFormFieldDataProcessor; +use wcf\system\form\builder\field\data\processor\CustomFormFieldDataProcessor; use wcf\system\form\builder\field\MultilineTextFormField; use wcf\system\form\builder\field\TextFormField; use wcf\system\form\builder\field\TitleFormField; diff --git a/wcfsetup/install/files/lib/system/package/plugin/OptionPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/OptionPackageInstallationPlugin.class.php index 85c7f366c1..f71c35297b 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/OptionPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/OptionPackageInstallationPlugin.class.php @@ -9,7 +9,7 @@ use wcf\system\devtools\pip\IGuiPackageInstallationPlugin; use wcf\system\exception\SystemException; use wcf\system\form\builder\container\IFormContainer; use wcf\system\form\builder\field\BooleanFormField; -use wcf\system\form\builder\field\data\CustomFormFieldDataProcessor; +use wcf\system\form\builder\field\data\processor\CustomFormFieldDataProcessor; use wcf\system\form\builder\field\dependency\NonEmptyFormFieldDependency; use wcf\system\form\builder\field\dependency\ValueFormFieldDependency; use wcf\system\form\builder\field\MultilineTextFormField; diff --git a/wcfsetup/install/files/lib/system/package/plugin/SmileyPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/SmileyPackageInstallationPlugin.class.php index d9d587e44c..04e6b24513 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/SmileyPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/SmileyPackageInstallationPlugin.class.php @@ -6,7 +6,7 @@ use wcf\system\devtools\pip\IDevtoolsPipEntryList; use wcf\system\devtools\pip\IGuiPackageInstallationPlugin; use wcf\system\devtools\pip\TXmlGuiPackageInstallationPlugin; use wcf\system\form\builder\container\FormContainer; -use wcf\system\form\builder\field\data\CustomFormFieldDataProcessor; +use wcf\system\form\builder\field\data\processor\CustomFormFieldDataProcessor; use wcf\system\form\builder\field\IntegerFormField; use wcf\system\form\builder\field\ItemListFormField; use wcf\system\form\builder\field\TextFormField; diff --git a/wcfsetup/install/files/lib/system/package/plugin/TemplateListenerPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/TemplateListenerPackageInstallationPlugin.class.php index 1d11a9c53c..5f2188d3e6 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/TemplateListenerPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/TemplateListenerPackageInstallationPlugin.class.php @@ -11,7 +11,7 @@ use wcf\system\devtools\pip\IDevtoolsPipEntryList; use wcf\system\devtools\pip\IGuiPackageInstallationPlugin; use wcf\system\devtools\pip\TXmlGuiPackageInstallationPlugin; use wcf\system\form\builder\container\FormContainer; -use wcf\system\form\builder\field\data\CustomFormFieldDataProcessor; +use wcf\system\form\builder\field\data\processor\CustomFormFieldDataProcessor; use wcf\system\form\builder\field\dependency\ValueFormFieldDependency; use wcf\system\form\builder\field\IntegerFormField; use wcf\system\form\builder\field\option\OptionFormField; diff --git a/wcfsetup/install/files/lib/system/package/plugin/UserOptionPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/UserOptionPackageInstallationPlugin.class.php index 1c9b3df7e9..05dd37aefa 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/UserOptionPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/UserOptionPackageInstallationPlugin.class.php @@ -12,7 +12,7 @@ use wcf\system\exception\SystemException; use wcf\system\form\builder\container\IFormContainer; use wcf\system\form\builder\field\BooleanFormField; use wcf\system\form\builder\field\ClassNameFormField; -use wcf\system\form\builder\field\data\CustomFormFieldDataProcessor; +use wcf\system\form\builder\field\data\processor\CustomFormFieldDataProcessor; use wcf\system\form\builder\field\dependency\ValueFormFieldDependency; use wcf\system\form\builder\field\MultilineTextFormField; use wcf\system\form\builder\field\SingleSelectionFormField;