|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ParamConverter; |
| 4 | + |
| 5 | +use Cake\Http\ServerRequest; |
| 6 | +use ReflectionMethod; |
| 7 | + |
| 8 | +class ParamConverterManager |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var \ParamConverter\ParamConverterInterface[] |
| 12 | + */ |
| 13 | + private $converters; |
| 14 | + |
| 15 | + /** |
| 16 | + * ParamConverterManager constructor. |
| 17 | + * @param \ParamConverter\ParamConverterInterface[] $paramConverters List of converters |
| 18 | + */ |
| 19 | + public function __construct(array $paramConverters) |
| 20 | + { |
| 21 | + foreach ($paramConverters as $paramConverter) { |
| 22 | + $this->add($paramConverter); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Add the specified converter |
| 28 | + * |
| 29 | + * @param \ParamConverter\ParamConverterInterface $paramConverter Param Converter to be add |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function add(ParamConverterInterface $paramConverter): void |
| 33 | + { |
| 34 | + $this->converters[] = $paramConverter; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns all the registered param converters |
| 39 | + * |
| 40 | + * @return \ParamConverter\ParamConverterInterface[] |
| 41 | + */ |
| 42 | + public function all(): array |
| 43 | + { |
| 44 | + return $this->converters; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Applies all the registered converters to the specified request |
| 49 | + * |
| 50 | + * @param \Cake\Http\ServerRequest $request Request to be updated (replace params with objects) |
| 51 | + * @param string $controller Controller name |
| 52 | + * @param string $action action name |
| 53 | + * @return \Cake\Http\ServerRequest |
| 54 | + * @throws \ReflectionException |
| 55 | + */ |
| 56 | + public function apply(ServerRequest $request, string $controller, string $action): ServerRequest |
| 57 | + { |
| 58 | + $method = new ReflectionMethod($controller, $action); |
| 59 | + $methodParams = $method->getParameters(); |
| 60 | + $requestParams = $request->getParam('pass'); |
| 61 | + |
| 62 | + $stopAt = min(count($methodParams), count($requestParams)); |
| 63 | + for ($i = 0; $i < $stopAt; $i++) { |
| 64 | + $methodParam = $methodParams[$i]; |
| 65 | + $requestParam = $requestParams[$i]; |
| 66 | + |
| 67 | + $methodParamClass = $methodParam->getClass(); |
| 68 | + if (!empty($methodParamClass)) { |
| 69 | + $requestParams[$i] = $this->convertParam($requestParam, $methodParamClass->getName()); |
| 70 | + } |
| 71 | + |
| 72 | + $methodParamType = $methodParam->getType(); |
| 73 | + if (!empty($methodParamType) && $methodParamType->isBuiltin()) { |
| 74 | + settype($requestParam, $methodParamType->getName()); |
| 75 | + $requestParams[$i] = $requestParam; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return $request->withParam('pass', $requestParams); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Converts the specified string value to the specified class |
| 84 | + * |
| 85 | + * @param string $value Raw value to be converted to a class |
| 86 | + * @param string $class Target class |
| 87 | + * @return mixed |
| 88 | + */ |
| 89 | + private function convertParam(string $value, string $class) |
| 90 | + { |
| 91 | + foreach ($this->all() as $converter) { |
| 92 | + if ($converter->supports($class)) { |
| 93 | + return $converter->convertTo($value, $class); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments