<?php
class Matrix {
private int $rows;
private int $columns;
private array $values;
public function __construct(int $rows, int $columns) {
$this->rows = $rows;
$this->columns = $columns;
}
public function rows(): int {
return $this->rows;
}
public function columns() : int {
return $this->columns;
}
}
class MatrixFactory {
public function create(int $rows, int $columns) : Matrix{
return new Matrix($rows, $columns);
}
}
interface MatrixCommand {
public function execute(Matrix $matrix): Response;
public function title() :string;
}
class TransposeMatrixCommand implements MatrixCommand {
public const TITLE = 'Pomnóż macierze wejściowe';
public function title() : string {
return self::TITLE;
}
public function execute(Matrix $matrix):Response {
return new Response();
}
}
class MultiplicationMatrixCommand implements MatrixCommand {
public const TITLE = 'Pomnóż macierze wejściowe';
public function title() : string {
return self::TITLE;
}
public function execute(Matrix $matrix):Response {
return new Response();
}
}
class Response {
}
class RegistryMatrixCommand {
private array $registry;
public function __construct(iterable $commands) {
$this->addCommands($commands);
}
public function addCommand(string $command) {
if(false === class_exists($command)) {
throw new Exception('Class not exists');
}
$this->registry[$command] = new $command;
}
public function addCommands(iterable $commands) {
foreach($commands as $command) {
$this->addCommand($command);
}
}
public function get(string $action) : MatrixCommand {
if(false === array_key_exists($action, $this->registry)) {
throw new Exception('Not found command!');
}
return $this->registry[$action];
}
public function list() : iterable {
return $this->registry;
}
}
class MenuRenderer {
private array $commands;
public function render(array $commands) : string {
$menu = '';
$counter = 1;
foreach($commands as $command) {
$counter++;
$menu .= sprinf('%d - %s', $counter, $command->title());
}
return $menu;
}
}
class MatrixAction {
private RegistryMatrixCommand $registry;
private MenuRenderer $menuRenderer;
public function __construct(RegistryMatrixCommand $registry, MenuRenderer $menuRenderer) {
$this->registry = $registry;
$this->menuRenderer = $menuRenderer;
}
public function displayActions(): string {
return $this->menuRenderer->render($this->registry->list());
}
public function execute(Request $request) : Response {
$matrix = (new MatrixFactory())->create(
$request->rows(),
$request->columns()
);
$command = $this->registry->get($request->action());
$response = $command->execute($matrix);
return $response;
}
}
class Request {
private string $action;
private int $rows;
private int $columns;
public function __construct(string $action, int $rows, int $columns) {
$this->action = $action;
$this->rows = $rows;
$this->columns = $columns;
}
public function action() :string {
return $this->action;
}
public function rows() : int {
return $this->rows;
}
public function columns(): int {
return $this->columns;
}
}
$actions = new ArrayIterator([
MultiplicationMatrixCommand::class,
TransposeMatrixCommand::class
]);
$registryMatrix = new RegistryMatrixCommand($actions);
$menuRenderer = new MenuRenderer();
$matrixAction = new MatrixAction($registryMatrix, $menuRenderer);
$request = new Request('TransposeMatrixCommand', 3, 4);
echo $matrixAction->displayActions();
// var_dump($matrixAction->execute($request));
Fatal error: Uncaught Error: Call to undefined function sprinf() in /in/A6cvm:104
Stack trace:
#0 /in/A6cvm(121): MenuRenderer->render(Array)
#1 /in/A6cvm(170): MatrixAction->displayActions()
#2 {main}
thrown in /in/A6cvm on line 104
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Fatal error: Uncaught Error: Call to undefined function sprinf() in /in/A6cvm:104
Stack trace:
#0 /in/A6cvm(121): MenuRenderer->render(Array)
#1 /in/A6cvm(170): MatrixAction->displayActions()
#2 {main}
thrown in /in/A6cvm on line 104
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
Parse error: syntax error, unexpected 'int' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/A6cvm on line 4
Process exited with code 255.