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: /** flag */
36: const VARYING = 'varying';
37:
38: /** @var string|null */
39: private $method;
40:
41: /** @var array */
42: private $flags = [];
43:
44: /** @var string */
45: private $name;
46:
47: /** @var array */
48: private $params;
49:
50: /** @var array */
51: private $post;
52:
53: /** @var array */
54: private $files;
55:
56:
57: /**
58: * @param string fully qualified presenter name (module:module:presenter)
59: * @param string method
60: * @param array variables provided to the presenter usually via URL
61: * @param array variables provided to the presenter via POST
62: * @param array all uploaded files
63: * @param array flags
64: */
65: public function __construct($name, $method = null, array $params = [], array $post = [], array $files = [], array $flags = [])
66: {
67: $this->name = $name;
68: $this->method = $method;
69: $this->params = $params;
70: $this->post = $post;
71: $this->files = $files;
72: $this->flags = $flags;
73: }
74:
75:
76: /**
77: * Sets the presenter name.
78: * @param string
79: * @return static
80: */
81: public function setPresenterName($name)
82: {
83: $this->name = $name;
84: return $this;
85: }
86:
87:
88: /**
89: * Retrieve the presenter name.
90: * @return string
91: */
92: public function getPresenterName()
93: {
94: return $this->name;
95: }
96:
97:
98: /**
99: * Sets variables provided to the presenter.
100: * @return static
101: */
102: public function setParameters(array $params)
103: {
104: $this->params = $params;
105: return $this;
106: }
107:
108:
109: /**
110: * Returns all variables provided to the presenter (usually via URL).
111: * @return array
112: */
113: public function getParameters()
114: {
115: return $this->params;
116: }
117:
118:
119: /**
120: * Returns a parameter provided to the presenter.
121: * @param string
122: * @return mixed
123: */
124: public function getParameter($key)
125: {
126: return isset($this->params[$key]) ? $this->params[$key] : null;
127: }
128:
129:
130: /**
131: * Sets variables provided to the presenter via POST.
132: * @return static
133: */
134: public function setPost(array $params)
135: {
136: $this->post = $params;
137: return $this;
138: }
139:
140:
141: /**
142: * Returns a variable provided to the presenter via POST.
143: * If no key is passed, returns the entire array.
144: * @param string
145: * @return mixed
146: */
147: public function getPost($key = null)
148: {
149: if (func_num_args() === 0) {
150: return $this->post;
151:
152: } elseif (isset($this->post[$key])) {
153: return $this->post[$key];
154:
155: } else {
156: return null;
157: }
158: }
159:
160:
161: /**
162: * Sets all uploaded files.
163: * @return static
164: */
165: public function setFiles(array $files)
166: {
167: $this->files = $files;
168: return $this;
169: }
170:
171:
172: /**
173: * Returns all uploaded files.
174: * @return array
175: */
176: public function getFiles()
177: {
178: return $this->files;
179: }
180:
181:
182: /**
183: * Sets the method.
184: * @param string|null
185: * @return static
186: */
187: public function setMethod($method)
188: {
189: $this->method = $method;
190: return $this;
191: }
192:
193:
194: /**
195: * Returns the method.
196: * @return string|null
197: */
198: public function getMethod()
199: {
200: return $this->method;
201: }
202:
203:
204: /**
205: * Checks if the method is the given one.
206: * @param string
207: * @return bool
208: */
209: public function isMethod($method)
210: {
211: return strcasecmp($this->method, $method) === 0;
212: }
213:
214:
215: /**
216: * Sets the flag.
217: * @param string
218: * @param bool
219: * @return static
220: */
221: public function setFlag($flag, $value = true)
222: {
223: $this->flags[$flag] = (bool) $value;
224: return $this;
225: }
226:
227:
228: /**
229: * Checks the flag.
230: * @param string
231: * @return bool
232: */
233: public function hasFlag($flag)
234: {
235: return !empty($this->flags[$flag]);
236: }
237: }
238: