Completed
Push — master ( ce62ef...a1e916 )
by Thomas
02:37
created

ConstantsPart::setConstant()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
crap 2
1
<?php
2
namespace gossi\codegen\model\parts;
3
4
use gossi\codegen\model\PhpConstant;
5
6
/**
7
 * Constants part
8
 *
9
 * For all models that can contain constants
10
 *
11
 * @author Thomas Gossmann
12
 */
13
trait ConstantsPart {
14
15
	/** @var PhpConstant[] */
16
	private $constants = [];
17
18
	/**
19
	 * Sets a collection of constants
20
	 *
21
	 * @param array|PhpConstant[] $constants
22
	 * @return $this
23
	 */
24 5
	public function setConstants(array $constants) {
25 5
		$normalizedConstants = [];
26 5
		foreach ($constants as $name => $value) {
27 2
			if ($value instanceof PhpConstant) {
28 1
				$name = $value->getName();
29 1
			} else {
30 2
				$constValue = $value;
31 2
				$value = new PhpConstant($name);
32 2
				$value->setValue($constValue);
33
			}
34
35 2
			$normalizedConstants[$name] = $value;
36 5
		}
37
38 5
		$this->constants = $normalizedConstants;
39
40 5
		return $this;
41
	}
42
43
	/**
44
	 * Adds a constant
45
	 *
46
	 * @param string|PhpConstant $nameOrConstant constant name or instance
47
	 * @param string $value
48
	 * @return $this
49
	 */
50 11
	public function setConstant($nameOrConstant, $value = null) {
51 11
		if ($nameOrConstant instanceof PhpConstant) {
52 6
			$name = $nameOrConstant->getName();
53 6
			$constant = $nameOrConstant;
54 6
		} else {
55 6
			$name = $nameOrConstant;
56 6
			$constant = new PhpConstant($nameOrConstant);
57 6
			$constant->setValue($value);
58
		}
59
60 11
		$this->constants[$name] = $constant;
61
62 11
		return $this;
63
	}
64
65
	/**
66
	 * Removes a constant
67
	 *
68
	 * @param string|PhpConstant $nameOrConstant constant name
69
	 * @throws \InvalidArgumentException If the constant cannot be found
70
	 * @return $this
71
	 */
72 2
	public function removeConstant($nameOrConstant) {
73 2
		if ($nameOrConstant instanceof PhpConstant) {
74 1
			$nameOrConstant = $nameOrConstant->getName();
75 1
		}
76
77 2
		if (!array_key_exists($nameOrConstant, $this->constants)) {
78 1
			throw new \InvalidArgumentException(sprintf('The constant "%s" does not exist.', $nameOrConstant));
79
		}
80
81 1
		unset($this->constants[$nameOrConstant]);
82
83 1
		return $this;
84
	}
85
86
	/**
87
	 * Checks whether a constant exists
88
	 *
89
	 * @param string|PhpConstant $nameOrConstant
90
	 * @return bool
91
	 */
92 2
	public function hasConstant($nameOrConstant) {
93 2
		if ($nameOrConstant instanceof PhpConstant) {
94 1
			$nameOrConstant = $nameOrConstant->getName();
95 1
		}
96 2
		return array_key_exists($nameOrConstant, $this->constants);
97
	}
98
99
	/**
100
	 * Returns a constant.
101
	 *
102
	 * @param string|PhpConstant $nameOrConstant
103
	 * @throws \InvalidArgumentException If the constant cannot be found
104
	 * @return PhpConstant
105
	 */
106 5
	public function getConstant($nameOrConstant) {
107 5
		if ($nameOrConstant instanceof PhpConstant) {
108 1
			$nameOrConstant = $nameOrConstant->getName();
109 1
		}
110
111 5
		if (!isset($this->constants[$nameOrConstant])) {
112 2
			throw new \InvalidArgumentException(sprintf('The constant "%s" does not exist.', $nameOrConstant));
113
		}
114
115 4
		return $this->constants[$nameOrConstant];
116
	}
117
118
	/**
119
	 * Returns all constants
120
	 *
121
	 * @return PhpConstant[]
122
	 */
123 13
	public function getConstants() {
124 13
		return $this->constants;
125
	}
126
127
	/**
128
	 * Returns all constant names
129
	 *
130
	 * @return string[]
131
	 */
132 1
	public function getConstantNames() {
133 1
		return array_keys($this->constants);
134
	}
135
136
	/**
137
	 * Clears all constants
138
	 *
139
	 * @return $this
140
	 */
141 1
	public function clearConstants() {
142 1
		$this->constants = [];
143
144 1
		return $this;
145
	}
146
}
147