private $parameters = array();
/**
- * Set named parameters to target method
- * @example [ [0] => [ ["link_id"] => "12312" ] ]
- * @var bool
- */
- private $parametersByName;
-
- /**
* @var null|string
*/
private $action;
$this->target = isset($config['target']) ? $config['target'] : null;
$this->name = isset($config['name']) ? $config['name'] : null;
$this->parameters = isset($config['parameters']) ? $config['parameters'] : array();
+ $this->filters = isset($config['filters']) ? $config['filters'] : array();
$action = explode('::', $this->config['_controller']);
$this->class = isset($action[0]) ? $action[0] : null;
$this->action = isset($action[1]) ? $action[1] : null;
$this->name = (string)$name;
}
- public function setFilters(array $filters, $parametersByName = false)
+ public function setFilters(array $filters)
{
- $this->filters = $filters;
- $this->parametersByName = $parametersByName;
+ $this->filters = $filters;
+ }
+
+ public function getFilters()
+ {
+ return $this->filters;
}
public function getRegex()
$class = $this->class;
$method = $this->action;
- if ( !class_exists($class)) {
+ if (!class_exists($class)) {
return null;
}
return $this->config['_controller'];
}
+ /**
+ * sort parameters according the the method's arguments
+ *
+ * @return array
+ *
+ * @throws \ReflectionException
+ */
+ private function sortParameters()
+ {
+ $class = $this->class;
+ $method = $this->action;
+ $parameters = $this->parameters;
+ $arguments = array();
+
+ if (empty($method) || trim($method) === '') {
+ $method = "__invoke";
+ }
+
+ $rexl = new \ReflectionMethod($class, $method);
+
+ foreach ($rexl->getParameters() as $methArgs) {
+ $arg = $methArgs->getName();
+
+ if (array_key_exists($arg, $parameters)) {
+ $arguments[$arg] = $parameters[$arg];
+
+ unset($parameters[$arg]);
+ } else {
+ // argument is not in the parameters
+ $arguments[$arg] = null;
+ }
+ }
+
+ if (count($parameters) > 0) {
+ // fill the unset arguments
+ foreach ($arguments as $arg => &$v) {
+ if ($v === null) {
+ //$key = array_keys($parameters)[0];
+
+ $v = array_shift($parameters);
+ }
+
+ if (count($parameters) <= 0) {
+ break;
+ }
+ }
+ }
+
+ // merge the remaining parameters
+ return array_merge($arguments, $parameters);
+ }
+
public function dispatch($instance = null)
{
is_null($instance) and $instance = new $this->class();
- // todo figure out what parametersByName is for
- $param = $this->parametersByName ?
- array($this->parameters) :
- $this->parameters;
+ $param = $this->sortParameters();
ob_start();