Completed
Push — develop ( af2f24...b1074b )
by Mikaël
85:27 queued 45:11
created

StructValue::toJsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\Model;
4
5
use WsdlToPhp\PackageGenerator\Generator\Generator;
6
use WsdlToPhp\PackageGenerator\Generator\Utils;
7
8
/**
9
 * Class StructValue stands for an enumeration value
10
 */
11
class StructValue extends AbstractModel
12
{
13
    /**
14
     * @var string
15
     */
16
    const MATCH_PATTERN = '/([[:upper:]]+[[:lower:]]*)|([[:lower:]]+)|(\d+)/';
17
    /**
18
     * @var string
19
     */
20
    const REPLACEMENT_PATTERN = '$1$2$3_';
21
    /**
22
     * Store the constants generated per structName
23
     * @var array
24
     */
25
    private static $uniqueConstants = array();
26
    /**
27
     * The index of the value in the enumeration struct
28
     * @var int
29
     */
30
    private $index = 0;
31
    /**
32
     * Main constructor
33
     * @see AbstractModel::__construct()
34
     * @uses AbstractModel::setOwner()
35
     * @uses StructValue::setIndex()
36
     * @param Generator $generator
37
     * @param string $name the original name
38
     * @param string $index the index of the value in the enumeration struct
39
     * @param Struct $struct defines the struct which owns this value
40
     */
41 410
    public function __construct(Generator $generator, $name, $index = 0, Struct $struct = null)
42
    {
43 410
        parent::__construct($generator, $name);
44 410
        $this->setIndex($index)->setOwner($struct);
45 405
    }
46
    /**
47
     * Returns the name of the value as constant
48
     * @see AbstractModel::getCleanName()
49
     * @uses AbstractModel::getCleanName()
50
     * @uses AbstractModel::getName()
51
     * @uses AbstractModel::getOwner()
52
     * @uses StructValue::constantSuffix()
53
     * @uses StructValue::getIndex()
54
     * @uses StructValue::getOwner()
55
     * @uses Generator::getOptionGenericConstantsNames()
56
     * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores
57
     * @return string
58
     */
59 70
    public function getCleanName($keepMultipleUnderscores = false)
60
    {
61 70
        if ($this->getGenerator()->getOptionGenericConstantsNames()) {
62 5
            return 'ENUM_VALUE_' . $this->getIndex();
63
        } else {
64 65
            $nameWithSeparatedWords = $this->getNameWithSeparatedWords($keepMultipleUnderscores);
65 65
            $key = self::constantSuffix($this->getOwner()->getName(), $nameWithSeparatedWords, $this->getIndex());
66 65
            return 'VALUE_' . strtoupper($nameWithSeparatedWords . ($key ? '_' . $key : ''));
67
        }
68
    }
69
    /**
70
     * @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores
71
     * @return string
72
     */
73 65
    public function getNameWithSeparatedWords($keepMultipleUnderscores = false)
74
    {
75 65
        return trim(self::cleanString(preg_replace(self::MATCH_PATTERN, self::REPLACEMENT_PATTERN, $this->getName()), $keepMultipleUnderscores), '_');
76
    }
77
    /**
78
     * Returns the value with good type
79
     * @uses AbstractModel::getName()
80
     * @uses Utils::getValueWithinItsType()
81
     * @return mixed
82
     */
83 70
    public function getValue()
84
    {
85 70
        return Utils::getValueWithinItsType($this->getName());
86
    }
87
    /**
88
     * Gets the index attribute value
89
     * @return int
90
     */
91 70
    public function getIndex()
92
    {
93 70
        return $this->index;
94
    }
95
    /**
96
     * Sets the index attribute value
97
     * @throws \InvalidArgumentException
98
     * @param int $index
99
     * @return StructValue
100
     */
101 410
    public function setIndex($index)
102
    {
103 410
        if (!is_int($index) || $index < 0) {
104 10
            throw new \InvalidArgumentException(sprintf('The value\'s index must be aa positive integer, "%s" given', var_export($index, true)));
105
        }
106 405
        $this->index = $index;
107 405
        return $this;
108
    }
109
    /**
110
     * Returns the index which has to be added at the end of natural constant name defined with the value cleaned
111
     * Allows to avoid multiple constant name to be indentic
112
     * @param string $structName the struct name
113
     * @param string $value the value
114
     * @param int $index the position of the value
115
     * @return int
116
     */
117 65
    private static function constantSuffix($structName, $value, $index)
118
    {
119 65
        $key = strtoupper($structName . '_' . $value);
120 65
        $indexedKey = $key . '_' . $index;
121 65
        if (array_key_exists($indexedKey, self::$uniqueConstants)) {
122 60
            return self::$uniqueConstants[$indexedKey];
123 35
        } elseif (!array_key_exists($key, self::$uniqueConstants)) {
124 35
            self::$uniqueConstants[$key] = 0;
125 21
        } else {
126 5
            self::$uniqueConstants[$key]++;
127
        }
128 35
        self::$uniqueConstants[$indexedKey] = self::$uniqueConstants[$key];
129 35
        return self::$uniqueConstants[$key];
130
    }
131
    /**
132
     * Returns the owner model object, meaning a Struct object
133
     * @see AbstractModel::getOwner()
134
     * @uses AbstractModel::getOwner()
135
     * @return Struct
136
     */
137 65
    public function getOwner()
138
    {
139 65
        return parent::getOwner();
140
    }
141
    /**
142
     * {@inheritDoc}
143
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::toJsonSerialize()
144
     */
145 5
    protected function toJsonSerialize()
146
    {
147
        return array(
148 5
            'index' => $this->index,
149 3
        );
150
    }
151
}
152