WordPoints_Container::_add()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 18
rs 9.2
1
<?php
2
3
/**
4
 * Abstract classes to act as item containers.
5
 *
6
 * @package WordPointsOrg
7
 * @since 1.0.0
8
 */
9
10
/**
11
 * Abstract class to be a container for items.
12
 *
13
 * @since 1.0.0
14
 */
15
abstract class WordPoints_Container {
16
17
	/**
18
	 * The items in this container.
19
	 *
20
	 * @since 1.0.0
21
	 *
22
	 * @type array $items
23
	 */
24
	protected $items = array();
25
26
	/**
27
	 * A class to instantiate new items as.
28
	 *
29
	 * @since 1.0.0
30
	 *
31
	 * @type string $item_class
32
	 */
33
	protected $item_class;
34
35
	/**
36
	 * Add an item to the collection.
37
	 *
38
	 * @since 1.0.0
39
	 *
40
	 * @param string $slug The unique identifier for this item.
41
	 * @param mixed  $item The item being added.
42
	 *
43
	 * @return bool|object Whether the item was added successfully, or the item
44
	 *                     object if self::$item_class is used.
45
	 */
46
	protected function _add( $slug, $item, $class = null ) {
47
48
		if ( $this->_contains( $slug ) ) {
49
			return false;
50
		}
51
52
		if ( isset( $class ) ) {
53
			$this->items[ $slug ] = new $class( $slug, $item );
54
			return $this->items[ $slug ];
55
		} elseif ( isset( $this->item_class ) ) {
56
			$this->items[ $slug ] = new $this->item_class( $slug, $item );
57
			return $this->items[ $slug ];
58
		}
59
60
		$this->items[ $slug ] = $item;
61
62
		return true;
63
	}
64
65
	/**
66
	 * Remove an item from the collection.
67
	 *
68
	 * @since 1.0.0
69
	 *
70
	 * @param string $slug The slug of the item to remove.
71
	 */
72
	protected function _remove( $slug ) {
73
		unset( $this->items[ $slug ] );
74
	}
75
76
	/**
77
	 * Check if this collection contains a specific item.
78
	 *
79
	 * @since 1.0.0
80
	 *
81
	 * @param string $slug The slug of the item to check for.
82
	 *
83
	 * @return bool Whether the item is present.
84
	 */
85
	protected function _contains( $slug ) {
86
		return isset( $this->items[ $slug ] );
87
	}
88
89
	/**
90
	 * Get an item from the collection.
91
	 *
92
	 * @since 1.0.0
93
	 *
94
	 * @param string $slug The slug of the item to get. If ommitted, all items are
95
	 *                     returned.
96
	 *
97
	 * @return mixed The item or the entire collection if $slug is ommitted. False
98
	 *               if the item does not exist.
99
	 */
100
	protected function _get( $slug = null ) {
101
102
		if ( ! isset( $slug ) ) {
103
			return $this->items;
104
		}
105
106
		if ( ! $this->_contains( $slug ) ) {
107
			return false;
108
		}
109
110
		return $this->items[ $slug ];
111
	}
112
}
113
114
/**
115
 * Class to be a item container object.
116
 *
117
 * @since 1.0.0
118
 */
119
class WordPoints_Container_Object extends WordPoints_Container {
120
121
	/**
122
	 * @since 1.0.0
123
	 *
124
	 * @see WordPoints_Container::_add()
125
	 */
126
	public function add( $slug, $item ) {
127
		return $this->_add( $slug, $item );
128
	}
129
130
	/**
131
	 * @since 1.0.0
132
	 *
133
	 * @see WordPoints_Container::_remove()
134
	 */
135
	public function remove( $slug ) {
136
		return $this->_remove( $slug );
137
	}
138
139
	/**
140
	 * @since 1.0.0
141
	 *
142
	 * @see WordPoints_Container::_contains()
143
	 */
144
	public function contains( $slug ) {
145
		return $this->_contains( $slug );
146
	}
147
148
	/**
149
	 * @since 1.0.0
150
	 *
151
	 * @see WordPoints_Container::_get()
152
	 */
153
	public function get( $slug = null ) {
154
		return $this->_get( $slug );
155
	}
156
}
157
158
/**
159
 * Class to be a static container for items.
160
 *
161
 * Note that this API cannot be fully implemented for static classes until PHP 5.3.
162
 *
163
 * @since 1.0.0
164
 */
165
class WordPoints_Container_Static extends WordPoints_Container {
166
167
	/**
168
	 * The container instance.
169
	 *
170
	 * @since 1.0.0
171
	 *
172
	 * @type WordPoints_Container_Static $instance
173
	 */
174
	protected static $instance;
175
176
	/**
177
	 * The name of the child class which is extending this one.
178
	 *
179
	 * @since 1.0.0
180
	 *
181
	 * @type string $child_class
182
	 */
183
	protected static $child_class = __CLASS__;
184
185
	/**
186
	 * Construction is not allowed.
187
	 *
188
	 * @since 1.0.0
189
	 */
190
	protected function __construct() {}
191
192
	/**
193
	 * We sleep the perpetual sleep.
194
	 *
195
	 * @since 1.0.0
196
	 */
197
	private function __wakeup() {}
198
199
	/**
200
	 * There can be no others.
201
	 *
202
	 * @since 1.0.0
203
	 */
204
	private function __clone() {}
205
}
206
207
// EOF
208