Skip to content

Commit

Permalink
Support retrieving array syntax values from POST, GET, etc. Fixes #627
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell committed Aug 28, 2017
1 parent a9a3322 commit 71c2f38
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 65 deletions.
4 changes: 2 additions & 2 deletions system/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ protected function mapProperty(string $key)
*
* @return \CodeIgniter\I18n\Time
*/
private function mutateDate($value)
protected function mutateDate($value)
{
if ($value instanceof Time)
{
Expand Down Expand Up @@ -320,7 +320,7 @@ private function mutateDate($value)
*
* @return mixed
*/
private function castAs($value, string $type)
protected function castAs($value, string $type)
{
switch($type)
{
Expand Down
106 changes: 44 additions & 62 deletions system/HTTP/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,24 +322,26 @@ protected function fetchGlobal($type, $index = null, $filter = null, $flags = nu
$filter = FILTER_DEFAULT;
}

$loopThrough = [];
switch ($type)
{
case INPUT_GET : $loopThrough = $_GET;
break;
case INPUT_POST : $loopThrough = $_POST;
break;
case INPUT_COOKIE : $loopThrough = $_COOKIE;
break;
case INPUT_SERVER : $loopThrough = $_SERVER;
break;
case INPUT_ENV : $loopThrough = $_ENV;
break;
case INPUT_REQUEST : $loopThrough = $_REQUEST;
break;
}

// If $index is null, it means that the whole input type array is requested
if (is_null($index))
{
$loopThrough = [];
switch ($type)
{
case INPUT_GET : $loopThrough = $_GET;
break;
case INPUT_POST : $loopThrough = $_POST;
break;
case INPUT_COOKIE : $loopThrough = $_COOKIE;
break;
case INPUT_SERVER : $loopThrough = $_SERVER;
break;
case INPUT_ENV : $loopThrough = $_ENV;
break;
}

$values = [];
foreach ($loopThrough as $key => $value)
{
Expand All @@ -361,57 +363,37 @@ protected function fetchGlobal($type, $index = null, $filter = null, $flags = nu

return $output;
}
//
// // Does the index contain array notation?
// if (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) // Does the index contain array notation
// {
// $value = $array;
// for ($i = 0; $i < $count; $i++)
// {
// $key = trim($matches[0][$i], '[]');
// if ($key === '') // Empty notation will return the value as array
// {
// break;
// }
//
// if (isset($value[$key]))
// {
// $value = $value[$key];
// }
// else
// {
// return NULL;
// }
// }
// }

// Does the index contain array notation?
if (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1)
{
$value = $loopThrough;
for ($i = 0; $i < $count; $i++)
{
$key = trim($matches[0][$i], '[]');

if ($key === '') // Empty notation will return the value as array
{
break;
}

if (isset($value[$key]))
{
$value = $value[$key];
}
else
{
return null;
}
}
}

// Due to issues with FastCGI and testing,
// we need to do these all manually instead
// of the simpler filter_input();
switch ($type)
if (empty($value))
{
case INPUT_GET:
$value = $_GET[$index] ?? null;
break;
case INPUT_POST:
$value = $_POST[$index] ?? null;
break;
case INPUT_SERVER:
$value = $_SERVER[$index] ?? null;
break;
case INPUT_ENV:
$value = $_ENV[$index] ?? null;
break;
case INPUT_COOKIE:
$value = $_COOKIE[$index] ?? null;
break;
case INPUT_REQUEST:
$value = $_REQUEST[$index] ?? null;
break;
case INPUT_SESSION:
$value = $_SESSION[$index] ?? null;
break;
default:
$value = '';
$value = $loopThrough[$index] ?? null;
}

if (is_array($value) || is_object($value) || is_null($value))
Expand Down
1 change: 1 addition & 0 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,7 @@ protected function trigger(string $event, array $data)
}

//--------------------------------------------------------------------

//--------------------------------------------------------------------
// Magic
//--------------------------------------------------------------------
Expand Down
61 changes: 60 additions & 1 deletion tests/system/HTTP/IncomingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,66 @@ public function testFetchGlobalReturnsSingleValue()

//--------------------------------------------------------------------

/**
public function testFetchGlobalWithArrayTop()
{
$_POST = [
'clients' => [
'address' => [
'zipcode' => 90210
]
]
];

$this->assertEquals(['address' => ['zipcode' => 90210]], $this->request->getPost('clients'));
}

public function testFetchGlobalWithArrayChildNumeric()
{
$_POST = [
'clients' => [
[
'address' => [
'zipcode' => 90210
],
],
[
'address' => [
'zipcode' => 60610
],
],
]
];

$this->assertEquals(['zipcode' => 60610], $this->request->getPost('clients[1][address]'));
}

public function testFetchGlobalWithArrayChildElement()
{
$_POST = [
'clients' => [
'address' => [
'zipcode' => 90210
],
]
];

$this->assertEquals(['zipcode' => 90210], $this->request->getPost('clients[address]'));
}

public function testFetchGlobalWithArrayLastElement()
{
$_POST = [
'clients' => [
'address' => [
'zipcode' => 90210
]
]
];

$this->assertEquals(90210, $this->request->getPost('clients[address][zipcode]'));
}

/**
* @see https://github.com/bcit-ci/CodeIgniter4/issues/353
*/
public function testGetPostReturnsArrayValues()
Expand Down

0 comments on commit 71c2f38

Please sign in to comment.