Simplify loadValueFromObject method
authorJoshua Rüsweg <josh@bastelstu.be>
Thu, 7 Feb 2019 10:32:02 +0000 (11:32 +0100)
committerJoshua Rüsweg <josh@bastelstu.be>
Thu, 7 Feb 2019 10:32:02 +0000 (11:32 +0100)
See #2825

wcfsetup/install/files/lib/data/reaction/type/ReactionType.class.php
wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php

index c3e6e9353e7542f04bd6e98032fd4a541697d979..3c0b977206cd10ca4bffaa8187b79234b123fc91 100644 (file)
@@ -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];
        }
 }
index c2e26dd159db694bbff6de6da982620bd5c4e61b..bda70b588b41e7f2fdaf0af588b8d4ce55fd171f 100644 (file)
@@ -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;