Completed
Push — feature/issue-101 ( 426b61...bb6cdd )
by Mikaël
49:18
created

StructValue::toJsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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
    public function __construct(Generator $generator, $name, $index = 0, Struct $struct = null)
42
    {
43
        parent::__construct($generator, $name);
44
        $this->setIndex($index)->setOwner($struct);
45
    }
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
    public function getCleanName($keepMultipleUnderscores = false)
60
    {
61
        if ($this->getGenerator()->getOptionGenericConstantsNames()) {
62
            return 'ENUM_VALUE_' . $this->getIndex();
63
        } else {
64
            $nameWithSeparatedWords = $this->getNameWithSeparatedWords($keepMultipleUnderscores);
65
            $key = self::constantSuffix($this->getOwner()->getName(), $nameWithSeparatedWords, $this->getIndex());
66
            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
    public function getNameWithSeparatedWords($keepMultipleUnderscores = false)
74
    {
75
        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
    public function getValue()
84
    {
85
        return Utils::getValueWithinItsType($this->getName());
86
    }
87
    /**
88
     * Gets the index attribute value
89
     * @return int
90
     */
91
    public function getIndex()
92
    {
93
        return $this->index;
94
    }
95
    /**
96
     * Sets the index attribute value
97
     * @throws \InvalidArgumentException
98
     * @param int $index
99
     * @return StructValue
100
     */
101
    public function setIndex($index)
102
    {
103
        if (!is_int($index) || $index < 0) {
104
            throw new \InvalidArgumentException(sprintf('The value\'s index must be aa positive integer, "%s" given', var_export($index, true)));
105
        }
106
        $this->index = $index;
107
        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
    private static function constantSuffix($structName, $value, $index)
118
    {
119
        $key = strtoupper($structName . '_' . $value);
120
        $indexedKey = $key . '_' . $index;
121
        if (array_key_exists($indexedKey, self::$uniqueConstants)) {
122
            return self::$uniqueConstants[$indexedKey];
123
        } elseif (!array_key_exists($key, self::$uniqueConstants)) {
124
            self::$uniqueConstants[$key] = 0;
125
        } else {
126
            self::$uniqueConstants[$key]++;
127
        }
128
        self::$uniqueConstants[$indexedKey] = self::$uniqueConstants[$key];
129
        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
    public function getOwner()
138
    {
139
        return parent::getOwner();
140
    }
141
    /**
142
     * {@inheritDoc}
143
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::toJsonSerialize()
144
     */
145
    protected function toJsonSerialize()
146
    {
147
        return array(
148
            'index' => $this->index,
149
        );
150
    }
151
}
152