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