From b47de012a2b644dec79bef3b0fdbc1215035e933 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joshua=20R=C3=BCsweg?= Date: Thu, 7 Feb 2019 11:32:02 +0100 Subject: [PATCH] Simplify loadValueFromObject method See #2825 --- .../data/reaction/type/ReactionType.class.php | 4 +- .../builder/field/UploadFormField.class.php | 84 ++++--------------- 2 files changed, 18 insertions(+), 70 deletions(-) diff --git a/wcfsetup/install/files/lib/data/reaction/type/ReactionType.class.php b/wcfsetup/install/files/lib/data/reaction/type/ReactionType.class.php index c3e6e9353e..3c0b977206 100644 --- a/wcfsetup/install/files/lib/data/reaction/type/ReactionType.class.php +++ b/wcfsetup/install/files/lib/data/reaction/type/ReactionType.class.php @@ -101,9 +101,9 @@ class ReactionType extends DatabaseObject implements ITitledObject { /** * Returns the absolute location of the icon file. * - * @return string + * @return string[] */ public function getIconFileUploadFileLocations() { - return WCF_DIR . 'images/reaction/'. $this->iconFile; + return [WCF_DIR . 'images/reaction/'. $this->iconFile]; } } diff --git a/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php index c2e26dd159..bda70b588b 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php @@ -160,81 +160,29 @@ class UploadFormField extends AbstractFormField { // first check, whether an getter for the field exists if (method_exists($object, 'get'. ucfirst($this->getObjectProperty()) . 'UploadFileLocations')) { $value = call_user_func([$object, 'get'. ucfirst($this->getObjectProperty()) . 'UploadFileLocations']); - - if (is_array($value)) { - foreach ($value as &$v) { - if (is_string($v) && file_exists($v)) { - $v = new UploadFile($v, basename($v), UploadHandler::isValidImage($v, basename($v), $this->svgImageAllowed()), true, $this->svgImageAllowed()); - } - else { - throw new \InvalidArgumentException("The method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."UploadFileLocations()' provides invalid data."); - } - } - - $this->value($value); - } - else { - if (is_string($value) && file_exists($value)) { - $value = new UploadFile($value, basename($value), UploadHandler::isValidImage($value, basename($value), $this->svgImageAllowed()), true, $this->svgImageAllowed()); - } - else { - throw new \InvalidArgumentException("The method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."UploadFileLocations()' provides invalid data."); - } - - $this->value([$value]); - } + $method = "method '" . get_class($object) . "::get" . ucfirst($this->getObjectProperty()) . "UploadFileLocations()'"; } else if (method_exists($object, 'get'. ucfirst($this->getObjectProperty()))) { $value = call_user_func([$object, 'get'. ucfirst($this->getObjectProperty())]); - - if (is_array($value)) { - foreach ($value as &$v) { - if (is_string($v) && file_exists($v)) { - $v = new UploadFile($v, basename($v), UploadHandler::isValidImage($v, basename($v), $this->svgImageAllowed()), true, $this->svgImageAllowed()); - } - else { - throw new \InvalidArgumentException("The method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."()' provides invalid data. To load values for this object, implement the method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."UploadFileLocations()'."); - } - } - - $this->value($value); - } - else { - if (is_string($value) && file_exists($value)) { - $value = new UploadFile($value, basename($value), UploadHandler::isValidImage($value, basename($value), $this->svgImageAllowed()), true, $this->svgImageAllowed()); - } - else { - throw new \InvalidArgumentException("The method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."()' provides invalid data. To load values for this object, implement the method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."UploadFileLocations()'."); - } - - $this->value([$value]); - } + $method = "method '" . get_class($object) . "::get" . ucfirst($this->getObjectProperty()) . "()'"; } else { $value = $object->{$this->getObjectProperty()}; - - if (is_array($value)) { - foreach ($value as &$v) { - if (is_string($v) && file_exists($v)) { - $v = new UploadFile($v, basename($v), UploadHandler::isValidImage($v, basename($v), $this->svgImageAllowed()), true, $this->svgImageAllowed()); - } - else { - throw new \InvalidArgumentException("The property '". get_class($object) ."::$". $this->getObjectProperty() ."' contains invalid data. To load values for this object, implement the method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."UploadFileLocations()'."); - } - } - - $this->value($value); - } - else { - if (is_string($value) && file_exists($value)) { - $value = new UploadFile($value, basename($value), UploadHandler::isValidImage($value, basename($value), $this->svgImageAllowed()), true, $this->svgImageAllowed()); - } - else { - throw new \InvalidArgumentException("The property '". get_class($object) ."::$". $this->getObjectProperty() ."' contains invalid data. To load values for this object, implement the method '". get_class($object) ."::get". ucfirst($this->getObjectProperty()) ."UploadFileLocations()'."); + $method = "variable '" . get_class($object) . "::$" . $this->getObjectProperty() . "'"; + } + + if (is_array($value)) { + $value = array_map(function ($v) use ($method) { + if (!is_string($v) || !file_exists($v)) { + throw new \InvalidArgumentException("The " . $method . " must return an array of strings with the file locations."); } - - $this->value([$value]); - } + return new UploadFile($v, basename($v), UploadHandler::isValidImage($v, basename($v), $this->svgImageAllowed()), true, $this->svgImageAllowed()); + }, $value); + + $this->value($value); + } + else { + throw new \InvalidArgumentException("The " . $method . " must return an array of strings with the file locations."); } return $this; -- 2.20.1