Completed
Push — master ( f361bc...f7f196 )
by Mathieu
03:24
created

CategoryTrait::categoryItemType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
     * @param string $type The category item type.
25
     * @throws InvalidArgumentException If the type argument is not a string.
26
     * @return self
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
     * @throws Exception If no item type was previously set.
41
     * @return string
42
     */
43
    public function getCategoryItemType()
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
     * Legacy support for old-style getter.
55
     *
56
     * @return string
57
     */
58
    public function categoryItemType()
59
    {
60
        return $this->categoryItemType();
61
    }
62
63
    /**
64
     * Gets the number of items, directly within this category.
65
     *
66
     * @return integer
67
     */
68
    public function numCategoryItems()
69
    {
70
        $items = $this->categoryItems();
71
        return count($items);
72
    }
73
74
    /**
75
     * Gets wether the category has any items, directly within it.
76
     *
77
     * @return boolean
78
     */
79
    public function hasCategoryItems()
80
    {
81
        $numItems = $this->numCategoryItems();
82
        return ($numItems > 0);
83
    }
84
85
    /**
86
     * Retrieves the category items, directly within it.
87
     *
88
     * @return \Charcoal\Object\CategorizableInterface[]|array A list of `CategorizableInterface` objects
89
     */
90
    public function categoryItems()
91
    {
92
        if ($this->categoryItems === null) {
93
            $this->categoryItems = $this->loadCategoryItems();
94
        }
95
        return $this->categoryItems;
96
    }
97
98
    /**
99
     * Loads the category items (directly within it).
100
     *
101
     * This method is abstract so must be reimplemented.
102
     * Typically, a class would use a CollectionLoader to load the category items.
103
     *
104
     * @return \Charcoal\Object\CategorizableInterface[]|array
105
     */
106
    abstract protected function loadCategoryItems();
107
}
108