1: <?php namespace Khill\Fontawesome;
2:
3: /**
4: * FontAwesomeList adds the ability to create unordered lists with icons
5: *
6: * @package FontAwesomePHP
7: * @author Kevin Hill <kevinkhill@gmail.com>
8: * @version 1.0b1
9: * @access public
10: * @see http://kevinkhill.github.io/FontAwesomePHP
11: */
12:
13: use Khill\Fontawesome\Exceptions\BadLabelException;
14: use Khill\Fontawesome\Exceptions\CollectionIconException;
15: use Khill\Fontawesome\Exceptions\IncompleteStackException;
16:
17: class FontAwesomeList {
18:
19: /**
20: * Html string template to build the list
21: */
22: const UL_HTML = '<ul class="fa-ul">%s</ul>';
23:
24: /**
25: * Html string template to build the list items
26: */
27: const LI_HTML = '<li>%s%s</li>';
28:
29: /**
30: * Html string template to build the icon
31: */
32: const ICON_HTML = '<i class="fa %s"></i>';
33:
34: /**
35: * Classes to be applied to the list
36: *
37: * @var array
38: */
39: private $classes = array();
40:
41: /**
42: * Lines in the list
43: *
44: * @var array
45: */
46: private $lines = array();
47:
48: /**
49: * Name of the icon to apply to entire list
50: *
51: * @var string
52: */
53: private $iconLabel = '';
54:
55: /**
56: * Full list
57: *
58: * @var array
59: */
60: private $fullList = array();
61:
62: /**
63: * Assigns the name to the icon
64: *
65: * @param string $icon Icon label
66: * @return Khill\Fontawesome\FontAwesome FontAwesome object
67: */
68: public function __construct($icon = '')
69: {
70: $this->_setIcon($icon);
71: }
72:
73: /**
74: * Outputs the FontAwesomeList object as an HTML string
75: *
76: * @access public
77: * @throws Khill\Fontawesome\Exceptions\IncompleteListException If No default icon was setempty string
78: * @return string HTML string of list
79: */
80: public function output()
81: {
82: $listItems = '';
83:
84: if(is_array($this->fullList) && count($this->fullList) > 0)
85: {
86: foreach($this->fullList as $icon => $line)
87: {
88: $lineIcon = $this->_buildIcon($icon);
89: $listItems .= sprintf(self::LI_HTML, $lineIcon, $line);
90: }
91: } else {
92: foreach($this->lines as $line)
93: {
94: if(isset($this->defaultIcon))
95: {
96: $icon = $this->_buildIcon($this->defaultIcon);
97: } else {
98: throw new IncompleteListException('No default icon was defined.');
99: }
100:
101: $listItems .= sprintf(self::LI_HTML, $icon, $line);
102: }
103: }
104:
105: return sprintf(self::UL_HTML, $listItems);
106: }
107:
108: /**
109: * Adds extra classes to list
110: *
111: * @access public
112: * @param string $class CSS class
113: * @throws Khill\Fontawesome\Exceptions\IncompleteListException If $class is not a non empty string
114: * @return Khill\Fontawesome\FontAwesomeList FontAwesomeList object
115: */
116: public function addClass($class)
117: {
118: if(is_string($class))
119: {
120: $this->classes[] = $class;
121: } else {
122: throw new BadLabelException('Additional classes must be a non empty string.');
123: }
124:
125: return $this;
126: }
127:
128: /**
129: * Add an item to the list
130: *
131: * @access public
132: * @param string $line Line to add to the list
133: * @throws Khill\Fontawesome\Exceptions\IncompleteListException If $line is not a non empty string
134: * @return Khill\Fontawesome\FontAwesomeList FontAwesomeList object
135: */
136: public function addItem($line)
137: {
138: if(is_string($line))
139: {
140: $this->lines[] = $line;
141: } else {
142: throw new IncompleteListException('List item must be a non empty string.');
143: }
144:
145: return $this;
146: }
147:
148: /**
149: * Add multiple items to list
150: *
151: * @access public
152: * @param string $lineArray Array of lines to add to list
153: * @throws Khill\Fontawesome\Exceptions\IncompleteListException If $lineArray is not an array
154: * @return Khill\Fontawesome\FontAwesomeList FontAwesomeList object
155: */
156: public function addItems($lineArray)
157: {
158: if(is_array($lineArray))
159: {
160: foreach($lineArray as $line)
161: {
162: $this->addItem($line);
163: }
164: } else {
165: throw new IncompleteListException('You must pass an array of non empty strings.');
166: }
167:
168: return $this;
169: }
170:
171: /**
172: * Sets the default icon to be used in the list
173: *
174: * @access public
175: * @param string $icon Icon label
176: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
177: * @return void
178: */
179: public function setDefaultIcon($icon)
180: {
181: $this->_setIcon($icon);
182: }
183:
184: /**
185: * Sets the entire list with multiple icons
186: *
187: * @access public
188: * @param array $listItems Array of icons and list items
189: * @return void
190: */
191: public function setListItems($listItems)
192: {
193: if(is_array($listItems) && $this->_testAssocArray($listItems))
194: {
195: $this->fullList = $listItems;
196: } else {
197: throw new IncompleteListException('Must pass array with keys as icon names and values as lines for list.');
198: }
199:
200: }
201:
202:
203: /**
204: * Sets icon label
205: *
206: * @access private
207: * @param string $icon Icon label
208: * @return void
209: */
210: private function _setIcon($icon)
211: {
212: if(is_string($icon))
213: {
214: if( ! empty($icon))
215: {
216: $this->defaultIcon = $icon;
217: }
218: } else {
219: throw new BadLabelException('Icon label must be a string.');
220: }
221: }
222:
223: /**
224: * Builds the icon from the template
225: *
226: * @access private
227: * @return void
228: */
229: private function _buildIcon($iconLabel)
230: {
231: $classes = 'fa-' . $iconLabel;
232: /*
233: if( ! empty($this->classes))
234: {
235: foreach($this->classes as $class)
236: {
237: $classes .= ' ' . $class;
238: }
239: }
240: */
241: return sprintf(self::ICON_HTML, $classes);
242: }
243:
244: /**
245: * Builds the stack from the template
246: *
247: * @access private
248: * @return void
249: */
250: private function _buildList()
251: {
252: $classes = 'fa-stack';
253:
254: $this->stackBottom = $this->_buildIcon();
255: $this->_setStackPositions();
256:
257: if( ! empty($this->stackClasses))
258: {
259: foreach($this->stackClasses as $class)
260: {
261: $classes .= ' ' . $class;
262: }
263: }
264:
265: return sprintf(self::STACK_HTML, $classes, $this->stackTop, $this->stackBottom);
266: }
267:
268: /**
269: * Tests if array keys are strings
270: *
271: * @param array ARray to test
272: * @return boolean True if keys are strings, false if not
273: */
274: private function _testAssocArray($array)
275: {
276: return (bool) count(array_filter(array_keys($array), 'is_string'));
277: }
278:
279: /**
280: * Resets the FontAwesomeList class
281: *
282: * @access private
283: * @return void
284: */
285: private function _reset()
286: {
287: $this->iconLabel = '';
288: $this->classes = array();
289: }
290:
291: }
292: