1: <?php namespace Khill\Fontawesome;
2:
3: /**
4: * FontAwesomePHP is a library that wraps the FontAwesome icon set into easy to use php methods
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: use Khill\Fontawesome\Exceptions\IncompleteListException;
17:
18: class FontAwesome {
19:
20: /**
21: * HTML Link tag to the FontAwesome CDN
22: */
23: const CDN_LINK = '<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.1/css/font-awesome.min.css" rel="stylesheet">';
24:
25: /**
26: * Html string template to build the icon
27: */
28: const ICON_HTML = '<i class="fa %s"></i>';
29:
30: /**
31: * Html string template to build the icon stack
32: */
33: const STACK_HTML = '<span class="%s">%s%s</span>';
34:
35: /**
36: * Name of the icon
37: *
38: * @var string
39: */
40: private $iconLabel = '';
41:
42: /**
43: * Classes to be applied to the icon
44: *
45: * @var array
46: */
47: private $classes = array();
48:
49: /**
50: * Store a collection of icons
51: *
52: * @var array
53: */
54: public $collection = array();
55:
56: /**
57: * Stores icon stack
58: *
59: * @var string
60: */
61: public $stack;
62:
63: /**
64: * Status of stacking or regular icon
65: *
66: * @var boolean
67: */
68: private $stacking = false;
69:
70: /**
71: * Stores unordered list
72: *
73: * @var string
74: */
75: public $list;
76:
77: /**
78: * HTML link to the FontAwesome CSS file through the bootstrapcdn
79: *
80: * @see http://www.bootstrapcdn.com/
81: * @return string HTML link element
82: */
83: public static function css()
84: {
85: return self::CDN_LINK;
86: }
87:
88: /**
89: * Assigns the name to the icon
90: *
91: * @param string $icon Icon label
92: * @return Khill\Fontawesome\FontAwesome FontAwesome object
93: */
94: public function __construct($icon = '')
95: {
96: $this->_setIcon($icon);
97: }
98:
99: /**
100: * Outputs the FontAwesome object as an HTML string
101: *
102: * @access public
103: * @return string HTML string of icon or stack
104: */
105: public function __toString()
106: {
107: if(is_a($this->stack, 'Khill\Fontawesome\FontAwesomeStack'))
108: {
109: $output = $this->stack->output();
110: } elseif(is_a($this->list, 'Khill\Fontawesome\FontAwesomeList')) {
111: $output = $this->list->output();
112: } else {
113: $output = $this->_buildIcon();
114: }
115:
116: $this->_reset();
117:
118: return $output;
119: }
120:
121: /**
122: * Stores icon to be rendered later
123: *
124: * @access public
125: * @param string $label Label of icon to save in collection
126: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $label is not a string
127: * @throws Khill\Fontawesome\Exceptions\CollectionIconException If store() method called without defining an icon
128: * @return void
129: */
130: public function store($label)
131: {
132: if(empty($this->iconLabel))
133: {
134: throw new CollectionIconException('There was no icon defined to store.');
135: } else {
136: if(is_string($label))
137: {
138: if( ! empty($label))
139: {
140: $this->collection[$label] = $this->_buildIcon();
141: } else {
142: throw new BadLabelException('Cannot store icon into collection with an empty label.');
143: }
144: } else {
145: throw new BadLabelException('Collection icon label must be a string.');
146: }
147: }
148: }
149:
150: /**
151: * Retrieve icon from collection
152: *
153: * @access public
154: * @param string $label Icon label used in store method
155: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $label is not a string
156: * @throws Khill\Fontawesome\Exceptions\CollectionIconException If icon $label is not set
157: * @return string HTML icon string
158: */
159: public function collection($label)
160: {
161: if(is_string($label))
162: {
163: if(isset($this->collection[$label]))
164: {
165: return $this->collection[$label];
166: } else {
167: throw new CollectionIconException('Collection icon "' . $label . '" does not exist.');
168: }
169: } else {
170: throw new BadLabelException('Collection icon label must be a string.');
171: }
172: }
173:
174: /**
175: * Sets which icon to use
176: *
177: * @access public
178: * @param string $icon Icon label, ommiting fa- prefix
179: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
180: * @return Khill\Fontawesome\FontAwesome FontAwesome object
181: */
182: public function icon($icon)
183: {
184: $this->_setIcon($icon);
185:
186: return $this;
187: }
188:
189: /**
190: * Adds extra classes to icon or stack
191: *
192: * @access public
193: * @param string $class CSS class
194: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $class is not a string
195: * @return Khill\Fontawesome\FontAwesome FontAwesome object
196: */
197: public function addClass($class)
198: {
199: if(is_array($class) && count($class) > 0)
200: {
201: foreach($class as $c) {
202: $this->_addClass($c);
203: }
204: } else {
205: $this->_addClass($class);
206: }
207:
208: return $this;
209: }
210:
211: /**
212: * Sets the icon or stack to be a fixed width
213: *
214: * @access public
215: * @param string $icon Icon label
216: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
217: * @return Khill\Fontawesome\FontAwesome FontAwesome object
218: */
219: public function fixedWidth($icon = '')
220: {
221: $this->_setIcon($icon);
222: $this->classes[] = 'fa-fw';
223:
224: return $this;
225: }
226:
227: /**
228: * Sets the icon or stack to be larger
229: *
230: * @access public
231: * @param string $icon Icon label
232: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
233: * @return Khill\Fontawesome\FontAwesome FontAwesome object
234: */
235: public function lg($icon = '')
236: {
237: $this->_setIcon($icon);
238: $this->_addClass('fa-lg');
239:
240: return $this;
241: }
242:
243: /**
244: * Sets the icon or stack to be 2 times larger
245: *
246: * @access public
247: * @param string $icon Icon label
248: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
249: * @return Khill\Fontawesome\FontAwesome FontAwesome object
250: */
251: public function x2($icon = '')
252: {
253: $this->_setIcon($icon);
254: $this->_addClass('fa-2x');
255:
256: return $this;
257: }
258:
259: /**
260: * Sets the icon or stack to be 3 times larger
261: *
262: * @access public
263: * @param string $icon Icon label
264: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
265: * @return Khill\Fontawesome\FontAwesome FontAwesome object
266: */
267: public function x3($icon = '')
268: {
269: $this->_setIcon($icon);
270: $this->_addClass('fa-3x');
271:
272: return $this;
273: }
274:
275: /**
276: * Sets the icon or stack to be 4 times larger
277: *
278: * @access public
279: * @param string $icon Icon label
280: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
281: * @return Khill\Fontawesome\FontAwesome FontAwesome object
282: */
283: public function x4($icon = '')
284: {
285: $this->_setIcon($icon);
286: $this->_addClass('fa-4x');
287:
288: return $this;
289: }
290:
291: /**
292: * Sets the icon or stack to be 5 times larger
293: *
294: * @access public
295: * @param string $icon Icon label
296: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
297: * @return Khill\Fontawesome\FontAwesome FontAwesome object
298: */
299: public function x5($icon = '')
300: {
301: $this->_setIcon($icon);
302: $this->_addClass('fa-5x');
303:
304: return $this;
305: }
306:
307: /**
308: * Sets the icon or stack to be inverted in color
309: *
310: * @access public
311: * @param string $icon Icon label
312: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
313: * @return Khill\Fontawesome\FontAwesome FontAwesome object
314: */
315: public function inverse($icon = '')
316: {
317: $this->_setIcon($icon);
318: $this->classes[] = 'fa-inverse';
319:
320: return $this;
321: }
322:
323: /**
324: * Sets the icon or stack to be rotated 90 degrees
325: *
326: * @access public
327: * @param string $icon Icon label
328: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
329: * @return Khill\Fontawesome\FontAwesome FontAwesome object
330: */
331: public function rotate90($icon = '')
332: {
333: $this->_setIcon($icon);
334: $this->classes[] = 'fa-rotate-90';
335:
336: return $this;
337: }
338:
339: /**
340: * Sets the icon or stack to be rotated 180 degrees
341: *
342: * @access public
343: * @param string $icon Icon label
344: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
345: * @return Khill\Fontawesome\FontAwesome FontAwesome object
346: */
347: public function rotate180($icon = '')
348: {
349: $this->_setIcon($icon);
350: $this->classes[] = 'fa-rotate-180';
351:
352: return $this;
353: }
354:
355: /**
356: * Sets the icon or stack to be rotated 270 degrees
357: *
358: * @access public
359: * @param string $icon Icon label
360: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
361: * @return Khill\Fontawesome\FontAwesome FontAwesome object
362: */
363: public function rotate270($icon = '')
364: {
365: $this->_setIcon($icon);
366: $this->classes[] = 'fa-rotate-270';
367:
368: return $this;
369: }
370:
371: /**
372: * Sets the icon or stack to be flipped horizontally
373: *
374: * @access public
375: * @param string $icon Icon label
376: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
377: * @return Khill\Fontawesome\FontAwesome FontAwesome object
378: */
379: public function flipHorizontal($icon = '')
380: {
381: $this->_setIcon($icon);
382: $this->classes[] = 'fa-flip-horizontal';
383:
384: return $this;
385: }
386:
387: /**
388: * Sets the icon or stack to be flipped vertically
389: *
390: * @access public
391: * @param string $icon Icon label
392: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
393: * @return Khill\Fontawesome\FontAwesome FontAwesome object
394: */
395: public function flipVertical($icon = '')
396: {
397: $this->_setIcon($icon);
398: $this->classes[] = 'fa-flip-vertical';
399:
400: return $this;
401: }
402:
403: /**
404: * Sets the icon to spin
405: *
406: * @access public
407: * @param string $icon Icon label
408: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
409: * @return Khill\Fontawesome\FontAwesome FontAwesome object
410: */
411: public function spin($icon = '')
412: {
413: $this->_setIcon($icon);
414: $this->classes[] = 'fa-spin';
415:
416: return $this;
417: }
418:
419: /**
420: * Sets a border around the icon
421: *
422: * @access public
423: * @param string $icon Icon label
424: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
425: * @return Khill\Fontawesome\FontAwesome FontAwesome object
426: */
427: public function border($icon = '')
428: {
429: $this->_setIcon($icon);
430: $this->classes[] = 'fa-border';
431:
432: return $this;
433: }
434:
435: /**
436: * Pulls the icon to the left
437: *
438: * @access public
439: * @param string $icon Icon label
440: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
441: * @return Khill\Fontawesome\FontAwesome FontAwesome object
442: */
443: public function left($icon = '')
444: {
445: $this->_setIcon($icon);
446: $this->classes[] = 'pull-left';
447:
448: return $this;
449: }
450:
451: /**
452: * Pulls the icon to the left
453: *
454: * @access public
455: * @param string $icon Icon label
456: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a string
457: * @return Khill\Fontawesome\FontAwesome FontAwesome object
458: */
459: public function right($icon = '')
460: {
461: $this->_setIcon($icon);
462: $this->classes[] = 'pull-right';
463:
464: return $this;
465: }
466:
467: /**
468: * Builds unordered list with icons
469: *
470: * @param string $iconLabel Default icon used in list (optional)
471: * @return Khill\Fontawesome\FontAwesome FontAwesome object
472: */
473: public function ul($iconLabel = '')
474: {
475: $this->list = new FontAwesomeList();
476:
477: if(is_string($iconLabel) && ! empty($iconLabel))
478: {
479: $this->list->setDefaultIcon($iconLabel);
480: } elseif(is_array($iconLabel) && count($iconLabel) > 0) {
481: $this->list->setListItems($iconLabel);
482: } else {
483: throw new IncompleteListException('List must have a default icon or associative array with icons as keys.');
484: }
485:
486: return $this;
487: }
488:
489: /**
490: * Adds items to unordered list with icons
491: *
492: * @param string|array $iconLine Adds a line or lines to the unordered list
493: * @return Khill\Fontawesome\FontAwesome FontAwesome object
494: */
495: public function li($iconLine = '')
496: {
497: if(is_string($iconLine) && ! empty($iconLine))
498: {
499: $this->list->addItem($iconLine);
500: } elseif(is_array($iconLine) && count($iconLine) > 0){
501: $this->list->addItems($iconLine);
502: } else {
503: throw new IncompleteListException('List must items must be a non empty string or array of strings.');
504: }
505:
506: return $this;
507: }
508:
509: /**
510: * Sets the top icon to be used in a stack
511: *
512: * @access public
513: * @param string $icon Icon label
514: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a non empty string
515: * @return Khill\Fontawesome\FontAwesome FontAwesome object
516: */
517: public function stack($icon)
518: {
519: if(is_string($icon) && ! empty($icon))
520: {
521: $this->stacking = true;
522: $this->stack = new FontAwesomeStack();
523: $this->stack->setTopIcon($icon);
524:
525: return $this;
526: } else {
527: throw new BadLabelException('Icon label must be a non empty string.');
528: }
529: }
530:
531: /**
532: * Sets the bottom icon to be used in a stack
533: *
534: * @access public
535: * @param string $icon Icon label
536: * @throws Khill\Fontawesome\Exceptions\BadLabelException If $icon is not a non empty string
537: * @throws Khill\Fontawesome\Exceptions\IncompleteStackException If The on() method was called without the stack() method
538: * @return Khill\Fontawesome\FontAwesome FontAwesome object
539: */
540: public function on($icon)
541: {
542: if($this->stacking === true)
543: {
544: if(is_string($icon) && ! empty($icon))
545: {
546: $this->stack->setBottomIcon($icon);
547:
548: return $this;
549: } else {
550: throw new BadLabelException('Icon label must be a non empty string.');
551: }
552: } else {
553: throw new IncompleteStackException('Stacks must be started with the stack() method.');
554: }
555: }
556:
557:
558: /**
559: * Sets icon label
560: *
561: * @access private
562: * @param string $icon Icon label
563: * @return void
564: */
565: private function _setIcon($icon)
566: {
567: if(is_string($icon))
568: {
569: if( ! empty($icon))
570: {
571: $this->iconLabel = $icon;
572: }
573: } else {
574: throw new BadLabelException('Icon label must be a string.');
575: }
576: }
577:
578: /**
579: * Builds the icon from the template
580: *
581: * @access private
582: * @return void
583: */
584: private function _buildIcon()
585: {
586: $classes = 'fa-' . $this->iconLabel;
587:
588: if( ! empty($this->classes))
589: {
590: foreach($this->classes as $class)
591: {
592: $classes .= ' ' . $class;
593: }
594: }
595:
596: return sprintf(self::ICON_HTML, $classes);
597: }
598:
599: /**
600: * Adds classes to icon or stack object
601: *
602: * @access private
603: * @return void
604: */
605: private function _addClass($class)
606: {
607: if(is_string($class) && ! empty($class))
608: {
609: if($this->stacking === true)
610: {
611: $this->stack->addClass($class);
612: } else {
613: $this->classes[] = $class;
614: }
615: } else {
616: throw new BadLabelException('Additional classes must be non empty strings.');
617: }
618: }
619:
620: /**
621: * Resets the FontAwesome class
622: *
623: * @access private
624: * @return void
625: */
626: private function _reset()
627: {
628: $this->iconLabel = '';
629: $this->stackTop = '';
630: $this->iconBottom = '';
631: $this->list = null;
632: $this->stack = null;
633: $this->stacking = false;
634: $this->classes = array();
635: }
636:
637: }
638: