Skip to content

Commit

Permalink
Refactoring logic for exceptBodyParams and onlyBodyParams to use \arr…
Browse files Browse the repository at this point in the history
…ay_filter by passing in ARRAY_FILTER_USE_KEY.

Adding hasUploadedFile method to ServerRequest along with test coverage.
  • Loading branch information
brentscheffler committed Jan 22, 2020
1 parent fb37d27 commit 722b8b5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,11 @@ public function getBodyParam(string $param)
*/
public function onlyBodyParams(array $params): array
{
$only = [];
return \array_filter((array) $this->parsedBody, function($key) use ($params): bool {

foreach( (array) $this->parsedBody as $key => $value ){
return \in_array($key, $params);

if( \in_array($key, $params) ){
$only[$key] = $value;
}
}

return $only;
}, ARRAY_FILTER_USE_KEY);
}

/**
Expand All @@ -287,15 +282,11 @@ public function onlyBodyParams(array $params): array
*/
public function exceptBodyParams(array $params): array
{
$except = [];
return \array_filter((array) $this->parsedBody, function($key) use ($params): bool {

foreach( (array) $this->parsedBody as $key => $value ){
if( !\in_array($key, $params) ){
$except[$key] = $value;
}
}
return !\in_array($key, $params);

return $except;
}, ARRAY_FILTER_USE_KEY);
}

/**
Expand Down Expand Up @@ -333,6 +324,17 @@ public function getAllParams(): array
);
}

/**
* Check for the presense of an uploaded file.
*
* @param string $name
* @return boolean
*/
public function hasUploadedFile(string $name): bool
{
return \array_key_exists($name, $this->getUploadedFiles());
}

/**
* Get an UploadedFileInterface instance by its name.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/ServerRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,21 @@ public function test_except_body_params()
);
}

public function test_has_uploaded_file()
{
$request = $this->makeRequest();

$uploadedFile = new UploadedFile("Ok");

$request = $request->withUploadedFiles([
"file" => $uploadedFile
]);

$this->assertTrue(
$request->hasUploadedFile("file")
);
}

public function test_get_uploaded_file()
{
$request = $this->makeRequest();
Expand Down

0 comments on commit 722b8b5

Please sign in to comment.