1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link      http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license   http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10namespace Zend\Mvc\Controller\Plugin;
11
12use Zend\Mvc\Exception\RuntimeException;
13use Zend\Mvc\InjectApplicationEventInterface;
14
15class Params extends AbstractPlugin
16{
17    /**
18     * Grabs a param from route match by default.
19     *
20     * @param string $param
21     * @param mixed $default
22     * @return mixed
23     */
24    public function __invoke($param = null, $default = null)
25    {
26        if ($param === null) {
27            return $this;
28        }
29        return $this->fromRoute($param, $default);
30    }
31
32    /**
33     * Return all files or a single file.
34     *
35     * @param  string $name File name to retrieve, or null to get all.
36     * @param  mixed $default Default value to use when the file is missing.
37     * @return array|\ArrayAccess|null
38     */
39    public function fromFiles($name = null, $default = null)
40    {
41        if ($name === null) {
42            return $this->getController()->getRequest()->getFiles($name, $default)->toArray();
43        }
44
45        return $this->getController()->getRequest()->getFiles($name, $default);
46    }
47
48    /**
49     * Return all header parameters or a single header parameter.
50     *
51     * @param  string $header Header name to retrieve, or null to get all.
52     * @param  mixed $default Default value to use when the requested header is missing.
53     * @return null|\Zend\Http\Header\HeaderInterface
54     */
55    public function fromHeader($header = null, $default = null)
56    {
57        if ($header === null) {
58            return $this->getController()->getRequest()->getHeaders($header, $default)->toArray();
59        }
60
61        return $this->getController()->getRequest()->getHeaders($header, $default);
62    }
63
64    /**
65     * Return all post parameters or a single post parameter.
66     *
67     * @param string $param Parameter name to retrieve, or null to get all.
68     * @param mixed $default Default value to use when the parameter is missing.
69     * @return mixed
70     */
71    public function fromPost($param = null, $default = null)
72    {
73        if ($param === null) {
74            return $this->getController()->getRequest()->getPost($param, $default)->toArray();
75        }
76
77        return $this->getController()->getRequest()->getPost($param, $default);
78    }
79
80    /**
81     * Return all query parameters or a single query parameter.
82     *
83     * @param string $param Parameter name to retrieve, or null to get all.
84     * @param mixed $default Default value to use when the parameter is missing.
85     * @return mixed
86     */
87    public function fromQuery($param = null, $default = null)
88    {
89        if ($param === null) {
90            return $this->getController()->getRequest()->getQuery($param, $default)->toArray();
91        }
92
93        return $this->getController()->getRequest()->getQuery($param, $default);
94    }
95
96    /**
97     * Return all route parameters or a single route parameter.
98     *
99     * @param string $param Parameter name to retrieve, or null to get all.
100     * @param mixed $default Default value to use when the parameter is missing.
101     * @return mixed
102     * @throws RuntimeException
103     */
104    public function fromRoute($param = null, $default = null)
105    {
106        $controller = $this->getController();
107
108        if (!$controller instanceof InjectApplicationEventInterface) {
109            throw new RuntimeException(
110                'Controllers must implement Zend\Mvc\InjectApplicationEventInterface to use this plugin.'
111            );
112        }
113
114        if ($param === null) {
115            return $controller->getEvent()->getRouteMatch()->getParams();
116        }
117
118        return $controller->getEvent()->getRouteMatch()->getParam($param, $default);
119    }
120}
121