1: <?php
2:
3: namespace Charcoal\Object;
4:
5: use Exception;
6: use InvalidArgumentException;
7:
8: 9: 10:
11: trait CategoryTrait
12: {
13: 14: 15:
16: private $categoryItemType;
17:
18: 19: 20:
21: private $categoryItems;
22:
23: 24: 25: 26: 27:
28: public function setCategoryItemType($type)
29: {
30: if (!is_string($type)) {
31: throw new InvalidArgumentException(
32: 'Item type must be a string.'
33: );
34: }
35: $this->categoryItemType = $type;
36: return $this;
37: }
38:
39: 40: 41: 42:
43: public function categoryItemType()
44: {
45: if ($this->categoryItemType === null) {
46: throw new Exception(
47: 'Item type is unset. Set item type before calling getter.'
48: );
49: }
50: return $this->categoryItemType;
51: }
52:
53: 54: 55:
56: public function numCategoryItems()
57: {
58: $items = $this->categoryItems();
59: return count($items);
60: }
61:
62: 63: 64:
65: public function hasCategoryItems()
66: {
67: $numItems = $this->numCategoryItems();
68: return ($numItems > 0);
69: }
70:
71: 72: 73:
74: public function categoryItems()
75: {
76: if ($this->categoryItems === null) {
77: $this->categoryItems = $this->loadCategoryItems();
78: }
79: return $this->categoryItems;
80: }
81:
82: 83: 84:
85: abstract public function loadCategoryItems();
86: }
87: