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