From e1d7914dff7941444d9f9a0f44a09b394d081c5a Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Thu, 25 Jul 2019 18:39:09 +0200 Subject: [PATCH] Improve type of IFormDataProcessor::processObjectData() parameter See #2988 --- .../form/builder/data/FormDataHandler.class.php | 3 +-- .../processor/AbstractFormDataProcessor.class.php | 3 ++- .../processor/CustomFormDataProcessor.class.php | 14 +++++++++++--- .../data/processor/IFormDataProcessor.class.php | 5 +++-- 4 files changed, 17 insertions(+), 8 deletions(-) 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 5fd0a23d81..32962e2e1c 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 @@ -57,9 +57,8 @@ class FormDataHandler implements IFormDataHandler { */ public function getObjectData(IFormDocument $document, IStorableObject $object) { $data = $object->getData(); - $objectId = $object->{$object::getDatabaseTableIndexName()}; foreach ($this->processors as $processor) { - $data = $processor->processObjectData($document, $data, $objectId); + $data = $processor->processObjectData($document, $data, $object); if (!is_array($data)) { if ($processor instanceof CustomFormDataProcessor) { diff --git a/wcfsetup/install/files/lib/system/form/builder/data/processor/AbstractFormDataProcessor.class.php b/wcfsetup/install/files/lib/system/form/builder/data/processor/AbstractFormDataProcessor.class.php index 6e24e879b3..7309408cf9 100644 --- a/wcfsetup/install/files/lib/system/form/builder/data/processor/AbstractFormDataProcessor.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/data/processor/AbstractFormDataProcessor.class.php @@ -1,5 +1,6 @@ getName() . "'") . " parameter is expected." ); } + if (!$parameters[1]->isArray()) { throw new \InvalidArgumentException("The form data processor function's second parameter must be an array."); } @@ -93,10 +95,16 @@ class CustomFormDataProcessor extends AbstractFormDataProcessor { ($parameterClass === null ? 'any' : "'" . $parameterClass->getName() . "'") . " parameter is expected." ); } + if (!$parameters[1]->isArray()) { throw new \InvalidArgumentException("The object data processor function's second parameter must be an array."); } + $parameterClass = $parameters[2]->getClass(); + if ($parameterClass === null || $parameterClass->getName() !== IStorableObject::class) { + throw new \InvalidArgumentException("The object data processor function's third parameter must be an instance of '" . IStorableObject::class . "', instead " . ($parameterClass === null ? 'any' : "'" . $parameterClass->getName() . "'") . " parameter is expected."); + } + $this->objectDataProcessor = $objectDataProcessor; } } @@ -130,12 +138,12 @@ class CustomFormDataProcessor extends AbstractFormDataProcessor { /** * @inheritDoc */ - public function processObjectData(IFormDocument $document, array $data, $objectId = null) { + public function processObjectData(IFormDocument $document, array $data, IStorableObject $object) { if ($this->objectDataProcessor === null) { - return parent::processObjectData($document, $data, $objectId); + return parent::processObjectData($document, $data, $object); } - $data = call_user_func($this->objectDataProcessor, $document, $data, $objectId); + $data = call_user_func($this->objectDataProcessor, $document, $data, $object); if (!is_array($data)) { throw new \UnexpectedValueException("Field data processor '{$this->id}' does not return an array."); diff --git a/wcfsetup/install/files/lib/system/form/builder/data/processor/IFormDataProcessor.class.php b/wcfsetup/install/files/lib/system/form/builder/data/processor/IFormDataProcessor.class.php index 21964b2e38..ab75c768e3 100644 --- a/wcfsetup/install/files/lib/system/form/builder/data/processor/IFormDataProcessor.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/data/processor/IFormDataProcessor.class.php @@ -1,5 +1,6 @@