Commit | Line | Data |
---|---|---|
dcc2332d | 1 | <?php |
a9229942 | 2 | |
dcc2332d | 3 | namespace wcf\data\smiley\category; |
a9229942 | 4 | |
dcc2332d | 5 | use wcf\data\category\AbstractDecoratedCategory; |
a9229942 | 6 | use wcf\data\ITraversableObject; |
7a23a706 | 7 | use wcf\data\smiley\Smiley; |
dcc2332d | 8 | use wcf\data\smiley\SmileyCache; |
dcc2332d | 9 | use wcf\system\exception\SystemException; |
5fb04245 | 10 | use wcf\system\WCF; |
dcc2332d MW |
11 | |
12 | /** | |
13 | * Represents a smiley category. | |
a9229942 TD |
14 | * |
15 | * @author Tim Duesterhus, Alexander Ebert | |
16 | * @copyright 2001-2019 WoltLab GmbH | |
17 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
18 | * @package WoltLabSuite\Core\Data\Smiley\Category | |
19 | * | |
20 | * @method SmileyCategory[] getChildCategories() | |
21 | * @method SmileyCategory[] getAllChildCategories() | |
22 | * @method SmileyCategory getParentCategory() | |
23 | * @method SmileyCategory[] getParentCategories() | |
24 | * @method static SmileyCategory|null getCategory($categoryID) | |
dcc2332d | 25 | */ |
a9229942 TD |
26 | class SmileyCategory extends AbstractDecoratedCategory implements \Countable, ITraversableObject |
27 | { | |
28 | /** | |
29 | * current iterator index | |
30 | * @var int | |
31 | */ | |
32 | protected $index = 0; | |
33 | ||
34 | /** | |
35 | * list of index to object relation | |
36 | * @var int[] | |
37 | */ | |
38 | protected $indexToObject; | |
39 | ||
40 | /** | |
41 | * list of assigned smilies | |
42 | * @var Smiley[] | |
43 | */ | |
44 | public $smilies; | |
45 | ||
46 | /** | |
47 | * Loads associated smilies from cache. | |
48 | */ | |
49 | public function loadSmilies() | |
50 | { | |
51 | if ($this->smilies === null) { | |
52 | $this->smilies = SmileyCache::getInstance()->getCategorySmilies($this->categoryID ?: null); | |
53 | $this->indexToObject = \array_keys($this->smilies); | |
54 | } | |
55 | } | |
56 | ||
57 | /** | |
58 | * @inheritDoc | |
59 | */ | |
60 | public function count() | |
61 | { | |
62 | return \count($this->smilies); | |
63 | } | |
64 | ||
65 | /** | |
66 | * @inheritDoc | |
67 | * @return Smiley | |
68 | */ | |
69 | public function current() | |
70 | { | |
71 | $objectID = $this->indexToObject[$this->index]; | |
72 | ||
73 | return $this->smilies[$objectID]; | |
74 | } | |
75 | ||
76 | /** | |
77 | * CAUTION: This methods does not return the current iterator index, | |
78 | * rather than the object key which maps to that index. | |
79 | * | |
80 | * @see \Iterator::key() | |
81 | */ | |
82 | public function key() | |
83 | { | |
84 | return $this->indexToObject[$this->index]; | |
85 | } | |
86 | ||
87 | /** | |
88 | * @inheritDoc | |
89 | */ | |
90 | public function next() | |
91 | { | |
92 | $this->index++; | |
93 | } | |
94 | ||
95 | /** | |
96 | * @inheritDoc | |
97 | */ | |
98 | public function rewind() | |
99 | { | |
100 | $this->index = 0; | |
101 | } | |
102 | ||
103 | /** | |
104 | * @inheritDoc | |
105 | */ | |
106 | public function valid() | |
107 | { | |
108 | return isset($this->indexToObject[$this->index]); | |
109 | } | |
110 | ||
111 | /** | |
112 | * @inheritDoc | |
113 | */ | |
114 | public function seek($index) | |
115 | { | |
116 | $this->index = $index; | |
117 | ||
118 | if (!$this->valid()) { | |
119 | throw new \OutOfBoundsException(); | |
120 | } | |
121 | } | |
122 | ||
123 | /** | |
124 | * @inheritDoc | |
125 | */ | |
126 | public function seekTo($objectID) | |
127 | { | |
128 | $this->index = \array_search($objectID, $this->indexToObject); | |
129 | ||
130 | if ($this->index === false) { | |
131 | throw new SystemException("object id '" . $objectID . "' is invalid"); | |
132 | } | |
133 | } | |
134 | ||
135 | /** | |
136 | * @inheritDoc | |
137 | * @return Smiley|null | |
138 | */ | |
139 | public function search($objectID) | |
140 | { | |
141 | try { | |
142 | $this->seekTo($objectID); | |
143 | ||
144 | return $this->current(); | |
145 | } catch (SystemException $e) { | |
c0b28aa2 | 146 | return null; |
a9229942 TD |
147 | } |
148 | } | |
149 | ||
150 | /** | |
151 | * Returns the category's name. | |
152 | * | |
153 | * @return string | |
154 | */ | |
155 | public function __toString() | |
156 | { | |
157 | return WCF::getLanguage()->get($this->title); | |
158 | } | |
dcc2332d | 159 | } |