Skip to content

Commit 7cfd634

Browse files
author
jools
committed
Merge commit 'c96830680ed7dd3dbd462a06e25ce8b5c14f71fa' into HEAD
2 parents 39b9929 + c63f034 commit 7cfd634

File tree

1 file changed

+83
-18
lines changed

1 file changed

+83
-18
lines changed

manual/en-US/chapters/packages/input.md

Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
## The Input Package
22

3-
The Input package provides a number of classes that can be used as an
4-
alternative to using the static calls of the `JRequest` class. The package
5-
comprises of four classes, `JInput`and three sub-classes extended from
6-
it: `JInputCli`, `JInputCookie` and `JInputFiles`. An input object is
7-
generally owned by the application and explicitly added to an
8-
application class as a public property, such as can be found in
9-
`JApplication`, `JApplicationCli` and `JApplicationDaemon`.
3+
This package comprises of four classes, `JInput`and three sub-classes extended from it: `JInputCli`, `JInputCookie` and `JInputFiles`. It replaces the role of the now deprecated `JRequest` class. An input object is generally owned by the application and explicitly added to an application class as a public property, such as can be found in `JApplicationWeb`, `JApplicationCli` and `JApplicationDaemon`.
104

11-
All classes in this package are supported by the auto-loader so can be
12-
invoked at any time.
5+
The intent of this package is to abstract out the input source to allow code to be reused in different applications and in different contexts through dependency injection. For example, a controller could inspect the request variables directly using `JRequest`. But suppose there is a requirement to add a web service that carries input as a JSON payload. Instead of writing a second controller to handle the different input source, it would be much easier to inject an input object, that is tailored for the type of input source, into the controller.
136

14-
### Classes
7+
Using a `JInput` object through dependency injection also makes code easier to test. Writing unit tests for code that relies on `JRequest` is problematic to say the least.
158

16-
#### JInput
9+
All classes in this package are supported by the auto-loader so can be invoked at any time.
1710

18-
##### Construction
11+
### JInput
12+
13+
#### Construction
1914

2015
Unlike its predecessor `JRequest` which is used statically, the `JInput`
2116
class is meant to be used as an instantiated concrete class. Among other
@@ -41,7 +36,7 @@ $filter = JFilterInput::getInstance(/* custom settings */);
4136
$input = new JInput(null, $filter);
4237
```
4338

44-
##### Usage
39+
#### Usage
4540

4641
The most common usage of the `JInput` class will be through the get method
4742
which is roughly equivalent to the `JRequest::getVar` method. The get
@@ -134,15 +129,15 @@ if ($input->server->get('SERVER_ADDR'))
134129
$host = $input->env->get('HOSTNAME');
135130
```
136131

137-
##### Serialization
132+
#### Serialization
138133

139134
The `JInput` class implements the `Serializable` interface so that it can be
140135
safely serialized and unserialized. Note that when serializing the "ENV"
141136
and "SERVER" inputs are removed from the class as they may conflict or
142137
inappropriately overwrite settings during unserialization. This allows
143138
for `JInput` objects to be safely used with cached data.
144139

145-
#### JInputCli
140+
### JInputCli
146141

147142
The JInputCli class is extended from `JInput` but is tailored to work with
148143
command line input. Once again the get method is used to get values of
@@ -210,10 +205,80 @@ bool(false)
210205
array(1) {[0] => string(3) "bar"}
211206
```
212207

213-
#### JInputCookie
208+
### JInputCookie
214209

215210
> Can you help improve this section of the manual?
216211
217-
#### JInputFiles
212+
### JInputFiles
218213

219-
> Can you help improve this section of the manual?
214+
The `JInputFiles` class provides a way to handle file attachments as payloads of POSTed forms. Consider the following form which is assumed to handle an array of files to be attached (through some JavaScript behavior):
215+
216+
```html
217+
<form method="POST" action="/files" enctype="multipart/form-data">
218+
Attachments:
219+
<input type="file" name="attachments[]" />
220+
<button>Add another file</button>
221+
</form>
222+
```
223+
224+
Access the files from the request could be done as follows:
225+
226+
```php
227+
// By default, a new JInputFiles will inspect $_FILES.
228+
$input = new JInputFiles;
229+
$files = $input->get('attachments');
230+
231+
echo 'Inspecting $_FILES:';
232+
var_dump($_FILES);
233+
234+
echo 'Inspecting $files:';
235+
var_dump($files);
236+
```
237+
238+
```
239+
Inspecting $_FILES:
240+
241+
array
242+
'name' =>
243+
array
244+
0 => string 'aje_sig_small.png' (length=17)
245+
1 => string '' (length=0)
246+
'type' =>
247+
array
248+
0 => string 'image/png' (length=9)
249+
1 => string '' (length=0)
250+
'tmp_name' =>
251+
array
252+
0 => string '/private/var/tmp/phpPfGfnN' (length=26)
253+
1 => string '' (length=0)
254+
'error' =>
255+
array
256+
0 => int 0
257+
1 => int 4
258+
'size' =>
259+
array
260+
0 => int 16225
261+
1 => int 0
262+
263+
Inspectiong $files:
264+
265+
array
266+
0 =>
267+
array
268+
'name' => string 'sig_small.png' (length=17)
269+
'type' => string 'image/png' (length=9)
270+
'tmp_name' => string '/private/var/tmp/phpybKghO' (length=26)
271+
'error' => int 0
272+
'size' => int 16225
273+
1 =>
274+
array
275+
'name' => string '' (length=0)
276+
'type' => string '' (length=0)
277+
'tmp_name' => string '' (length=0)
278+
'error' => int 4
279+
'size' => int 0
280+
```
281+
282+
Unlike the PHP `$_FILES` supergobal, this array is very easier to parse. The example above assumes two files were submitted, but only one was specified. The 'blank' file contains an error code (see [PHP file upload errors](http://php.net/manual/en/features.file-upload.errors.php)).
283+
284+
The `set` method is disabled in `JInputFiles`. However,

0 commit comments

Comments
 (0)