Completed
Push — master ( d075bd...c77af7 )
by Thomas
03:19
created

ValuePart   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 152
ccs 33
cts 33
cp 1
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
B isPrimitive() 0 8 6
A setValue() 0 9 2
A unsetValue() 0 6 1
A getValue() 0 3 1
A hasValue() 0 3 2
A isExpression() 0 3 1
A setExpression() 0 6 1
A getExpression() 0 3 1
A unsetExpression() 0 6 1
A setDefaultValue() 0 3 1
A unsetDefaultValue() 0 3 1
A getDefaultValue() 0 3 1
A hasDefaultValue() 0 3 1
1
<?php
2
namespace gossi\codegen\model\parts;
3
4
use gossi\codegen\model\PhpConstant;
5
6
/**
7
 * Value part
8
 *
9
 * For all models that have a value (or expression)
10
 *
11
 * @author Thomas Gossmann
12
 */
13
trait ValuePart {
14
15
	/** @var mixed */
16
	private $value;
17
18
	/** @var bool */
19
	private $hasValue = false;
20
21
	/** @var string */
22
	private $expression;
23
24
	/** @var bool */
25
	private $hasExpression = false;
26
27
	/**
28
	 * @deprecated use `setValue()` instead
29
	 * @param mixed $value
30
	 * @return $this
31
	 */
32
	public function setDefaultValue($value) {
33
		return $this->setValue($value);
34
	}
35
36
	/**
37
	 * @deprecated use `unsetValue()` instead
38
	 * @return $this
39
	 */
40
	public function unsetDefaultValue() {
41
		return $this->unsetValue();
42
	}
43
44
	/**
45
	 * @deprecated use `getValue()` instead
46
	 * @return mixed
47
	 */
48
	public function getDefaultValue() {
49
		return $this->getValue();
50
	}
51
52
	/**
53
	 * @deprecated use `hasValue()` instead
54
	 * @return bool
55
	 */
56
	public function hasDefaultValue() {
57
		return $this->hasValue();
58
	}
59
	
60
	/**
61
	 * Returns whether the given value is a primitive
62
	 * 
63
	 * @param mixed $value
64
	 * @return boolean
65
	 */
66 28
	private function isPrimitive($value) {
67 28
		return (is_string($value)
68 23
			|| is_int($value)
69 23
			|| is_float($value)
70 23
			|| is_bool($value)
71 23
			|| is_null($value)
72 28
			|| ($value instanceof PhpConstant));
73
	}
74
75
	/**
76
	 * Sets the value
77
	 *
78
	 * @param string|integer|float|bool|null|PhpConstant $value
79
	 * @throws \InvalidArgumentException if the value is not an accepted primitve
80
	 * @return $this
81
	 */
82 28
	public function setValue($value) {
83 28
		if (!$this->isPrimitive($value)) {
84 1
			throw new \InvalidArgumentException('Use setValue() only for primitives and PhpConstant, anyway use setExpression() instead.');
85
		}
86 27
		$this->value = $value;
87 27
		$this->hasValue = true;
88
89 27
		return $this;
90
	}
91
92
	/**
93
	 * Unsets the value
94
	 *
95
	 * @return $this
96
	 */
97 1
	public function unsetValue() {
98 1
		$this->value = null;
99 1
		$this->hasValue = false;
100
101 1
		return $this;
102
	}
103
104
	/**
105
	 * Returns the value
106
	 *
107
	 * @return string|integer|float|bool|null|PhpConstant
108
	 */
109 18
	public function getValue() {
110 18
		return $this->value;
111
	}
112
113
	/**
114
	 * Checks whether a value or expression is set
115
	 *
116
	 * @return bool
117
	 */
118 21
	public function hasValue() {
119 21
		return $this->hasValue || $this->hasExpression;
120
	}
121
122
	/**
123
	 * Returns whether an expression is set
124
	 *
125
	 * @return bool
126
	 */
127 9
	public function isExpression() {
128 9
		return $this->hasExpression;
129
	}
130
131
	/**
132
	 * Sets an expression
133
	 *
134
	 * @param string $expr
135
	 * @return $this
136
	 */
137 11
	public function setExpression($expr) {
138 11
		$this->expression = $expr;
139 11
		$this->hasExpression = true;
140
141 11
		return $this;
142
	}
143
144
	/**
145
	 * Returns the expression
146
	 *
147
	 * @return string
148
	 */
149 8
	public function getExpression() {
150 8
		return $this->expression;
151
	}
152
153
	/**
154
	 * Unsets the expression
155
	 *
156
	 * @return $this
157
	 */
158 1
	public function unsetExpression() {
159 1
		$this->expression = null;
160 1
		$this->hasExpression = false;
161
162 1
		return $this;
163
	}
164
}
165