1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Object; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
trait CategoryTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $categoryItemType; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \Charcoal\Object\CategorizableInterface[]|array |
20
|
|
|
*/ |
21
|
|
|
private $categoryItems; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var integer |
25
|
|
|
*/ |
26
|
|
|
private $numCategoryItems; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $type The category item type. |
30
|
|
|
* @throws InvalidArgumentException If the type argument is not a string. |
31
|
|
|
* @return self |
32
|
|
|
*/ |
33
|
|
|
public function setCategoryItemType($type) |
34
|
|
|
{ |
35
|
|
|
if (!is_string($type)) { |
36
|
|
|
throw new InvalidArgumentException( |
37
|
|
|
'Item type must be a string.' |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
$this->categoryItemType = $type; |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @throws Exception If no item type was previously set. |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function getCategoryItemType() |
49
|
|
|
{ |
50
|
|
|
if ($this->categoryItemType === null) { |
51
|
|
|
throw new Exception( |
52
|
|
|
'Item type is unset. Set item type before calling getter.' |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
return $this->categoryItemType; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Alias of {@see self::getCategoryItemType()}. |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function categoryItemType() |
64
|
|
|
{ |
65
|
|
|
return $this->getCategoryItemType(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Gets the number of items, directly within this category. |
70
|
|
|
* |
71
|
|
|
* @return integer |
72
|
|
|
*/ |
73
|
|
|
public function getNumCategoryItems() |
74
|
|
|
{ |
75
|
|
|
if ($this->numCategoryItems === null) { |
76
|
|
|
$items = $this->getCategoryItems(); |
77
|
|
|
if (is_array($items) || ($items instanceof \Countable)) { |
78
|
|
|
$count = count($items); |
79
|
|
|
} else { |
80
|
|
|
$count = 0; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->numCategoryItems = $count; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->numCategoryItems; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Alias of {@see self::getNumCategoryItems()}. |
91
|
|
|
* |
92
|
|
|
* @return integer |
93
|
|
|
*/ |
94
|
|
|
public function numCategoryItems() |
95
|
|
|
{ |
96
|
|
|
return $this->getNumCategoryItems(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Gets wether the category has any items, directly within it. |
101
|
|
|
* |
102
|
|
|
* @return boolean |
103
|
|
|
*/ |
104
|
|
|
public function hasCategoryItems() |
105
|
|
|
{ |
106
|
|
|
$numItems = $this->getNumCategoryItems(); |
107
|
|
|
return ($numItems > 0); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Retrieves the category items, directly within it. |
112
|
|
|
* |
113
|
|
|
* @return \Charcoal\Object\CategorizableInterface[]|array A list of `CategorizableInterface` objects |
114
|
|
|
*/ |
115
|
|
|
public function getCategoryItems() |
116
|
|
|
{ |
117
|
|
|
if ($this->categoryItems === null) { |
118
|
|
|
$this->categoryItems = $this->loadCategoryItems(); |
119
|
|
|
$this->numCategoryItems = null; |
120
|
|
|
} |
121
|
|
|
return $this->categoryItems; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Loads the category items (directly within it). |
126
|
|
|
* |
127
|
|
|
* This method is abstract so must be reimplemented. |
128
|
|
|
* Typically, a class would use a CollectionLoader to load the category items. |
129
|
|
|
* |
130
|
|
|
* @return \Charcoal\Object\CategorizableInterface[]|array |
131
|
|
|
*/ |
132
|
|
|
abstract protected function loadCategoryItems(); |
133
|
|
|
} |
134
|
|
|
|