Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
BodyGenerator | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
createReturnConfig | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
render | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Code\Generator; |
15 | |
16 | /** |
17 | * Body generator class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Code |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 5.0.0 |
25 | */ |
26 | class BodyGenerator extends AbstractGenerator |
27 | { |
28 | |
29 | use Traits\DocblockTrait, Traits\BodyTrait; |
30 | |
31 | /** |
32 | * Create return config |
33 | * |
34 | * @param array $config |
35 | * @param int $indent |
36 | * @return BodyGenerator |
37 | */ |
38 | public function createReturnConfig(array $config, int $indent = 4): BodyGenerator |
39 | { |
40 | $body = var_export($config, true); |
41 | |
42 | if (($indent !== null) && (($indent % 2) == 0)) { |
43 | $multiplier = $indent / 2; |
44 | $replacePattern = str_repeat('$1', $multiplier) . '$2'; |
45 | } else { |
46 | $replacePattern = '$1$1$2'; |
47 | } |
48 | |
49 | $body = preg_replace("/^([ ]*)(.*)/m", $replacePattern, $body); |
50 | $bodyAry = preg_split("/\r\n|\n|\r/", $body); |
51 | $bodyAry = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [null, ']$1', ' => ['], $bodyAry); |
52 | $body = str_replace('NULL', 'null', implode(PHP_EOL, array_filter(["["] + $bodyAry))); |
53 | |
54 | $this->setBody('return ' . $body . ';', 0); |
55 | |
56 | return $this; |
57 | } |
58 | |
59 | /** |
60 | * Render body |
61 | * |
62 | * @return string |
63 | */ |
64 | public function render(): string |
65 | { |
66 | $this->output = PHP_EOL . (($this->docblock !== null) ? $this->docblock->render() : null); |
67 | $this->output .= $this->body. PHP_EOL; |
68 | |
69 | return $this->output; |
70 | } |
71 | |
72 | /** |
73 | * Print function |
74 | * |
75 | * @return string |
76 | */ |
77 | public function __toString(): string |
78 | { |
79 | return $this->render(); |
80 | } |
81 | |
82 | } |