Skip to content

Commit 294e62e

Browse files
committed
add files handling to superglobals
1 parent b3eb0cc commit 294e62e

File tree

2 files changed

+143
-11
lines changed

2 files changed

+143
-11
lines changed

system/Superglobals.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,32 @@ final class Superglobals
5656
private array $request;
5757

5858
/**
59-
* @param array<string, array|float|int|string>|null $server
60-
* @param array<string, array|string>|null $get
61-
* @param array<string, array|string>|null $post
62-
* @param array<string, array|string>|null $cookie
63-
* @param array<string, array|string>|null $request
59+
* @var array<string, array{name: list<string>|string, type: list<string>|string, tmp_name: list<string>|string, error: int|list<int>, size: int|list<int>, full_path?: list<string>|string}>
60+
*/
61+
private array $files;
62+
63+
/**
64+
* @param array<string, array|float|int|string>|null $server
65+
* @param array<string, array|string>|null $get
66+
* @param array<string, array|string>|null $post
67+
* @param array<string, array|string>|null $cookie
68+
* @param array<string, array|string>|null $request
69+
* @param array<string, array{name: list<string>|string, type: list<string>|string, tmp_name: list<string>|string, error: int|list<int>, size: int|list<int>, full_path?: list<string>|string}>|null $files
6470
*/
6571
public function __construct(
6672
?array $server = null,
6773
?array $get = null,
6874
?array $post = null,
6975
?array $cookie = null,
7076
?array $request = null,
77+
?array $files = null,
7178
) {
7279
$this->server = $server ?? $_SERVER;
7380
$this->get = $get ?? $_GET;
7481
$this->post = $post ?? $_POST;
7582
$this->cookie = $cookie ?? $_COOKIE;
7683
$this->request = $request ?? $_REQUEST;
84+
$this->files = $files ?? $_FILES;
7785
}
7886

7987
/**
@@ -326,10 +334,31 @@ public function setRequestArray(array $array): void
326334
$_REQUEST = $array;
327335
}
328336

337+
/**
338+
* Get all $_FILES values
339+
*
340+
* @return array<string, array{name: list<string>|string, type: list<string>|string, tmp_name: list<string>|string, error: int|list<int>, size: int|list<int>, full_path?: list<string>|string}>
341+
*/
342+
public function getFilesArray(): array
343+
{
344+
return $this->files;
345+
}
346+
347+
/**
348+
* Set the entire $_FILES array
349+
*
350+
* @param array<string, array{name: list<string>|string, type: list<string>|string, tmp_name: list<string>|string, error: int|list<int>, size: int|list<int>, full_path?: list<string>|string}> $array
351+
*/
352+
public function setFilesArray(array $array): void
353+
{
354+
$this->files = $array;
355+
$_FILES = $array;
356+
}
357+
329358
/**
330359
* Get a superglobal array by name
331360
*
332-
* @param string $name The superglobal name (server, get, post, cookie, request)
361+
* @param string $name The superglobal name (server, get, post, cookie, request, files)
333362
*
334363
* @return array<string, array|float|int|string>
335364
*/
@@ -341,14 +370,15 @@ public function getGlobalArray(string $name): array
341370
'post' => $this->post,
342371
'cookie' => $this->cookie,
343372
'request' => $this->request,
373+
'files' => $this->files,
344374
default => [],
345375
};
346376
}
347377

348378
/**
349379
* Set a superglobal array by name
350380
*
351-
* @param string $name The superglobal name (server, get, post, cookie, request)
381+
* @param string $name The superglobal name (server, get, post, cookie, request, files)
352382
* @param array<string, array|float|int|string> $array The array to set
353383
*/
354384
public function setGlobalArray(string $name, array $array): void
@@ -359,6 +389,7 @@ public function setGlobalArray(string $name, array $array): void
359389
'post' => $this->setPostArray($array),
360390
'cookie' => $this->setCookieArray($array),
361391
'request' => $this->setRequestArray($array),
392+
'files' => $this->setFilesArray($array),
362393
default => null,
363394
};
364395
}

tests/system/SuperglobalsTest.php

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected function setUp(): void
2929
parent::setUp();
3030

3131
// Clear superglobals before each test
32-
$_SERVER = $_GET = $_POST = $_COOKIE = $_REQUEST = [];
32+
$_SERVER = $_GET = $_POST = $_COOKIE = $_REQUEST = $_FILES = [];
3333

3434
$this->superglobals = new Superglobals();
3535
}
@@ -302,14 +302,87 @@ public function testRequestSetArray(): void
302302
$this->assertSame($data, $_REQUEST);
303303
}
304304

305-
// Generic array methods
305+
// $_FILES tests
306+
public function testFilesGetArray(): void
307+
{
308+
$filesData = [
309+
'upload' => [
310+
'name' => 'document.pdf',
311+
'type' => 'application/pdf',
312+
'tmp_name' => '/tmp/phpTest',
313+
'error' => UPLOAD_ERR_OK,
314+
'size' => 12345,
315+
],
316+
];
317+
318+
$this->superglobals->setFilesArray($filesData);
319+
320+
$this->assertSame($filesData, $this->superglobals->getFilesArray());
321+
$this->assertSame($filesData, $_FILES);
322+
}
323+
324+
public function testFilesSetArrayWithMultipleFiles(): void
325+
{
326+
$filesData = [
327+
'photos' => [
328+
'name' => ['photo1.jpg', 'photo2.jpg'],
329+
'type' => ['image/jpeg', 'image/jpeg'],
330+
'tmp_name' => ['/tmp/phpA', '/tmp/phpB'],
331+
'error' => [UPLOAD_ERR_OK, UPLOAD_ERR_OK],
332+
'size' => [1234, 5678],
333+
],
334+
];
335+
336+
$this->superglobals->setFilesArray($filesData);
337+
338+
$this->assertSame($filesData, $this->superglobals->getFilesArray());
339+
$this->assertSame($filesData, $_FILES);
340+
}
341+
342+
public function testFilesSetArrayEmpty(): void
343+
{
344+
$this->superglobals->setFilesArray([
345+
'upload' => [
346+
'name' => 'test.txt',
347+
'type' => 'text/plain',
348+
'tmp_name' => '/tmp/test',
349+
'error' => UPLOAD_ERR_OK,
350+
'size' => 100,
351+
],
352+
]);
353+
354+
// Reset to empty
355+
$this->superglobals->setFilesArray([]);
356+
357+
$this->assertSame([], $this->superglobals->getFilesArray());
358+
$this->assertSame([], $_FILES);
359+
}
360+
361+
// Generic methods
306362
public function testGetGlobalArray(): void
307363
{
308364
$this->superglobals->setGet('test', 'value');
309365

310366
$this->assertSame(['test' => 'value'], $this->superglobals->getGlobalArray('get'));
311367
}
312368

369+
public function testGetGlobalArrayForFiles(): void
370+
{
371+
$filesData = [
372+
'upload' => [
373+
'name' => 'test.pdf',
374+
'type' => 'application/pdf',
375+
'tmp_name' => '/tmp/phpTest',
376+
'error' => UPLOAD_ERR_OK,
377+
'size' => 999,
378+
],
379+
];
380+
381+
$this->superglobals->setFilesArray($filesData);
382+
383+
$this->assertSame($filesData, $this->superglobals->getGlobalArray('files'));
384+
}
385+
313386
public function testGetGlobalArrayReturnsEmptyForInvalid(): void
314387
{
315388
$this->assertSame([], $this->superglobals->getGlobalArray('invalid'));
@@ -325,6 +398,24 @@ public function testSetGlobalArray(): void
325398
$this->assertSame($data, $_POST);
326399
}
327400

401+
public function testSetGlobalArrayForFiles(): void
402+
{
403+
$filesData = [
404+
'doc' => [
405+
'name' => 'file.txt',
406+
'type' => 'text/plain',
407+
'tmp_name' => '/tmp/test',
408+
'error' => UPLOAD_ERR_OK,
409+
'size' => 555,
410+
],
411+
];
412+
413+
$this->superglobals->setGlobalArray('files', $filesData);
414+
415+
$this->assertSame($filesData, $this->superglobals->getFilesArray());
416+
$this->assertSame($filesData, $_FILES);
417+
}
418+
328419
public function testSetGlobalArrayWithInvalidNameDoesNothing(): void
329420
{
330421
$this->superglobals->setGlobalArray('invalid', ['key' => 'value']);
@@ -341,13 +432,23 @@ public function testConstructorWithCustomArrays(): void
341432
$post = ['post_key' => 'post_value'];
342433
$cookie = ['cookie_key' => 'cookie_value'];
343434
$request = ['request_key' => 'request_value'];
344-
345-
$superglobals = new Superglobals($server, $get, $post, $cookie, $request);
435+
$files = [
436+
'upload' => [
437+
'name' => 'custom.pdf',
438+
'type' => 'application/pdf',
439+
'tmp_name' => '/tmp/custom',
440+
'error' => UPLOAD_ERR_OK,
441+
'size' => 7777,
442+
],
443+
];
444+
445+
$superglobals = new Superglobals($server, $get, $post, $cookie, $request, $files);
346446

347447
$this->assertSame('server_value', $superglobals->server('SERVER_KEY'));
348448
$this->assertSame('get_value', $superglobals->get('get_key'));
349449
$this->assertSame('post_value', $superglobals->post('post_key'));
350450
$this->assertSame('cookie_value', $superglobals->cookie('cookie_key'));
351451
$this->assertSame('request_value', $superglobals->request('request_key'));
452+
$this->assertSame($files, $superglobals->getFilesArray());
352453
}
353454
}

0 commit comments

Comments
 (0)