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    /**
70     * @throws \InvalidArgumentException
71     */
72    public function setIndex(int $index): StructValue
73    {
74        if (0 > $index) {
75            throw new \InvalidArgumentException(sprintf('The value\'s index must be a positive integer, "%s" given', var_export($index, true)));
76        }
77        $this->index = $index;
78
79        return $this;
80    }
81
82    public function getOwner(): Struct
83    {
84        return parent::getOwner();
85    }
86
87    protected static function constantSuffix(string $structName, string $value, int $index): int
88    {
89        $key = mb_strtoupper($structName.'_'.$value);
90        $indexedKey = $key.'_'.$index;
91        if (array_key_exists($indexedKey, self::$uniqueConstants)) {
92            return self::$uniqueConstants[$indexedKey];
93        }
94        if (!array_key_exists($key, self::$uniqueConstants)) {
95            self::$uniqueConstants[$key] = 0;
96        } else {
97            ++self::$uniqueConstants[$key];
98        }
99        self::$uniqueConstants[$indexedKey] = self::$uniqueConstants[$key];
100
101        return self::$uniqueConstants[$key];
102    }
103
104    protected function toJsonSerialize(): array
105    {
106        return [
107            'index' => $this->index,
108        ];
109    }
110}