3v4l.org

run code in 300+ PHP versions simultaneously
<?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) { $menu .= sprintf('%d - %s', $counter, $command->title()) . '\n'; $counter++; } 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));
Output for 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
1 - Pomnóż macierze wejściowe\n2 - Pomnóż macierze wejściowe\n
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/kDK7X on line 4
Process exited with code 255.

preferences:
159.89 ms | 401 KiB | 172 Q