Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
ConstantGenerator
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
9 / 9
24
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 setType
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setValue
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
11
 formatArrayValues
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
5
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
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 */
14namespace Pop\Code\Generator;
15
16/**
17 * Constant 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 */
26class ConstantGenerator extends AbstractClassElementGenerator
27{
28
29    use Traits\NameTrait, Traits\DocblockTrait;
30
31    /**
32     * Constant type
33     * @var ?string
34     */
35    protected ?string $type = null;
36
37    /**
38     * Constant value
39     * @var mixed
40     */
41    protected mixed $value = null;
42
43    /**
44     * Constructor
45     *
46     * Instantiate the constant generator object
47     *
48     * @param  string $name
49     * @param  string $type
50     * @param  mixed  $value
51     */
52    public function __construct(string $name, string $type, mixed $value = null)
53    {
54        $this->setName($name);
55        $this->setType($type);
56        if ($value !== null) {
57            $this->setValue($value);
58        }
59    }
60
61    /**
62     * Set the constant type
63     *
64     * @param  string $type
65     * @return ConstantGenerator
66     */
67    public function setType(string $type): ConstantGenerator
68    {
69        $this->type = $type;
70        return $this;
71    }
72
73    /**
74     * Get the constant type
75     *
76     * @return string
77     */
78    public function getType(): string
79    {
80        return $this->type;
81    }
82
83    /**
84     * Set the constant value
85     *
86     * @param  mixed $value
87     * @return ConstantGenerator
88     */
89    public function setValue(mixed $value = null): ConstantGenerator
90    {
91        $this->value = $value;
92        return $this;
93    }
94
95    /**
96     * Get the constant value
97     *
98     * @return mixed
99     */
100    public function getValue(): mixed
101    {
102        return $this->value;
103    }
104
105    /**
106     * Has constant value
107     *
108     * @return bool
109     */
110    public function hasValue(): bool
111    {
112        return ($this->value !== null);
113    }
114
115    /**
116     * Render constant
117     *
118     * @return string
119     */
120    public function render(): string
121    {
122        if ($this->docblock === null) {
123            $this->docblock = new DocblockGenerator(null, $this->indent);
124        }
125
126        $this->docblock->addTag('var', $this->type);
127        $this->output = PHP_EOL . $this->docblock->render();
128        $this->output .= $this->printIndent() . 'const ' . $this->name;
129
130        if ($this->value !== null) {
131            if ($this->type == 'array') {
132                $val = (count($this->value) == 0) ? '[]' : $this->formatArrayValues();
133                $this->output .= ' = ' . $val . PHP_EOL;
134            } else if (($this->type == 'integer') || ($this->type == 'int') || ($this->type == 'float')) {
135                $this->output .= ' = ' . $this->value . ';';
136            } else if ($this->type == 'bool') {
137                $val = ($this->value) ? 'true' : 'false';
138                $this->output .= " = " . $val . ";";
139            } else {
140                $this->output .= " = '" . $this->value . "';";
141            }
142        } else {
143            $val = ($this->type == 'array') ? '[]' : 'null';
144            $this->output .= ' = ' . $val . ';';
145        }
146
147        return $this->output;
148    }
149
150    /**
151     * Format array value
152     *
153     * @return string
154     */
155    protected function formatArrayValues(): string
156    {
157        $ary = str_replace(PHP_EOL, PHP_EOL . $this->printIndent() . '  ', var_export($this->value, true));
158        $ary .= ';';
159        $ary = str_replace('array (', '[', $ary);
160        $ary = str_replace('  );', '];', $ary);
161        $ary = str_replace('NULL', 'null', $ary);
162
163        $keys = array_keys($this->value);
164
165        $isAssoc = false;
166
167        for ($i = 0; $i < count($keys); $i++) {
168            if ($keys[$i] != $i) {
169                $isAssoc = true;
170            }
171        }
172
173        if (!$isAssoc) {
174            for ($i = 0; $i < count($keys); $i++) {
175                $ary = str_replace($i . ' => ', '', $ary);
176            }
177        }
178
179        return $ary;
180    }
181
182    /**
183     * Print constant
184     *
185     * @return string
186     */
187    public function __toString(): string
188    {
189        return $this->render();
190    }
191
192}