-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,20 +20,29 @@ | |
use Chevere\Parameter\Interfaces\ArrayParameterInterface; | ||
use Chevere\Parameter\Interfaces\ArrayStringParameterInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\UploadedFileInterface; | ||
use function Chevere\Parameter\arguments; | ||
use function Chevere\Parameter\arrayp; | ||
use function Chevere\Parameter\arrayString; | ||
|
||
abstract class Controller extends BaseController implements ControllerInterface | ||
{ | ||
/** | ||
* @var array<string, mixed> | ||
*/ | ||
public array $_attributes; | ||
|
||
/** | ||
* @var array<string, mixed> | ||
*/ | ||
public array $_serverParams; | ||
|
||
private ?ArgumentsInterface $_query = null; | ||
|
||
private ?ArgumentsInterface $_body = null; | ||
|
||
/** | ||
* @var ?array<ArgumentsInterface> | ||
*/ | ||
private ?array $_files = null; | ||
private ?ArgumentsInterface $_files = null; | ||
|
||
public static function acceptQuery(): ArrayStringParameterInterface | ||
{ | ||
|
@@ -55,38 +64,20 @@ public function terminate(ResponseInterface $response): ResponseInterface | |
return $response; | ||
} | ||
|
||
final public function withQuery(array $query): static | ||
{ | ||
$new = clone $this; | ||
$new->_query = arguments($new::acceptQuery()->parameters(), $query); | ||
|
||
return $new; | ||
} | ||
|
||
final public function withBody(array $body): static | ||
final public function withServerRequest(ServerRequestInterface $serverRequest): static | ||
{ | ||
$new = clone $this; | ||
$new->_body = arguments($new::acceptBody()->parameters(), $body); | ||
|
||
return $new; | ||
} | ||
|
||
final public function withFiles(array $files): static | ||
{ | ||
$new = clone $this; | ||
$array = []; | ||
$parameters = $new->acceptFiles()->parameters(); | ||
foreach ($files as $key => $file) { | ||
$key = strval($key); | ||
$parameters->assertHas($key); | ||
$collection = match (true) { | ||
$parameters->requiredKeys()->contains($key) => $parameters->required($key), | ||
default => $parameters->optional($key), | ||
}; | ||
$arguments = arguments($collection->array(), $file); | ||
$array[$key] = $arguments; | ||
} | ||
$new->_files = $array; | ||
$new->_query = arguments( | ||
$new::acceptQuery()->parameters(), | ||
$serverRequest->getQueryParams() | ||
); | ||
$new->_body = arguments( | ||
$new::acceptBody()->parameters(), | ||
(array) ($serverRequest->getParsedBody() ?? []) | ||
Check warning on line 76 in src/Controller.php GitHub Actions / PHP 8.1 test on ubuntu-24.04
Check warning on line 76 in src/Controller.php GitHub Actions / PHP 8.2 test on ubuntu-24.04
Check warning on line 76 in src/Controller.php GitHub Actions / PHP 8.3 test on ubuntu-24.04
Check warning on line 76 in src/Controller.php GitHub Actions / PHP 8.4 test on ubuntu-24.04
|
||
); | ||
$new->_serverParams = $serverRequest->getServerParams(); | ||
$new->_attributes = $serverRequest->getAttributes(); | ||
$new->setFiles($serverRequest->getUploadedFiles()); | ||
|
||
return $new; | ||
} | ||
|
@@ -103,10 +94,20 @@ final public function body(): ArgumentsInterface | |
??= arguments(static::acceptBody()->parameters(), []); | ||
} | ||
|
||
final public function files(): array | ||
final public function files(): ArgumentsInterface | ||
{ | ||
return $this->_files | ||
??= []; | ||
??= arguments(static::acceptFiles()->parameters(), []); | ||
} | ||
|
||
final public function serverParams(): array | ||
{ | ||
return $this->_serverParams; | ||
} | ||
|
||
final public function attributes(): array | ||
{ | ||
return $this->_attributes; | ||
} | ||
|
||
protected function assertRuntime(ReflectionActionInterface $reflection): void | ||
|
@@ -115,4 +116,26 @@ protected function assertRuntime(ReflectionActionInterface $reflection): void | |
$this->body(); | ||
$this->files(); | ||
} | ||
|
||
/** | ||
* @param array<string, UploadedFileInterface> $files | ||
*/ | ||
protected function setFiles(array $files): void | ||
Check warning on line 123 in src/Controller.php GitHub Actions / PHP 8.1 test on ubuntu-24.04
Check warning on line 123 in src/Controller.php GitHub Actions / PHP 8.2 test on ubuntu-24.04
Check warning on line 123 in src/Controller.php GitHub Actions / PHP 8.3 test on ubuntu-24.04
Check warning on line 123 in src/Controller.php GitHub Actions / PHP 8.4 test on ubuntu-24.04
|
||
{ | ||
$arguments = []; | ||
$parameters = $this->acceptFiles()->parameters(); | ||
foreach ($files as $key => $file) { | ||
$key = strval($key); | ||
$parameters->assertHas($key); | ||
Check warning on line 129 in src/Controller.php GitHub Actions / PHP 8.1 test on ubuntu-24.04
Check warning on line 129 in src/Controller.php GitHub Actions / PHP 8.2 test on ubuntu-24.04
Check warning on line 129 in src/Controller.php GitHub Actions / PHP 8.3 test on ubuntu-24.04
Check warning on line 129 in src/Controller.php GitHub Actions / PHP 8.4 test on ubuntu-24.04
|
||
$array = [ | ||
'error' => $file->getError(), | ||
'name' => $file->getClientFilename(), | ||
'type' => $file->getClientMediaType(), | ||
'size' => $file->getSize(), | ||
'tmp_name' => $file->getStream()->getMetadata('uri'), | ||
]; | ||
$arguments[$key] = $array; | ||
} | ||
$this->_files = arguments($parameters, $arguments); | ||
} | ||
} |