Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
StructValue
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
9 / 9
15
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
 getCleanName
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 getNameWithSeparatedWords
100.00% covered (success)
100.00%
1 / 1
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
 getIndex
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setIndex
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getOwner
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 constantSuffix
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 toJsonSerialize
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace WsdlToPhp\PackageGenerator\Model;
6
7use WsdlToPhp\PackageGenerator\Generator\Generator;
8use WsdlToPhp\PackageGenerator\Generator\Utils;
9
10/**
11 * Class StructValue stands for an enumeration value.
12 */
13final class StructValue extends AbstractModel
14{
15    public const MATCH_PATTERN = '/([[:upper:]]+[[:lower:]]*)|([[:lower:]]+)|(\d+)/';
16    public const REPLACEMENT_PATTERN = '$1$2$3_';
17    public const GENERIC_NAME_PREFIX = 'ENUM_VALUE_';
18    public const VALUE_NAME_PREFIX = 'VALUE_';
19
20    protected static array $uniqueConstants = [];
21
22    protected int $index = 0;
23
24    /**
25     * Cleaned name of the element stored in order to avoid multiple call that would generate incremental name.
26     */
27    private ?string $cleanedName = null;
28
29    public function __construct(Generator $generator, $name, int $index = 0, ?Struct $struct = null)
30    {
31        parent::__construct($generator, $name);
32        $this
33            ->setIndex($index)
34            ->setOwner($struct)
35        ;
36    }
37
38    public function getCleanName(bool $keepMultipleUnderscores = false): string
39    {
40        if ($this->cleanedName) {
41            return $this->cleanedName;
42        }
43
44        if ($this->getGenerator()->getOptionGenericConstantsNames()) {
45            return self::GENERIC_NAME_PREFIX.$this->getIndex();
46        }
47
48        $nameWithSeparatedWords = $this->getNameWithSeparatedWords($keepMultipleUnderscores);
49        $key = self::constantSuffix($this->getOwner()->getName(), $nameWithSeparatedWords, $this->getIndex());
50
51        return $this->cleanedName = self::VALUE_NAME_PREFIX.mb_strtoupper($nameWithSeparatedWords.($key ? '_'.$key : ''));
52    }
53
54    public function getNameWithSeparatedWords(bool $keepMultipleUnderscores = false): string
55    {
56        return trim(self::cleanString(preg_replace(self::MATCH_PATTERN, self::REPLACEMENT_PATTERN, (string) $this->getName()), $keepMultipleUnderscores), '_');
57    }
58
59    public function getValue()
60    {
61        return Utils::getValueWithinItsType($this->getName());
62    }
63
64    public function getIndex(): int
65    {
66        return $this->index;
67    }
68
69    public function setIndex(int $index): StructValue
70    {
71        if (0 > $index) {
72            throw new \InvalidArgumentException(sprintf('The value\'s index must be a positive integer, "%s" given', var_export($index, true)));
73        }
74        $this->index = $index;
75
76        return $this;
77    }
78
79    public function getOwner(): Struct
80    {
81        return parent::getOwner();
82    }
83
84    protected static function constantSuffix(string $structName, string $value, int $index): int
85    {
86        $key = mb_strtoupper($structName.'_'.$value);
87        $indexedKey = $key.'_'.$index;
88        if (array_key_exists($indexedKey, self::$uniqueConstants)) {
89            return self::$uniqueConstants[$indexedKey];
90        }
91        if (!array_key_exists($key, self::$uniqueConstants)) {
92            self::$uniqueConstants[$key] = 0;
93        } else {
94            ++self::$uniqueConstants[$key];
95        }
96        self::$uniqueConstants[$indexedKey] = self::$uniqueConstants[$key];
97
98        return self::$uniqueConstants[$key];
99    }
100
101    protected function toJsonSerialize(): array
102    {
103        return [
104            'index' => $this->index,
105        ];
106    }
107}