ConsoleRequestDataHolder::mergeFiles()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Agavi\Request;
3
4
// +---------------------------------------------------------------------------+
5
// | This file is part of the Agavi package.                                   |
6
// | Copyright (c) 2005-2011 the Agavi Project.                                |
7
// |                                                                           |
8
// | For the full copyright and license information, please view the LICENSE   |
9
// | file that was distributed with this source code. You can also view the    |
10
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
11
// |   vi: set noexpandtab:                                                    |
12
// |   Local Variables:                                                        |
13
// |   indent-tabs-mode: t                                                     |
14
// |   End:                                                                    |
15
// +---------------------------------------------------------------------------+
16
use Agavi\Util\ArrayPathDefinition;
17
18
/**
19
 * AgaviConsoleRequestDataHolder provides methods for retrieving client request
20
 * information parameters.
21
 *
22
 * @package    agavi
23
 * @subpackage request
24
 *
25
 * @author     David Zülke <[email protected]>
26
 * @copyright  Authors
27
 * @copyright  The Agavi Project
28
 *
29
 * @since      1.0.0
30
 *
31
 * @version    $Id$
32
 */
33
class ConsoleRequestDataHolder extends RequestDataHolder implements FilesRequestDataHolderInterface
34
{
35
    /**
36
     * @constant   Constant for source name of files.
37
     */
38
    const SOURCE_FILES = 'files';
39
    
40
    /**
41
     * @var        array An array of files uploaded during the request.
42
     */
43
    protected $files = array();
44
45
    /**
46
     * Retrieve an array of file information.
47
     *
48
     * @param      string $name A file name.
49
     * @param      mixed  $default A default return value.
50
     *
51
     * @return     mixed An AgaviUploadedFile object with file information, or an
52
     *                   array if the field name has child elements, or null (or
53
     *                   the supplied default return value) no such file exists.
54
     *
55
     * @author     David Zülke <[email protected]>
56
     * @since      1.0.0
57
     */
58 View Code Duplication
    public function &getFile($name, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        if ((isset($this->files[$name]) || array_key_exists($name, $this->files))) {
61
            $retval =& $this->files[$name];
62
        } else {
63
            try {
64
                $retval =& ArrayPathDefinition::getValue($name, $this->files);
65
            } catch (\InvalidArgumentException $e) {
66
                $retval = $default;
67
            }
68
        }
69
        if (is_array($retval) || $retval instanceof UploadedFile) {
70
            return $retval;
71
        }
72
        return $default;
73
    }
74
75
    /**
76
     * Retrieve an array of files.
77
     *
78
     * @return     array An associative array of files.
79
     *
80
     * @author     David Zülke <[email protected]>
81
     * @since      1.0.0
82
     */
83
    public function &getFiles()
84
    {
85
        return $this->files;
86
    }
87
88
    /**
89
     * Indicates whether or not a file exists.
90
     *
91
     * @param      string $name A file name.
92
     *
93
     * @return     bool true, if the file exists, otherwise false.
94
     *
95
     * @author     David Zülke <[email protected]>
96
     * @since      1.0.0
97
     */
98 View Code Duplication
    public function hasFile($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        if ((isset($this->files[$name]) || array_key_exists($name, $this->files))) {
101
            $val = $this->files[$name];
102
        } else {
103
            try {
104
                $val = ArrayPathDefinition::getValue($name, $this->files);
105
            } catch (\InvalidArgumentException $e) {
106
                return false;
107
            }
108
        }
109
        return (is_array($val) || $val instanceof UploadedFile);
110
    }
111
112
    /**
113
     * Indicates whether or not any files exist.
114
     *
115
     * @return     bool true, if any files exist, otherwise false.
116
     *
117
     * @author     David Zülke <[email protected]>
118
     * @since      1.0.0
119
     */
120
    public function hasFiles()
121
    {
122
        return count($this->files) > 0;
123
    }
124
125
    /**
126
     * Checks if a file is empty, i.e. not set or set, but not actually uploaded.
127
     *
128
     * @param      string $name The file name.
129
     *
130
     * @return     bool The result.
131
     *
132
     * @author     David Zülke <[email protected]>
133
     * @since      1.0.0
134
     */
135 View Code Duplication
    public function isFileValueEmpty($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        $file = $this->getFile($name);
138
        if (!($file instanceof UploadedFile)) {
139
            return true;
140
        }
141
        return ($file->getError() == UPLOAD_ERR_NO_FILE);
142
    }
143
144
    /**
145
     * Removes file information for given file.
146
     *
147
     * @param      string $name A file name
148
     *
149
     * @return     mixed The old AgaviUploadedFile instance or array of elements.
150
     *
151
     * @author     David Zülke <[email protected]>
152
     * @since      1.0.0
153
     */
154 View Code Duplication
    public function &removeFile($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156
        if (isset($this->files[$name]) || array_key_exists($name, $this->files)) {
157
            $retval =& $this->files[$name];
158
            unset($this->files[$name]);
159
            return $retval;
160
        }
161
        try {
162
            return ArrayPathDefinition::unsetValue($name, $this->files);
163
        } catch (\InvalidArgumentException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
164
        }
165
    }
166
167
    /**
168
     * Set a file.
169
     *
170
     * If a file with the name already exists the value will be overridden.
171
     *
172
     * @param      string       $name A file name.
173
     * @param      UploadedFile $file An UploadedFile object.
174
     *
175
     * @author     David Zülke <[email protected]>
176
     * @since      1.0.0
177
     */
178
    public function setFile($name, UploadedFile $file)
179
    {
180
        $this->files[$name] = $file;
181
    }
182
183
    /**
184
     * Set an array of files.
185
     *
186
     * @param      array $files An assoc array of names and UploadedFile objects.
187
     *
188
     * @author     David Zülke <[email protected]>
189
     * @since      1.0.0
190
     */
191
    public function setFiles(array $files)
192
    {
193
        $this->files = array_merge($this->files, $files);
194
    }
195
196
    /**
197
     * Clear all files.
198
     *
199
     * @author     David Zülke <[email protected]>
200
     * @since      1.0.0
201
     */
202
    public function clearFiles()
203
    {
204
        $this->files = array();
205
    }
206
207
    /**
208
     * Retrieve an array of file names.
209
     *
210
     * @return     array An indexed array of file names.
211
     *
212
     * @author     David Zülke <[email protected]>
213
     * @since      1.0.0
214
     */
215
    public function getFileNames()
216
    {
217
        return array_keys($this->files);
218
    }
219
    
220
    /**
221
     * Retrieve an array of flattened file names. This means when a file is an
222
     * array you wont get the name of the file in the result but instead all child
223
     * keys appended to the name (like foo[0],foo[1][0], ...).
224
     *
225
     * @return     array An indexed array of file names.
226
     *
227
     * @author     David Zülke <[email protected]>
228
     * @since      1.0.0
229
     */
230
    public function getFlatFileNames()
231
    {
232
        return ArrayPathDefinition::getFlatKeyNames($this->files);
233
    }
234
    
235
    /**
236
     * Constructor
237
     *
238
     * @param      array $data An associative array of request data source names and
239
     *                   data arrays.
240
     *
241
     * @author     David Zülke <[email protected]>
242
     * @since      1.0.0
243
     */
244
    public function __construct(array $data = array())
245
    {
246
        $this->registerSource(self::SOURCE_FILES, $this->files);
247
        
248
        // call the parent ctor which handles the actual loading of the data
249
        parent::__construct($data);
250
    }
251
    
252
    /**
253
     * Merge in Files from another request data holder.
254
     *
255
     * @param      RequestDataHolder $other The other request data holder.
256
     *
257
     * @author     David Zülke <[email protected]>
258
     * @since      1.0.0
259
     */
260
    public function mergeFiles(RequestDataHolder $other)
261
    {
262
        if ($other instanceof FilesRequestDataHolderInterface) {
263
            $this->setFiles($other->getFiles());
264
        }
265
    }
266
}
267