Completed
Pull Request — master (#999)
by
unknown
13:00 queued 04:03
created
core/services/request/files/FileSubmission.php 2 patches
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -71,7 +71,7 @@  discard block
 block discarded – undo
71 71
      */
72 72
     public function getType()
73 73
     {
74
-        if (!$this->type) {
74
+        if ( ! $this->type) {
75 75
             $this->type = $this->determineType();
76 76
         }
77 77
         return $this->type;
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
      */
84 84
     protected function determineType()
85 85
     {
86
-        if (!$this->getTmpFile()) {
86
+        if ( ! $this->getTmpFile()) {
87 87
             return '';
88 88
         }
89 89
         $finfo = new finfo(FILEINFO_MIME_TYPE);
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
      */
98 98
     public function getExtension()
99 99
     {
100
-        if (!$this->extension) {
100
+        if ( ! $this->extension) {
101 101
             $this->extension = $this->determineExtension();
102 102
         }
103 103
         return $this->extension;
@@ -110,7 +110,7 @@  discard block
 block discarded – undo
110 110
      */
111 111
     protected function determineExtension()
112 112
     {
113
-        if (!$this->getTmpFile()) {
113
+        if ( ! $this->getTmpFile()) {
114 114
             return '';
115 115
         }
116 116
         return pathinfo($this->getTmpFile(), PATHINFO_EXTENSION);
Please login to merge, or discard this patch.
Indentation   +154 added lines, -154 removed lines patch added patch discarded remove patch
@@ -19,160 +19,160 @@
 block discarded – undo
19 19
  */
20 20
 class FileSubmission implements FileSubmissionInterface
21 21
 {
22
-    /**
23
-     * @var string original name on the client machine
24
-     */
25
-    protected $name;
26
-
27
-    /**
28
-     * @var string mime type
29
-     */
30
-    protected $type;
31
-
32
-    /**
33
-     * @var string file extension
34
-     */
35
-    protected $extension;
36
-
37
-    /**
38
-     * @var int in bytes
39
-     */
40
-    protected $size;
41
-
42
-    /**
43
-     * @var string local filepath to the temporary file
44
-     */
45
-    protected $tmp_file;
46
-
47
-    /**
48
-     * @var int one of UPLOAD_ERR_OK, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE or other values
49
-     * although those aren't expected.
50
-     */
51
-    protected $error_code;
52
-
53
-    /**
54
-     * FileSubmission constructor.
55
-     * @param $name
56
-     * @param $tmp_file
57
-     * @param $size
58
-     * @param null $error_code
59
-     * @throws InvalidArgumentException
60
-     */
61
-    public function __construct($name, $tmp_file, $size, $error_code = null)
62
-    {
63
-        $this->name = basename($name);
64
-        $scheme = parse_url($tmp_file, PHP_URL_SCHEME);
65
-        if (in_array($scheme, ['http', 'https'])) {
66
-            // Wait a minute- just local filepaths please, no URL schemes allowed!
67
-            throw new InvalidArgumentException(
68
-                sprintf(
69
-                    // @codingStandardsIgnoreStart
70
-                    esc_html__('The scheme ("%1$s") on the temporary file ("%2$s") indicates is located elsewhere, that’s not ok!', 'event_espresso'),
71
-                    // @codingStandardsIgnoreEnd
72
-                    $scheme,
73
-                    $tmp_file
74
-                )
75
-            );
76
-        }
77
-        $this->tmp_file = (string) $tmp_file;
78
-        $this->size = (int) $size;
79
-        $this->error_code = (int) $error_code;
80
-    }
81
-
82
-    /**
83
-     * @return string
84
-     */
85
-    public function getName()
86
-    {
87
-        return $this->name;
88
-    }
89
-
90
-    /**
91
-     * Gets the file's mime type
92
-     * @return string
93
-     */
94
-    public function getType()
95
-    {
96
-        if (!$this->type) {
97
-            $this->type = $this->determineType();
98
-        }
99
-        return $this->type;
100
-    }
101
-
102
-    /**
103
-     * @since $VID:$
104
-     * @return string
105
-     */
106
-    protected function determineType()
107
-    {
108
-        if (!$this->getTmpFile()) {
109
-            return '';
110
-        }
111
-        $finfo = new finfo(FILEINFO_MIME_TYPE);
112
-        return $finfo->file($this->getTmpFile());
113
-    }
114
-
115
-    /**
116
-     * Gets the file's extension.
117
-     * @since $VID:$
118
-     * @return string
119
-     */
120
-    public function getExtension()
121
-    {
122
-        if (!$this->extension) {
123
-            $this->extension = $this->determineExtension();
124
-        }
125
-        return $this->extension;
126
-    }
127
-
128
-    /**
129
-     * Determine's the file's extension given the temporary file.
130
-     * @since $VID:$
131
-     * @return string
132
-     */
133
-    protected function determineExtension()
134
-    {
135
-        if (!$this->getTmpFile()) {
136
-            return '';
137
-        }
138
-        return pathinfo($this->getTmpFile(), PATHINFO_EXTENSION);
139
-    }
140
-
141
-    /**
142
-     * Gets the size of the file
143
-     * @return int
144
-     */
145
-    public function getSize()
146
-    {
147
-        return $this->size;
148
-    }
149
-
150
-    /**
151
-     * Gets the path to the temporary file which was uploaded.
152
-     * @return string
153
-     */
154
-    public function getTmpFile()
155
-    {
156
-        return $this->tmp_file;
157
-    }
158
-
159
-    /**
160
-     * @since $VID:$
161
-     * @return string
162
-     */
163
-    public function __toString()
164
-    {
165
-        return $this->getTmpFile();
166
-    }
167
-
168
-    /**
169
-     * Gets the error code PHP reported for the file upload.
170
-     * @return string
171
-     */
172
-    public function getErrorCode()
173
-    {
174
-        return $this->error_code;
175
-    }
22
+	/**
23
+	 * @var string original name on the client machine
24
+	 */
25
+	protected $name;
26
+
27
+	/**
28
+	 * @var string mime type
29
+	 */
30
+	protected $type;
31
+
32
+	/**
33
+	 * @var string file extension
34
+	 */
35
+	protected $extension;
36
+
37
+	/**
38
+	 * @var int in bytes
39
+	 */
40
+	protected $size;
41
+
42
+	/**
43
+	 * @var string local filepath to the temporary file
44
+	 */
45
+	protected $tmp_file;
46
+
47
+	/**
48
+	 * @var int one of UPLOAD_ERR_OK, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE or other values
49
+	 * although those aren't expected.
50
+	 */
51
+	protected $error_code;
52
+
53
+	/**
54
+	 * FileSubmission constructor.
55
+	 * @param $name
56
+	 * @param $tmp_file
57
+	 * @param $size
58
+	 * @param null $error_code
59
+	 * @throws InvalidArgumentException
60
+	 */
61
+	public function __construct($name, $tmp_file, $size, $error_code = null)
62
+	{
63
+		$this->name = basename($name);
64
+		$scheme = parse_url($tmp_file, PHP_URL_SCHEME);
65
+		if (in_array($scheme, ['http', 'https'])) {
66
+			// Wait a minute- just local filepaths please, no URL schemes allowed!
67
+			throw new InvalidArgumentException(
68
+				sprintf(
69
+					// @codingStandardsIgnoreStart
70
+					esc_html__('The scheme ("%1$s") on the temporary file ("%2$s") indicates is located elsewhere, that’s not ok!', 'event_espresso'),
71
+					// @codingStandardsIgnoreEnd
72
+					$scheme,
73
+					$tmp_file
74
+				)
75
+			);
76
+		}
77
+		$this->tmp_file = (string) $tmp_file;
78
+		$this->size = (int) $size;
79
+		$this->error_code = (int) $error_code;
80
+	}
81
+
82
+	/**
83
+	 * @return string
84
+	 */
85
+	public function getName()
86
+	{
87
+		return $this->name;
88
+	}
89
+
90
+	/**
91
+	 * Gets the file's mime type
92
+	 * @return string
93
+	 */
94
+	public function getType()
95
+	{
96
+		if (!$this->type) {
97
+			$this->type = $this->determineType();
98
+		}
99
+		return $this->type;
100
+	}
101
+
102
+	/**
103
+	 * @since $VID:$
104
+	 * @return string
105
+	 */
106
+	protected function determineType()
107
+	{
108
+		if (!$this->getTmpFile()) {
109
+			return '';
110
+		}
111
+		$finfo = new finfo(FILEINFO_MIME_TYPE);
112
+		return $finfo->file($this->getTmpFile());
113
+	}
114
+
115
+	/**
116
+	 * Gets the file's extension.
117
+	 * @since $VID:$
118
+	 * @return string
119
+	 */
120
+	public function getExtension()
121
+	{
122
+		if (!$this->extension) {
123
+			$this->extension = $this->determineExtension();
124
+		}
125
+		return $this->extension;
126
+	}
127
+
128
+	/**
129
+	 * Determine's the file's extension given the temporary file.
130
+	 * @since $VID:$
131
+	 * @return string
132
+	 */
133
+	protected function determineExtension()
134
+	{
135
+		if (!$this->getTmpFile()) {
136
+			return '';
137
+		}
138
+		return pathinfo($this->getTmpFile(), PATHINFO_EXTENSION);
139
+	}
140
+
141
+	/**
142
+	 * Gets the size of the file
143
+	 * @return int
144
+	 */
145
+	public function getSize()
146
+	{
147
+		return $this->size;
148
+	}
149
+
150
+	/**
151
+	 * Gets the path to the temporary file which was uploaded.
152
+	 * @return string
153
+	 */
154
+	public function getTmpFile()
155
+	{
156
+		return $this->tmp_file;
157
+	}
158
+
159
+	/**
160
+	 * @since $VID:$
161
+	 * @return string
162
+	 */
163
+	public function __toString()
164
+	{
165
+		return $this->getTmpFile();
166
+	}
167
+
168
+	/**
169
+	 * Gets the error code PHP reported for the file upload.
170
+	 * @return string
171
+	 */
172
+	public function getErrorCode()
173
+	{
174
+		return $this->error_code;
175
+	}
176 176
 }
177 177
 // End of file FileSubmission.php
178 178
 // Location: EventEspresso\core\services\request\files/FileSubmission.php
Please login to merge, or discard this patch.
core/services/request/files/FileSubmissionInterface.php 1 patch
Indentation   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -15,36 +15,36 @@
 block discarded – undo
15 15
 interface FileSubmissionInterface
16 16
 {
17 17
 
18
-    /**
19
-     * @return string
20
-     */
21
-    public function getName();
22
-
23
-    /**
24
-     * @return string
25
-     */
26
-    public function getType();
27
-
28
-    /**
29
-     * @return int
30
-     */
31
-    public function getSize();
32
-
33
-    /**
34
-     * @return string
35
-     */
36
-    public function getTmpFile();
37
-
38
-    /**
39
-     * @since $VID:$
40
-     * @return string
41
-     */
42
-    public function __toString();
43
-
44
-    /**
45
-     * @return string
46
-     */
47
-    public function getErrorCode();
18
+	/**
19
+	 * @return string
20
+	 */
21
+	public function getName();
22
+
23
+	/**
24
+	 * @return string
25
+	 */
26
+	public function getType();
27
+
28
+	/**
29
+	 * @return int
30
+	 */
31
+	public function getSize();
32
+
33
+	/**
34
+	 * @return string
35
+	 */
36
+	public function getTmpFile();
37
+
38
+	/**
39
+	 * @since $VID:$
40
+	 * @return string
41
+	 */
42
+	public function __toString();
43
+
44
+	/**
45
+	 * @return string
46
+	 */
47
+	public function getErrorCode();
48 48
 }
49 49
 // End of file FileSubmissionInterface.php
50 50
 // Location: EventEspresso\core\services\request\files/FileSubmissionInterface.php
Please login to merge, or discard this patch.
core/services/request/files/FilesDataHandler.php 2 patches
Indentation   +229 added lines, -229 removed lines patch added patch discarded remove patch
@@ -39,246 +39,246 @@
 block discarded – undo
39 39
  */
40 40
 class FilesDataHandler
41 41
 {
42
-    /**
43
-     * @var Request
44
-     */
45
-    protected $request;
42
+	/**
43
+	 * @var Request
44
+	 */
45
+	protected $request;
46 46
 
47
-    /**
48
-     * @var CollectionInterface | FileSubmissionInterface[]
49
-     */
50
-    protected $file_objects;
47
+	/**
48
+	 * @var CollectionInterface | FileSubmissionInterface[]
49
+	 */
50
+	protected $file_objects;
51 51
 
52
-    /**
53
-     * @var bool
54
-     */
55
-    protected $initialized = false;
52
+	/**
53
+	 * @var bool
54
+	 */
55
+	protected $initialized = false;
56 56
 
57
-    /**
58
-     * FilesDataHandler constructor.
59
-     * @param Request $request
60
-     */
61
-    public function __construct(Request $request)
62
-    {
63
-        $this->request = $request;
64
-    }
57
+	/**
58
+	 * FilesDataHandler constructor.
59
+	 * @param Request $request
60
+	 */
61
+	public function __construct(Request $request)
62
+	{
63
+		$this->request = $request;
64
+	}
65 65
 
66
-    /**
67
-     * @since $VID:$
68
-     * @return CollectionInterface | FileSubmissionInterface[]
69
-     * @throws UnexpectedValueException
70
-     * @throws InvalidArgumentException
71
-     */
72
-    public function getFileObjects()
73
-    {
74
-        if (!$this->initialized) {
75
-            $this->initialize();
76
-        }
77
-        return $this->file_objects;
78
-    }
66
+	/**
67
+	 * @since $VID:$
68
+	 * @return CollectionInterface | FileSubmissionInterface[]
69
+	 * @throws UnexpectedValueException
70
+	 * @throws InvalidArgumentException
71
+	 */
72
+	public function getFileObjects()
73
+	{
74
+		if (!$this->initialized) {
75
+			$this->initialize();
76
+		}
77
+		return $this->file_objects;
78
+	}
79 79
 
80
-    /**
81
-     * Sets up the file objects from the request's $_FILES data.
82
-     * @since $VID:$
83
-     * @throws UnexpectedValueException
84
-     * @throws InvalidArgumentException
85
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
86
-     */
87
-    protected function initialize()
88
-    {
89
-        $files_raw_data = $this->request->filesParams();
90
-        if (empty($files_raw_data)) {
91
-            return;
92
-        }
93
-        if ($this->isStrangeFilesArray($files_raw_data)) {
94
-            $data = $this->fixFilesDataArray($files_raw_data);
95
-        } else {
96
-            $data = $files_raw_data;
97
-        }
98
-        $this->file_objects = new Collection(
99
-            // collection interface
100
-            'EventEspresso\core\services\request\files\FileSubmissionInterface',
101
-            // collection name
102
-            'submitted_files'
103
-        );
104
-        $this->createFileObjects($data);
105
-        $this->initialized = true;
106
-    }
80
+	/**
81
+	 * Sets up the file objects from the request's $_FILES data.
82
+	 * @since $VID:$
83
+	 * @throws UnexpectedValueException
84
+	 * @throws InvalidArgumentException
85
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
86
+	 */
87
+	protected function initialize()
88
+	{
89
+		$files_raw_data = $this->request->filesParams();
90
+		if (empty($files_raw_data)) {
91
+			return;
92
+		}
93
+		if ($this->isStrangeFilesArray($files_raw_data)) {
94
+			$data = $this->fixFilesDataArray($files_raw_data);
95
+		} else {
96
+			$data = $files_raw_data;
97
+		}
98
+		$this->file_objects = new Collection(
99
+			// collection interface
100
+			'EventEspresso\core\services\request\files\FileSubmissionInterface',
101
+			// collection name
102
+			'submitted_files'
103
+		);
104
+		$this->createFileObjects($data);
105
+		$this->initialized = true;
106
+	}
107 107
 
108
-    /**
109
-     * Detects if $_FILES is a weird multi-dimensional array that needs fixing or not.
110
-     * @since $VID:$
111
-     * @param $files_data
112
-     * @return bool
113
-     * @throws UnexpectedValueException
114
-     */
115
-    protected function isStrangeFilesArray($files_data)
116
-    {
117
-        if (!is_array($files_data)) {
118
-            throw new UnexpectedValueException(
119
-                sprintf(
120
-                    esc_html__(
121
-                        'Unexpected PHP $_FILES data format. "%1$s" was expected to be an array.',
122
-                        'event_espresso'
123
-                    ),
124
-                    (string) $files_data
125
-                )
126
-            );
127
-        }
128
-        $first_value = reset($files_data);
129
-        if (!is_array($first_value)) {
130
-            throw new UnexpectedValueException(
131
-                sprintf(
132
-                    esc_html__(
133
-                        'Unexpected PHP $_FILES data format. "%1$s" was expected to be an array.',
134
-                        'event_espresso'
135
-                    ),
136
-                    (string) $first_value
137
-                )
138
-            );
139
-        }
140
-        $first_sub_array_item = reset($first_value);
141
-        if (is_array($first_sub_array_item)) {
142
-            // not just a 2d array
143
-            return true;
144
-        }
145
-        // yep, just 2d array
146
-        return false;
147
-    }
108
+	/**
109
+	 * Detects if $_FILES is a weird multi-dimensional array that needs fixing or not.
110
+	 * @since $VID:$
111
+	 * @param $files_data
112
+	 * @return bool
113
+	 * @throws UnexpectedValueException
114
+	 */
115
+	protected function isStrangeFilesArray($files_data)
116
+	{
117
+		if (!is_array($files_data)) {
118
+			throw new UnexpectedValueException(
119
+				sprintf(
120
+					esc_html__(
121
+						'Unexpected PHP $_FILES data format. "%1$s" was expected to be an array.',
122
+						'event_espresso'
123
+					),
124
+					(string) $files_data
125
+				)
126
+			);
127
+		}
128
+		$first_value = reset($files_data);
129
+		if (!is_array($first_value)) {
130
+			throw new UnexpectedValueException(
131
+				sprintf(
132
+					esc_html__(
133
+						'Unexpected PHP $_FILES data format. "%1$s" was expected to be an array.',
134
+						'event_espresso'
135
+					),
136
+					(string) $first_value
137
+				)
138
+			);
139
+		}
140
+		$first_sub_array_item = reset($first_value);
141
+		if (is_array($first_sub_array_item)) {
142
+			// not just a 2d array
143
+			return true;
144
+		}
145
+		// yep, just 2d array
146
+		return false;
147
+	}
148 148
 
149
-    /**
150
-     * Takes into account that $_FILES does a weird thing when you have hierarchical form names (eg `<input type="file"
151
-     * name="my[hierarchical][form]">`): it leaves the top-level form part alone, but replaces the SECOND part with
152
-     * "name", "size", "tmp_name", etc. So that file's data is located at "my[name][hierarchical][form]",
153
-     * "my[size][hierarchical][form]", "my[tmp_name][hierarchical][form]", etc. It's really weird.
154
-     * @since $VID:$
155
-     * @param $files_data
156
-     * @return array
157
-     */
158
-    protected function fixFilesDataArray($files_data)
159
-    {
160
-        $sane_files_array = [];
161
-        foreach ($files_data as $top_key => $top_key_value) {
162
-            foreach ($top_key_value as $lower_key => $lower_key_value) {
163
-                foreach ($lower_key_value as $lowest_key => $lowest_key_value) {
164
-                    $next_data = [
165
-                        $top_key => [
166
-                            $lowest_key => $this->organizeFilesData($lowest_key_value, $lower_key, $lowest_key)
167
-                        ]
168
-                    ];
169
-                    $sane_files_array = array_merge_recursive(
170
-                        $sane_files_array,
171
-                        $next_data
172
-                    );
173
-                }
174
-            }
175
-        }
176
-        return $sane_files_array;
177
-    }
149
+	/**
150
+	 * Takes into account that $_FILES does a weird thing when you have hierarchical form names (eg `<input type="file"
151
+	 * name="my[hierarchical][form]">`): it leaves the top-level form part alone, but replaces the SECOND part with
152
+	 * "name", "size", "tmp_name", etc. So that file's data is located at "my[name][hierarchical][form]",
153
+	 * "my[size][hierarchical][form]", "my[tmp_name][hierarchical][form]", etc. It's really weird.
154
+	 * @since $VID:$
155
+	 * @param $files_data
156
+	 * @return array
157
+	 */
158
+	protected function fixFilesDataArray($files_data)
159
+	{
160
+		$sane_files_array = [];
161
+		foreach ($files_data as $top_key => $top_key_value) {
162
+			foreach ($top_key_value as $lower_key => $lower_key_value) {
163
+				foreach ($lower_key_value as $lowest_key => $lowest_key_value) {
164
+					$next_data = [
165
+						$top_key => [
166
+							$lowest_key => $this->organizeFilesData($lowest_key_value, $lower_key, $lowest_key)
167
+						]
168
+					];
169
+					$sane_files_array = array_merge_recursive(
170
+						$sane_files_array,
171
+						$next_data
172
+					);
173
+				}
174
+			}
175
+		}
176
+		return $sane_files_array;
177
+	}
178 178
 
179
-    /**
180
-     * Recursively explores the array until it finds a leaf node, and tacks `$type` as a final index in front of it.
181
-     * @since $VID:$
182
-     * @param $data either 'name', 'tmp_name', 'size', or 'error'
183
-     * @param $type
184
-     * @return array
185
-     */
186
-    protected function organizeFilesData($data, $type)
187
-    {
188
-        $organized_data = [];
189
-        foreach ($data as $key => $val) {
190
-            if (is_array($val)) {
191
-                $organized_data[ $key ] = $this->organizeFilesData($val, $type);
192
-            } else {
193
-                $organized_data[ $key ][ $type ] = $val;
194
-            }
195
-        }
196
-        return $organized_data;
197
-    }
179
+	/**
180
+	 * Recursively explores the array until it finds a leaf node, and tacks `$type` as a final index in front of it.
181
+	 * @since $VID:$
182
+	 * @param $data either 'name', 'tmp_name', 'size', or 'error'
183
+	 * @param $type
184
+	 * @return array
185
+	 */
186
+	protected function organizeFilesData($data, $type)
187
+	{
188
+		$organized_data = [];
189
+		foreach ($data as $key => $val) {
190
+			if (is_array($val)) {
191
+				$organized_data[ $key ] = $this->organizeFilesData($val, $type);
192
+			} else {
193
+				$organized_data[ $key ][ $type ] = $val;
194
+			}
195
+		}
196
+		return $organized_data;
197
+	}
198 198
 
199
-    /**
200
-     * Takes the organized $_FILES array (where all file info is located at the same spot as you'd expect an input
201
-     * to be in $_GET or $_POST, with all the file's data located side-by-side in an array) and creates a
202
-     * multi-dimensional array of FileSubmissionInterface objects. Stores it in `$this->file_objects`.
203
-     * @since $VID:$
204
-     * @param array $organized_files $_FILES but organized like $_POST
205
-     * @param array $name_parts_so_far for multidimensional HTML form names,
206
-     * @throws UnexpectedValueException
207
-     * @throws InvalidArgumentException
208
-     */
209
-    protected function createFileObjects($organized_files, $name_parts_so_far = [])
210
-    {
211
-        if (!is_array($organized_files)) {
212
-            throw new UnexpectedValueException(
213
-                sprintf(
214
-                    esc_html__(
215
-                        'Unexpected PHP $organized_files data format. "%1$s" was expected to be an array.',
216
-                        'event_espresso'
217
-                    ),
218
-                    (string) $organized_files
219
-                )
220
-            );
221
-        }
222
-        foreach ($organized_files as $key => $value) {
223
-            array_push(
224
-                $name_parts_so_far,
225
-                $key
226
-            );
227
-            if (isset($value['name'], $value['tmp_name'], $value['size'])) {
228
-                $html_name = $this->inputNameFromParts($name_parts_so_far);
229
-                $this->file_objects->add(
230
-                    new FileSubmission(
231
-                        $html_name,
232
-                        $value['name'],
233
-                        $value['tmp_name'],
234
-                        $value['size'],
235
-                        $value['error']
236
-                    ),
237
-                    $html_name
238
-                );
239
-            } else {
240
-                $this->createFileObjects($value, $name_parts_so_far);
241
-            }
242
-        }
243
-    }
199
+	/**
200
+	 * Takes the organized $_FILES array (where all file info is located at the same spot as you'd expect an input
201
+	 * to be in $_GET or $_POST, with all the file's data located side-by-side in an array) and creates a
202
+	 * multi-dimensional array of FileSubmissionInterface objects. Stores it in `$this->file_objects`.
203
+	 * @since $VID:$
204
+	 * @param array $organized_files $_FILES but organized like $_POST
205
+	 * @param array $name_parts_so_far for multidimensional HTML form names,
206
+	 * @throws UnexpectedValueException
207
+	 * @throws InvalidArgumentException
208
+	 */
209
+	protected function createFileObjects($organized_files, $name_parts_so_far = [])
210
+	{
211
+		if (!is_array($organized_files)) {
212
+			throw new UnexpectedValueException(
213
+				sprintf(
214
+					esc_html__(
215
+						'Unexpected PHP $organized_files data format. "%1$s" was expected to be an array.',
216
+						'event_espresso'
217
+					),
218
+					(string) $organized_files
219
+				)
220
+			);
221
+		}
222
+		foreach ($organized_files as $key => $value) {
223
+			array_push(
224
+				$name_parts_so_far,
225
+				$key
226
+			);
227
+			if (isset($value['name'], $value['tmp_name'], $value['size'])) {
228
+				$html_name = $this->inputNameFromParts($name_parts_so_far);
229
+				$this->file_objects->add(
230
+					new FileSubmission(
231
+						$html_name,
232
+						$value['name'],
233
+						$value['tmp_name'],
234
+						$value['size'],
235
+						$value['error']
236
+					),
237
+					$html_name
238
+				);
239
+			} else {
240
+				$this->createFileObjects($value, $name_parts_so_far);
241
+			}
242
+		}
243
+	}
244 244
 
245
-    /**
246
-     * Takes the input name parts, like `['my', 'great', 'file', 'input1']`
247
-     * and returns the HTML name for it, "my[great][file][input1]"
248
-     * @since $VID:$
249
-     * @param $parts
250
-     * @throws UnexpectedValueException
251
-     */
252
-    protected function inputNameFromParts($parts)
253
-    {
254
-        if (!is_array($parts)) {
255
-            throw new UnexpectedValueException(esc_html__('Name parts should be an array.', 'event_espresso'));
256
-        }
257
-        $generated_string = '';
258
-        foreach ($parts as $part) {
259
-            if ($generated_string === '') {
260
-                $generated_string = (string) $part;
261
-            } else {
262
-                $generated_string .= '[' . (string) $part . ']';
263
-            }
264
-        }
265
-        return $generated_string;
266
-    }
245
+	/**
246
+	 * Takes the input name parts, like `['my', 'great', 'file', 'input1']`
247
+	 * and returns the HTML name for it, "my[great][file][input1]"
248
+	 * @since $VID:$
249
+	 * @param $parts
250
+	 * @throws UnexpectedValueException
251
+	 */
252
+	protected function inputNameFromParts($parts)
253
+	{
254
+		if (!is_array($parts)) {
255
+			throw new UnexpectedValueException(esc_html__('Name parts should be an array.', 'event_espresso'));
256
+		}
257
+		$generated_string = '';
258
+		foreach ($parts as $part) {
259
+			if ($generated_string === '') {
260
+				$generated_string = (string) $part;
261
+			} else {
262
+				$generated_string .= '[' . (string) $part . ']';
263
+			}
264
+		}
265
+		return $generated_string;
266
+	}
267 267
 
268
-    /**
269
-     * Gets the input by the indicated $name_parts.
270
-     * Eg if you're looking for an input named "my[great][file][input1]", $name_parts
271
-     * should be `['my', 'great', 'file', 'input1']`.
272
-     * Alternatively, you could use `FileDataHandler::getFileObjects()->get('my[great][file][input1]');`
273
-     * @since $VID:$
274
-     * @param $name_parts
275
-     * @throws UnexpectedValueException
276
-     * @return FileSubmissionInterface
277
-     */
278
-    public function getFileObjectFromNameParts($name_parts)
279
-    {
280
-        return $this->getFileObjects()->get($this->inputNameFromParts($name_parts));
281
-    }
268
+	/**
269
+	 * Gets the input by the indicated $name_parts.
270
+	 * Eg if you're looking for an input named "my[great][file][input1]", $name_parts
271
+	 * should be `['my', 'great', 'file', 'input1']`.
272
+	 * Alternatively, you could use `FileDataHandler::getFileObjects()->get('my[great][file][input1]');`
273
+	 * @since $VID:$
274
+	 * @param $name_parts
275
+	 * @throws UnexpectedValueException
276
+	 * @return FileSubmissionInterface
277
+	 */
278
+	public function getFileObjectFromNameParts($name_parts)
279
+	{
280
+		return $this->getFileObjects()->get($this->inputNameFromParts($name_parts));
281
+	}
282 282
 }
283 283
 // End of file FilesDataHandler.php
284 284
 // Location: EventEspresso\core\services\request\files/FilesDataHandler.php
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -71,7 +71,7 @@  discard block
 block discarded – undo
71 71
      */
72 72
     public function getFileObjects()
73 73
     {
74
-        if (!$this->initialized) {
74
+        if ( ! $this->initialized) {
75 75
             $this->initialize();
76 76
         }
77 77
         return $this->file_objects;
@@ -114,7 +114,7 @@  discard block
 block discarded – undo
114 114
      */
115 115
     protected function isStrangeFilesArray($files_data)
116 116
     {
117
-        if (!is_array($files_data)) {
117
+        if ( ! is_array($files_data)) {
118 118
             throw new UnexpectedValueException(
119 119
                 sprintf(
120 120
                     esc_html__(
@@ -126,7 +126,7 @@  discard block
 block discarded – undo
126 126
             );
127 127
         }
128 128
         $first_value = reset($files_data);
129
-        if (!is_array($first_value)) {
129
+        if ( ! is_array($first_value)) {
130 130
             throw new UnexpectedValueException(
131 131
                 sprintf(
132 132
                     esc_html__(
@@ -188,9 +188,9 @@  discard block
 block discarded – undo
188 188
         $organized_data = [];
189 189
         foreach ($data as $key => $val) {
190 190
             if (is_array($val)) {
191
-                $organized_data[ $key ] = $this->organizeFilesData($val, $type);
191
+                $organized_data[$key] = $this->organizeFilesData($val, $type);
192 192
             } else {
193
-                $organized_data[ $key ][ $type ] = $val;
193
+                $organized_data[$key][$type] = $val;
194 194
             }
195 195
         }
196 196
         return $organized_data;
@@ -208,7 +208,7 @@  discard block
 block discarded – undo
208 208
      */
209 209
     protected function createFileObjects($organized_files, $name_parts_so_far = [])
210 210
     {
211
-        if (!is_array($organized_files)) {
211
+        if ( ! is_array($organized_files)) {
212 212
             throw new UnexpectedValueException(
213 213
                 sprintf(
214 214
                     esc_html__(
@@ -251,7 +251,7 @@  discard block
 block discarded – undo
251 251
      */
252 252
     protected function inputNameFromParts($parts)
253 253
     {
254
-        if (!is_array($parts)) {
254
+        if ( ! is_array($parts)) {
255 255
             throw new UnexpectedValueException(esc_html__('Name parts should be an array.', 'event_espresso'));
256 256
         }
257 257
         $generated_string = '';
@@ -259,7 +259,7 @@  discard block
 block discarded – undo
259 259
             if ($generated_string === '') {
260 260
                 $generated_string = (string) $part;
261 261
             } else {
262
-                $generated_string .= '[' . (string) $part . ']';
262
+                $generated_string .= '['.(string) $part.']';
263 263
             }
264 264
         }
265 265
         return $generated_string;
Please login to merge, or discard this patch.