Completed
Push — master ( 880828...8688d8 )
by Thomas
02:37
created

AbstractModel::hasAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace gossi\codegen\model;
3
4
/**
5
 * Parent of all models
6
 *
7
 * @author Thomas Gossmann
8
 */
9
abstract class AbstractModel {
10
11
	/** @var array */
12
	private $attributes = [];
13
14
	/** @var string */
15
	protected $description;
16
17
	/**
18
	 * Sets a custom user attribute
19
	 *
20
	 * @deprecated See: https://github.com/gossi/php-code-generator/issues/29
21
	 * @param string $key
22
	 * @param mixed $value
23
	 * @return $this
24
	 */
25
	public function setAttribute($key, $value) {
26
		$this->attributes[$key] = $value;
27
28
		return $this;
29
	}
30
31
	/**
32
	 * Removes and returns a custom user attribute
33
	 *
34
	 * @deprecated See: https://github.com/gossi/php-code-generator/issues/29
35
	 * @param string $key
36
	 * @return mixed
37
	 */
38
	public function removeAttribute($key) {
39
		$val = $this->attributes[$key];
40
		unset($this->attributes[$key]);
41
		return $val;
42
	}
43
44
	/**
45
	 * Returns a custom user attribute
46
	 *
47
	 * @deprecated See: https://github.com/gossi/php-code-generator/issues/29
48
	 * @param string $key
49
	 * @throws \InvalidArgumentException if the key cannot be found
50
	 * @return mixed
51
	 */
52
	public function getAttribute($key) {
53
		if (!isset($this->attributes[$key])) {
54
			throw new \InvalidArgumentException(sprintf('There is no attribute named "%s".', $key));
55
		}
56
57
		return $this->attributes[$key];
58
	}
59
60
	/**
61
	 * Returns a custom user attribute or the default value if the attribute doesn't exist
62
	 *
63
	 * @deprecated See: https://github.com/gossi/php-code-generator/issues/29
64
	 * @param string $key
65
	 * @param mixed $default
66
	 * @return mixed
67
	 */
68
	public function getAttributeOrElse($key, $default) {
69
		if (!isset($this->attributes[$key])) {
70
			return $default;
71
		}
72
73
		return $this->attributes[$key];
74
	}
75
76
	/**
77
	 * Checks whether an attribute exists
78
	 *
79
	 * @deprecated See: https://github.com/gossi/php-code-generator/issues/29
80
	 * @param string $key
81
	 * @return bool
82
	 */
83
	public function hasAttribute($key) {
84
		return isset($this->attributes[$key]);
85
	}
86
87
	/**
88
	 * Sets custom user attributes
89
	 *
90
	 * @deprecated See: https://github.com/gossi/php-code-generator/issues/29
91
	 * @param array $attrs
92
	 * @return $this
93
	 */
94
	public function setAttributes(array $attrs) {
95
		$this->attributes = $attrs;
96
97
		return $this;
98
	}
99
100
	/**
101
	 * Returns all custom user attributes
102
	 *
103
	 * @deprecated See: https://github.com/gossi/php-code-generator/issues/29
104
	 * @return array
105
	 */
106
	public function getAttributes() {
107
		return $this->attributes;
108
	}
109
110
	/**
111
	 * Returns this description
112
	 *
113
	 * @return string
114
	 */
115 24
	public function getDescription() {
116 24
		return $this->description;
117
	}
118
119
	/**
120
	 * Sets the description, which will also be used when generating a docblock
121
	 *
122
	 * @param string|array $description
123
	 * @return $this
124
	 */
125 19
	public function setDescription($description) {
126 19
		if (is_array($description)) {
127 1
			$description = implode("\n", $description);
128 1
		}
129 19
		$this->description = $description;
130 19
		return $this;
131
	}
132
133
}
134