22
33namespace Differ ;
44
5+ use Differ \Formatters \StylishCommand ;
6+ use Differ \Formatters \PlainCommand ;
7+ use Differ \Formatters \JSONCommand ;
8+
59class Formatters implements CommandInterface
610{
711 private CommandInterface $ formatCommand ;
12+ private const array FORMAT_KEYS = [
13+ "stylish " => "stylish " ,
14+ "plain " => "plain " ,
15+ "json " => "json "
16+ ];
17+
18+ private function createCommand (string $ commandKey ): CommandInterface
19+ {
20+ switch ($ commandKey ) {
21+ case self ::FORMAT_KEYS ["stylish " ]:
22+ return new StylishCommand ();
23+ case self ::FORMAT_KEYS ["plain " ]:
24+ return new PlainCommand ();
25+ case self ::FORMAT_KEYS ["json " ]:
26+ return new JSONCommand ();
27+ default :
28+ return throw new DifferException ("input error: unknown output format \nUse gendiff -h \n" );
29+ }
30+ }
831
932 public function selectFormat (CommandInterface $ command = null ): CommandInterface
1033 {
@@ -13,14 +36,11 @@ public function selectFormat(CommandInterface $command = null): CommandInterface
1336 new FileReader ()
1437 );
1538 $ currentFormat = strtolower ($ command ->getFormat ());
16- $ formatKeys = $ commandFactory ->getFormatKeys ();
17- if (in_array ($ currentFormat , $ formatKeys )) {
18- $ this ->formatCommand = $ commandFactory ->getCommand (
19- $ currentFormat
20- );
21- } else {
22- throw new DifferException ("input error: unknown output format \nUse gendiff -h \n" );
23- }
39+ //$formatKeys = $commandFactory->getFormatKeys();
40+
41+ $ this ->formatCommand = $ this ->createCommand (
42+ $ currentFormat
43+ );
2444
2545 return $ this ;
2646 }
0 commit comments