Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
PropertyGenerator
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
10 / 10
25
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 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%
18 / 18
100.00% covered (success)
100.00%
1 / 1
12
 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-2023 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-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    4.1.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 $type = null;
36
37    /**
38     * Property value
39     * @var mixed
40     */
41    protected $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  boolean $static
53     */
54    public function __construct($name, $type = null, $value = null, $visibility = 'public', $static = false)
55    {
56        $this->setName($name);
57        $this->setType($type);
58        $this->setValue($value);
59        $this->setVisibility($visibility);
60        $this->setAsStatic($static);
61    }
62
63    /**
64     * Set the property type
65     *
66     * @param  string $type
67     * @return PropertyGenerator
68     */
69    public function setType($type)
70    {
71        $this->type = $type;
72        return $this;
73    }
74
75    /**
76     * Get the property type
77     *
78     * @return string
79     */
80    public function getType()
81    {
82        return $this->type;
83    }
84
85    /**
86     * Has property type
87     *
88     * @return boolean
89     */
90    public function hasType()
91    {
92        return (null !== $this->type);
93    }
94
95    /**
96     * Set the property value
97     *
98     * @param  mixed $value
99     * @return PropertyGenerator
100     */
101    public function setValue($value = null)
102    {
103        $this->value = $value;
104        return $this;
105    }
106
107    /**
108     * Get the property value
109     *
110     * @return mixed
111     */
112    public function getValue()
113    {
114        return $this->value;
115    }
116
117    /**
118     * Has property value
119     *
120     * @return boolean
121     */
122    public function hasValue()
123    {
124        return (null !== $this->value);
125    }
126
127    /**
128     * Render property
129     *
130     * @return mixed
131     */
132    public function render()
133    {
134        if (null === $this->docblock) {
135            $this->docblock = new DocblockGenerator(null, $this->indent);
136        }
137
138        $this->docblock->addTag('var', $this->type);
139        $this->output = PHP_EOL . $this->docblock->render();
140        $this->output .= $this->printIndent() . $this->visibility . (($this->static) ? ' static' : '') . ' $' . $this->name;
141
142        if (null !== $this->value) {
143            if ($this->type == 'array') {
144                $val = (count($this->value) == 0) ? '[]' : $this->formatArrayValues();
145                $this->output .= ' = ' . $val . PHP_EOL;
146            } else if (($this->type == 'integer') || ($this->type == 'int') || ($this->type == 'float')) {
147                $this->output .= ' = ' . $this->value . ';';
148            } else if ($this->type == 'boolean') {
149                $val = ($this->value) ? 'true' : 'false';
150                $this->output .= " = " . $val . ";";
151            } else {
152                $this->output .= " = '" . $this->value . "';";
153            }
154        } else {
155            $val = ($this->type == 'array') ? '[]' : 'null';
156            $this->output .= ' = ' . $val . ';';
157        }
158
159        return $this->output;
160    }
161
162    /**
163     * Format array value
164     *
165     * @return string
166     */
167    protected function formatArrayValues()
168    {
169        $ary = str_replace(PHP_EOL, PHP_EOL . $this->printIndent() . '  ', var_export($this->value, true));
170        $ary .= ';';
171        $ary = str_replace('array (', '[', $ary);
172        $ary = str_replace('  );', '];', $ary);
173        $ary = str_replace('NULL', 'null', $ary);
174
175        $keys = array_keys($this->value);
176
177        $isAssoc = false;
178
179        for ($i = 0; $i < count($keys); $i++) {
180            if ($keys[$i] != $i) {
181                $isAssoc = true;
182            }
183        }
184
185        if (!$isAssoc) {
186            for ($i = 0; $i < count($keys); $i++) {
187                $ary = str_replace($i . ' => ', '', $ary);
188            }
189        }
190
191        return $ary;
192    }
193
194    /**
195     * Print property
196     *
197     * @return string
198     */
199    public function __toString()
200    {
201        return $this->render();
202    }
203
204}