ConstantGenerator::generate()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2014-04-27 
5
 */
6
7
namespace Net\Bazzline\Component\CodeGenerator;
8
9
use Net\Bazzline\Component\CodeGenerator\InvalidArgumentException;
10
use Net\Bazzline\Component\CodeGenerator\RuntimeException;
11
12
/**
13
 * Class ConstantGenerator
14
 * @package Net\Bazzline\Component\Locator\LocatorGenerator\Generator
15
 */
16
class ConstantGenerator extends AbstractGenerator
17
{
18
    /**
19
     * @param string $name
20
     * @return $this
21
     */
22
    public function setName($name)
23
    {
24
        $this->addGeneratorProperty('name', strtoupper($name), false);
25
26
        return $this;
27
    }
28
29
    /**
30
     * @param string $value
31
     * @return $this
32
     */
33
    public function setValue($value)
34
    {
35
        if (!is_numeric($value)) {
36
            $value = '\'' . (string) $value . '\'';
37
        }
38
39
        $this->addGeneratorProperty('value', $value, false);
40
41
        return $this;
42
    }
43
44
    /**
45
     * @throws InvalidArgumentException|RuntimeException
46
     * @return string
47
     * @todo implement exception throwing if mandatory parameter is missing
48
     */
49
    public function generate()
50
    {
51
        if ($this->canBeGenerated()) {
52
            $this->resetContent();
53
            $name = $this->getGeneratorProperty('name');
54
            $value = $this->getGeneratorProperty('value');
55
56
            if (is_null($name)
57
                || is_null($value)) {
58
                throw new RuntimeException('name and value are mandatory');
59
            }
60
61
            $block = $this->getBlockGenerator();
62
63
            $block->add('const ' . $name . ' = ' . $value . ';');
64
            $this->addContent($block);
65
        }
66
67
        return $this->generateStringFromContent();
68
    }
69
}