diff --git a/src/Model/Image.php b/src/Model/Image.php index b71a3e3..e36a05b 100644 --- a/src/Model/Image.php +++ b/src/Model/Image.php @@ -6,6 +6,7 @@ use Elgentos\Imgproxy\Service\Curl; use Exception; +use Imgproxy\OptionSet; use Imgproxy\UrlBuilder; use Magento\Store\Model\Store; use Magento\Store\Model\StoreManagerInterface; @@ -78,13 +79,15 @@ public function getCustomUrl( if ($customProcessingOptions) { $options = array_map('trim', explode('/', $customProcessingOptions)); foreach ($options as $option) { - $arguments = $this->convertCustomProcessingOptionsToInt(explode(':', $option)); + $arguments = explode(':', $option); + $method = sprintf('with%s', array_shift($arguments)); - $name = array_shift($arguments); + $reflectionClass = new \ReflectionClass($url->options()); + if (! $reflectionClass->hasMethod($method)) { + continue; + } - // @todo We need to use reflection here - $method = sprintf('with%s', $name); - $url->options()->{$method}(...$arguments); + $url->options()->{$method}(...$this->castArguments($url->options(), $method, $arguments)); } } @@ -102,14 +105,18 @@ public function getCustomUrl( return $imgProxyUrl; } - private function convertCustomProcessingOptionsToInt(array $arguments): array + private function castArguments(OptionSet $class, string $methodName, array $arguments): array { - foreach ($arguments as $key => $value) { - if (is_numeric($value) && ctype_digit($value)) { - $arguments[$key] = (int)$value; + $reflectionMethod = new \ReflectionMethod($class, $methodName); + $parameters = $reflectionMethod->getParameters(); + + return array_map(function($parameter, $argument) { + $paramType = $parameter->getType(); + if ($paramType) { + $typeName = $paramType->getName(); + settype($argument, $typeName); } - } - - return $arguments; + return $argument; + }, $parameters, $arguments); } } diff --git a/src/ViewModel/Imgproxy.php b/src/ViewModel/Imgproxy.php new file mode 100644 index 0000000..4295179 --- /dev/null +++ b/src/ViewModel/Imgproxy.php @@ -0,0 +1,21 @@ +image->getCustomUrl($currentUrl, $width, $height); + } +}