44
55namespace CyrilVerloop \Codingame \Configuration ;
66
7- use CyrilVerloop \Codingame \Generator \GeneratorCodeConfiguration ;
8- use CyrilVerloop \Codingame \Generator \GeneratorTestConfiguration ;
7+ use CyrilVerloop \Codingame \Configuration \TestConfigurations ;
8+ use CyrilVerloop \Codingame \Generator \TestConfiguration as TCGenerator ;
9+ use CyrilVerloop \Codingame \Generator \TestConfigurations as TCsGenerator ;
10+ use CyrilVerloop \Codingame \Generator \CodeGeneratorConfiguration ;
11+ use CyrilVerloop \Codingame \Generator \TestGeneratorConfiguration ;
912use CyrilVerloop \Codingame \Parser \ParsedConfiguration ;
1013
1114final class ConfigurationConvertor
@@ -16,51 +19,118 @@ final class ConfigurationConvertor
1619 * Returns the code configuration for the generator.
1720 * @param \CyrilVerloop\Codingame\Parser\ParsedConfiguration $parsedConfiguration the parsed configuration.
1821 * @param string $defaultCodeFile the file with the default code.
19- * @return \CyrilVerloop\Codingame\Generator\GeneratorCodeConfiguration the code configuration for the generator.
22+ * @return \CyrilVerloop\Codingame\Generator\CodeGeneratorConfiguration the code configuration for the generator.
2023 * @throws \RuntimeException if the default code file is not readable.
2124 */
22- static function getGeneratorCodeConfiguration (
25+ static function getCodeGeneratorConfiguration (
2326 ParsedConfiguration $ parsedConfiguration ,
2427 string $ defaultCodeFile
25- ): GeneratorCodeConfiguration {
28+ ): CodeGeneratorConfiguration {
2629
2730 if (is_readable ($ defaultCodeFile ) === false ) {
2831 throw new \RuntimeException ('defaultCodeFileNotReadable ' );
2932 }
3033
3134 $ defaultCode = file_get_contents ($ defaultCodeFile );
35+
36+ return new CodeGeneratorConfiguration (
37+ self ::convertPathToNamespace ($ parsedConfiguration ->getPath ()),
38+ $ parsedConfiguration ->getName (),
39+ $ parsedConfiguration ->getLink (),
40+ self ::adaptCodeForCodeGenerator ($ defaultCode )
41+ );
42+ }
43+
44+ /**
45+ * Converts a path to a namespace.
46+ * @param string $path the path.
47+ * @return string the namespace.
48+ */
49+ private static function convertPathToNamespace (string $ path ): string
50+ {
51+ $ explodedPath = explode ('/ ' , $ path );
52+
53+ foreach ($ explodedPath as $ index => $ pathSection ) {
54+ $ explodedPath [$ index ] = ucfirst ($ pathSection );
55+ }
56+
57+ return implode ('\\' , $ explodedPath );
58+ }
59+
60+ /**
61+ * Adapts the code for the code generator.
62+ * @param string $defaultCode the default code.
63+ * @return string the adapted code.
64+ */
65+ private static function adaptCodeForCodeGenerator (string $ defaultCode ): string
66+ {
67+ // Removes the PHP open tag :
68+ $ openTagRemovedCode = preg_replace (
69+ '/<\?php\n/ ' ,
70+ '' ,
71+ $ defaultCode
72+ );
73+
74+ // Changes "STDIN" to "$stdin" :
75+ $ changedStdinCode = preg_replace (
76+ '/STDIN/ ' ,
77+ '$stdin ' ,
78+ $ openTagRemovedCode
79+ );
80+
81+ // Adds indentation :
3282 $ spaceAddedCode = preg_replace (
3383 '/\n/ ' ,
3484 "\n " ,
35- $ defaultCode
85+ $ changedStdinCode
3686 );
37- $ indentedDefaultCode = preg_replace (
87+
88+ // Removes indentation for empty lines :
89+ return preg_replace (
3890 '/\n \n/ ' ,
3991 "\n\n" ,
4092 $ spaceAddedCode
4193 );
42-
43- return new GeneratorCodeConfiguration (
44- $ parsedConfiguration ->getNamespace (),
45- $ parsedConfiguration ->getName (),
46- $ parsedConfiguration ->getLink (),
47- $ indentedDefaultCode
48- );
4994 }
5095
96+
5197 /**
5298 * Returns the test configuration for the generator.
5399 * @param \CyrilVerloop\Codingame\Parser\ParsedConfiguration $parsedConfiguration the parsed configuration.
54- * @return \CyrilVerloop\Codingame\Generator\GeneratorTestConfiguration the test configuration for the generator.
100+ * @return \CyrilVerloop\Codingame\Generator\TestGeneratorConfiguration the test configuration for the generator.
55101 */
56- static function getGeneratorTestConfiguration (
102+ static function getTestGeneratorConfiguration (
57103 ParsedConfiguration $ parsedConfiguration ,
58- ): GeneratorTestConfiguration {
59- return new GeneratorTestConfiguration (
60- $ parsedConfiguration ->getNamespace ( ),
104+ ): TestGeneratorConfiguration {
105+ return new TestGeneratorConfiguration (
106+ self :: convertPathToNamespace ( $ parsedConfiguration ->getPath () ),
61107 $ parsedConfiguration ->getName (),
62- $ parsedConfiguration ->getGroup (),
63- $ parsedConfiguration ->getTestConfigurations ()
108+ $ parsedConfiguration ->getAlphanumName (),
109+ self :: convertTestConfigurationsForGenerator ( $ parsedConfiguration ->getTestConfigurations () )
64110 );
65111 }
112+
113+ /**
114+ * Converts the tests configurations.
115+ * @param TestConfigurations $testConfigurations the tests configurations.
116+ * @return \CyrilVerloop\Codingame\Generator\TestConfigurations the tests configurations.
117+ */
118+ private static function convertTestConfigurationsForGenerator (
119+ TestConfigurations $ testConfigurations
120+ ): TCsGenerator {
121+ $ tCsGenerator = new TCsGenerator ();
122+
123+ foreach ($ testConfigurations as $ testConfiguration ) {
124+ $ tCGenerator = new TCGenerator (
125+ $ testConfiguration ->getName (),
126+ $ testConfiguration ->getAlphanumName (),
127+ ucfirst ($ testConfiguration ->getAlphanumName ()),
128+ $ testConfiguration ->getFile ()
129+ );
130+
131+ $ tCsGenerator ->add ($ tCGenerator );
132+ }
133+
134+ return $ tCsGenerator ;
135+ }
66136}
0 commit comments