1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6: */
7:
8: namespace Nette\Application;
9:
10: use Nette;
11:
12:
13: /**
14: * Presenter request.
15: *
16: * @property string $presenterName
17: * @property array $parameters
18: * @property array $post
19: * @property array $files
20: * @property string|null $method
21: */
22: class Request
23: {
24: use Nette\SmartObject;
25:
26: /** method */
27: const FORWARD = 'FORWARD';
28:
29: /** flag */
30: const SECURED = 'secured';
31:
32: /** flag */
33: const RESTORED = 'restored';
34:
35: /** @var string|null */
36: private $method;
37:
38: /** @var array */
39: private $flags = [];
40:
41: /** @var string */
42: private $name;
43:
44: /** @var array */
45: private $params;
46:
47: /** @var array */
48: private $post;
49:
50: /** @var array */
51: private $files;
52:
53:
54: /**
55: * @param string fully qualified presenter name (module:module:presenter)
56: * @param string method
57: * @param array variables provided to the presenter usually via URL
58: * @param array variables provided to the presenter via POST
59: * @param array all uploaded files
60: * @param array flags
61: */
62: public function __construct($name, $method = null, array $params = [], array $post = [], array $files = [], array $flags = [])
63: {
64: $this->name = $name;
65: $this->method = $method;
66: $this->params = $params;
67: $this->post = $post;
68: $this->files = $files;
69: $this->flags = $flags;
70: }
71:
72:
73: /**
74: * Sets the presenter name.
75: * @param string
76: * @return static
77: */
78: public function setPresenterName($name)
79: {
80: $this->name = $name;
81: return $this;
82: }
83:
84:
85: /**
86: * Retrieve the presenter name.
87: * @return string
88: */
89: public function getPresenterName()
90: {
91: return $this->name;
92: }
93:
94:
95: /**
96: * Sets variables provided to the presenter.
97: * @return static
98: */
99: public function setParameters(array $params)
100: {
101: $this->params = $params;
102: return $this;
103: }
104:
105:
106: /**
107: * Returns all variables provided to the presenter (usually via URL).
108: * @return array
109: */
110: public function getParameters()
111: {
112: return $this->params;
113: }
114:
115:
116: /**
117: * Returns a parameter provided to the presenter.
118: * @param string
119: * @return mixed
120: */
121: public function getParameter($key)
122: {
123: return isset($this->params[$key]) ? $this->params[$key] : null;
124: }
125:
126:
127: /**
128: * Sets variables provided to the presenter via POST.
129: * @return static
130: */
131: public function setPost(array $params)
132: {
133: $this->post = $params;
134: return $this;
135: }
136:
137:
138: /**
139: * Returns a variable provided to the presenter via POST.
140: * If no key is passed, returns the entire array.
141: * @param string
142: * @return mixed
143: */
144: public function getPost($key = null)
145: {
146: if (func_num_args() === 0) {
147: return $this->post;
148:
149: } elseif (isset($this->post[$key])) {
150: return $this->post[$key];
151:
152: } else {
153: return null;
154: }
155: }
156:
157:
158: /**
159: * Sets all uploaded files.
160: * @return static
161: */
162: public function setFiles(array $files)
163: {
164: $this->files = $files;
165: return $this;
166: }
167:
168:
169: /**
170: * Returns all uploaded files.
171: * @return array
172: */
173: public function getFiles()
174: {
175: return $this->files;
176: }
177:
178:
179: /**
180: * Sets the method.
181: * @param string|null
182: * @return static
183: */
184: public function setMethod($method)
185: {
186: $this->method = $method;
187: return $this;
188: }
189:
190:
191: /**
192: * Returns the method.
193: * @return string|null
194: */
195: public function getMethod()
196: {
197: return $this->method;
198: }
199:
200:
201: /**
202: * Checks if the method is the given one.
203: * @param string
204: * @return bool
205: */
206: public function isMethod($method)
207: {
208: return strcasecmp($this->method, $method) === 0;
209: }
210:
211:
212: /**
213: * Sets the flag.
214: * @param string
215: * @param bool
216: * @return static
217: */
218: public function setFlag($flag, $value = true)
219: {
220: $this->flags[$flag] = (bool) $value;
221: return $this;
222: }
223:
224:
225: /**
226: * Checks the flag.
227: * @param string
228: * @return bool
229: */
230: public function hasFlag($flag)
231: {
232: return !empty($this->flags[$flag]);
233: }
234: }
235: