Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
EnumCaseGenerator
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 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%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(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 */
15namespace Pop\Code\Generator;
16
17use Pop\Code\Generator\Support\ValueFormatter;
18
19/**
20 * Enum case generator class
21 *
22 * @category   Pop
23 * @package    Pop\Code
24 * @author     Nick Sagona, III <dev@noladev.com>
25 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    6.0.0
28 */
29class EnumCaseGenerator extends AbstractGenerator
30{
31
32    use Traits\NameTrait, Traits\DocblockTrait, Traits\AttributesTrait;
33
34    /**
35     * Case value (backed enums only)
36     * @var int|string|null
37     */
38    protected int|string|null $value = null;
39
40    /**
41     * Constructor
42     *
43     * Instantiate the enum case generator object
44     *
45     * @param  string          $name
46     * @param  int|string|null $value
47     */
48    public function __construct(string $name, int|string|null $value = null)
49    {
50        $this->setName($name);
51        if ($value !== null) {
52            $this->setValue($value);
53        }
54    }
55
56    /**
57     * Set the case value
58     *
59     * @param  int|string|null $value
60     * @return EnumCaseGenerator
61     */
62    public function setValue(int|string|null $value = null): EnumCaseGenerator
63    {
64        $this->value = $value;
65        return $this;
66    }
67
68    /**
69     * Get the case value
70     *
71     * @return int|string|null
72     */
73    public function getValue(): int|string|null
74    {
75        return $this->value;
76    }
77
78    /**
79     * Has case value
80     *
81     * @return bool
82     */
83    public function hasValue(): bool
84    {
85        return ($this->value !== null);
86    }
87
88    /**
89     * Render case
90     *
91     * @return string
92     */
93    public function render(): string
94    {
95        $this->output = PHP_EOL . (($this->docblock !== null) ? $this->docblock->render() : null);
96        $this->output .= $this->formatAttributes();
97        $this->output .= $this->printIndent() . 'case ' . $this->name;
98
99        if ($this->value !== null) {
100            $type = is_int($this->value) ? 'int' : 'string';
101            $this->output .= ' = ' . ValueFormatter::format($this->value, $type) . ';';
102        } else {
103            $this->output .= ';';
104        }
105
106        return $this->output;
107    }
108
109    /**
110     * Print case
111     *
112     * @return string
113     */
114    public function __toString(): string
115    {
116        return $this->render();
117    }
118
119}