online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
<?php /** * PHP Challenge vol.9 - Pan od informatyki * * Napisz klasę która z imion ze zmiennej $data wyfiltruje imiona zaczynające się * na wybraną literę, oraz zwróci tablicę imion * * Proszę o stworzenie inferfejsu zgodnie z zasadą * Interface segregation principle (Zasada segregacji interfejsów) * aby łatwo zmienić format danych przesyłanych do naszej biblioteki. Tym razem dane imiona przyjdą rozdzielone przecinkami * możeliwe, że w przyszłości zaistnieje potrzeba wyodrębnienia imion rozdzielonych tylko spacją */ require('./autoloader.php'); $data = 'judyta; julia; kamila; karina; laura; lena; magdalena; maja; nadia; natalia; olga; oliwia; pamela; patrycja; regina; renata; sabina; sandra; tamara; tatiana; urszula; weronika; wiesława; zofia; zuzanna; Żaneta; adam; bartłomiej; bartosz; cezary; cyprian; damian; daniel; edward; emanuel; fabian; feliks; gabriel; gerard; henryk; herbert; ignacy; igor; jacek; jakub; kacper; kajetan; lech; leon; maciej; maksymilian; nikodem; norbert; olaf; olgierd; patryk; paweł; radosław; rafał; sebastian; seweryn; tadeusz; teodor; wacław; waldemar; zdzisław; zygmunt'; $data2 = 'judyta, julia, kamila, karina, laura, lena, magdalena, maja, nadia, natalia, olga, oliwia, pamela, patrycja, regina, renata, sabina, sandra, tamara, tatiana, urszula, weronika, wiesława, zofia, zuzanna, Żaneta, adam, bartłomiej, bartosz, cezary, cyprian, damian, daniel, edward, emanuel, fabian, feliks, gabriel, gerard, henryk, herbert, ignacy, igor, jacek, jakub, kacper, kajetan, lech, leon, maciej, maksymilian, nikodem, norbert, olaf, olgierd, patryk, paweł, radosław, rafał, sebastian, seweryn, tadeusz, teodor, wacław, waldemar, zdzisław, zygmunt'; // Aby rozpocząć edycję kliknij w przycisk powyżej "Fork this" use App\IndexController; use App\RozwiazanieController; $controller = new IndexController(); $out = $controller->indexAction('h', $data); var_dump($out); $out2 = $controller->index2Action('h', $data2); var_dump($out2); /** * Rozwiązanie */ // $rozwiazanie = new RozwiazanieController(); // $out = $rozwiazanie->indexAction('h', $data); // var_dump($out); // $out2 = $rozwiazanie->index2Action('h', $data2); // var_dump($out2);
<?php spl_autoload_register(function($class){ $classArr = explode('\\', $class, 2); if(isset($classArr[1])){ $class = str_replace('\\', '/', $classArr[1]); $namespce = [ 'App' => __DIR__.'/', ]; if(isset($namespce[$classArr[0]])) { $path = $namespce[$classArr[0]]; $file = $path.$class.'.php'; if (file_exists($file)) { require_once($file); } } } }, true, true);
<?php namespace App; use App\NamesParser; use App\BigFirstLetterFormatter; use App\BigAllLettersFormatter; use App\NamesFilter; use App\NamesService; class IndexController { /** * Główna akcja */ public function indexAction($firstLetter, $names){ $formatter = new BigFirstLetterFormatter(); // $formatter = new BigAllLettersFormatter(); $service = new NamesService($names, new NamesParser(), new NamesFilter(), $formatter); return $service->getNames($firstLetter); } /** * Główna akcja */ public function index2Action($firstLetter, $names){ $formatter = new BigFirstLetterFormatter(); // $formatter = new BigAllLettersFormatter(); $service = new NamesService($names, new NamesParser(), new NamesFilter(), $formatter); return $service->getNames($firstLetter); } }
<?php namespace App; use App\FormatterInterface; class NamesService { private $names; private $namesParser; private $nameFilter; private $nameFormatter; public function __construct($names, $parser, $filter, FormatterInterface $formatter){ $this->names = $names; $this->namesParser = $parser; $this->nameFilter = $filter; $this->nameFormatter = $formatter; } /** * Główna funkcja zwracająca listę wyfiltrowanych imion */ public function getNames($firstLetter){ $arr = $this->namesParser->getNamesArray($this->names); $arr = $this->nameFilter->filterByFirstLetter($firstLetter, $arr); foreach($arr as &$name){ $name = $this->nameFormatter->format($name); } return $arr; } }
<?php namespace App; interface FormatterInterface { public function format(string $name) : string; }
<?php namespace App; use App\FormatterInterface; class BigFirstLetterFormatter implements FormatterInterface { public function format(string $name) : string { return ucfirst($name); } }
<?php namespace App; use App\FormatterInterface; class BigAllLettersFormatter implements FormatterInterface { public function format(string $name) : string { return strtoupper($name); } }
<?php namespace App; class NamesFilter { /** * Funkcja filtrująca listę i zostawiająca tylko imiona zaczynające się na wybraną literę */ public function filterByFirstLetter($firstLetter, $names){ $list = array_filter($names, function($item) use ($firstLetter) { return strtoupper($item[0]) == strtoupper($firstLetter); }); return $list; } }
<?php namespace App; class NamesParser { /** * Główna funkcja zwracająca listę wyfiltrowanych imion */ public function getNamesArray($names){ return $this->getNamesListFromString($names); } /** * Funkcja przetwarzająca ciąg imion w tablicę, jednocześnie usuwając białe znaki z początku i końca imienia */ private function getNamesListFromString($data, $separator = ';'){ $data = explode($separator, $data); foreach($data as &$item){ $item = trim($item); } return $data; } }
<?php namespace App; use App\NamesParser; use App\BigFirstLetterFormatter; use App\BigAllLettersFormatter; use App\NamesFilter; use App\NewNamesService; use App\CommaParser; use App\SemicolonParser; class RozwiazanieController { /** * Główna akcja */ public function indexAction($firstLetter, $names){ $formatter = new BigFirstLetterFormatter(); // $formatter = new BigAllLettersFormatter(); $service = new NewNamesService($names, new SemicolonParser(), new NamesFilter(), $formatter); return $service->getNames($firstLetter); } /** * Główna akcja */ public function index2Action($firstLetter, $names){ $formatter = new BigFirstLetterFormatter(); // $formatter = new BigAllLettersFormatter(); $service = new NewNamesService($names, new CommaParser(), new NamesFilter(), $formatter); return $service->getNames($firstLetter); } }
<?php namespace App; use App\FormatterInterface; use App\ParserInterface; class NewNamesService { private $names; private $namesParser; private $nameFilter; private $nameFormatter; public function __construct($names, ParserInterface $parser, $filter, FormatterInterface $formatter){ $this->names = $names; $this->namesParser = $parser; $this->nameFilter = $filter; $this->nameFormatter = $formatter; } /** * Główna funkcja zwracająca listę wyfiltrowanych imion */ public function getNames($firstLetter){ $arr = $this->namesParser->getDataArray($this->names); $arr = $this->nameFilter->filterByFirstLetter($firstLetter, $arr); foreach($arr as &$name){ $name = $this->nameFormatter->format($name); } return $arr; } }
<?php namespace App; interface ParserInterface { public function getDataArray(string $name) : array; }
<?php namespace App; use App\ParserInterface; class SemicolonParser implements ParserInterface { public function getDataArray(string $data) : array { $data = explode(';', $data); foreach($data as &$item){ $item = trim($item); } return $data; } }
<?php namespace App; use App\ParserInterface; class CommaParser implements ParserInterface { public function getDataArray(string $data) : array { $data = explode(',', $data); foreach($data as &$item){ $item = trim($item); } return $data; } }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue