1: <?php
2:
3: namespace Chromabits\Nucleus\View\Composite;
4:
5: use Chromabits\Nucleus\Data\ArrayList;
6: use Chromabits\Nucleus\Foundation\BaseObject;
7: use Chromabits\Nucleus\Support\Html;
8: use Chromabits\Nucleus\View\Common\Italic;
9: use Chromabits\Nucleus\View\Interfaces\RenderableInterface;
10: use Chromabits\Nucleus\View\Interfaces\SafeHtmlProducerInterface;
11: use Chromabits\Nucleus\View\SafeHtmlWrapper;
12:
13: /**
14: * Class AwesomeIcon.
15: *
16: * @author Eduardo Trujillo <ed@chromabits.com>
17: * @package Chromabits\Nucleus\View\Composite
18: */
19: class AwesomeIcon extends BaseObject implements
20: RenderableInterface,
21: SafeHtmlProducerInterface
22: {
23: /**
24: * @var string
25: */
26: protected $icon;
27:
28: /**
29: * @var int
30: */
31: protected $size;
32:
33: /**
34: * @var bool
35: */
36: protected $spin;
37:
38: /**
39: * @var bool
40: */
41: protected $fixedWidth;
42:
43: /**
44: * @var bool
45: */
46: protected $list;
47:
48: /**
49: * @var bool
50: */
51: protected $bordered;
52:
53: /**
54: * @var string|null
55: */
56: protected $pull;
57:
58: /**
59: * @var int
60: */
61: protected $rotation;
62:
63: /**
64: * @var string|null
65: */
66: protected $flip;
67:
68: /**
69: * Construct an instance of a AwesomeIcon.
70: *
71: * @param string $icon
72: * @param int $size
73: * @param bool $spin
74: */
75: public function __construct($icon, $size = 0, $spin = false)
76: {
77: parent::__construct();
78:
79: $this->icon = $icon;
80: $this->size = $size;
81: $this->spin = $spin;
82:
83: $this->fixedWidth = false;
84: $this->list = false;
85: $this->bordered = false;
86: $this->pull = null;
87: $this->rotation = 0;
88: $this->flip = null;
89: }
90:
91: /**
92: * @return AwesomeIcon
93: */
94: public function setFixedWidth()
95: {
96: $copy = clone $this;
97:
98: $this->fixedWidth = true;
99:
100: return $copy;
101: }
102:
103: /**
104: * @return AwesomeIcon
105: */
106: public function setList()
107: {
108: $copy = clone $this;
109:
110: $this->list = true;
111:
112: return $copy;
113: }
114:
115: /**
116: * @return AwesomeIcon
117: */
118: public function setBordered()
119: {
120: $copy = clone $this;
121:
122: $this->bordered = true;
123:
124: return $copy;
125: }
126:
127: /**
128: * @return AwesomeIcon
129: */
130: public function pullRight()
131: {
132: $copy = clone $this;
133:
134: $this->pull = 'right';
135:
136: return $copy;
137: }
138:
139: /**
140: * @return AwesomeIcon
141: */
142: public function pullLeft()
143: {
144: $copy = clone $this;
145:
146: $this->pull = 'left';
147:
148: return $copy;
149: }
150:
151: /**
152: * @return AwesomeIcon
153: */
154: public function rotate90()
155: {
156: $copy = clone $this;
157:
158: $this->rotation = 90;
159:
160: return $copy;
161: }
162:
163: /**
164: * @return AwesomeIcon
165: */
166: public function rotate180()
167: {
168: $copy = clone $this;
169:
170: $this->rotation = 180;
171:
172: return $copy;
173: }
174:
175: /**
176: * @return AwesomeIcon
177: */
178: public function rotate270()
179: {
180: $copy = clone $this;
181:
182: $this->rotation = 270;
183:
184: return $copy;
185: }
186:
187: /**
188: * @return AwesomeIcon
189: */
190: public function flipHorizontally()
191: {
192: $copy = clone $this;
193:
194: $this->flip = 'horizontal';
195:
196: return $copy;
197: }
198:
199: /**
200: * @return AwesomeIcon
201: */
202: public function flipVertically()
203: {
204: $copy = clone $this;
205:
206: $this->flip = 'vertical';
207:
208: return $copy;
209: }
210:
211: /**
212: * Render the object into a string.
213: *
214: * @return mixed
215: */
216: public function render()
217: {
218: $classes = ArrayList::of([
219: 'fa',
220: vsprintf('fa-%s', [$this->icon])
221: ]);
222:
223: // Spin class (fa-spin)
224: if ($this->spin) {
225: $classes = $classes->append(ArrayList::of(['fa-spin']));
226: }
227:
228: // Size classes (fa-lg, fa-2x, fa-3x...)
229: if ($this->size === 1) {
230: $classes = $classes->append(ArrayList::of(['fa-lg']));
231: } elseif ($this->size > 1 && $this->size < 6) {
232: $classes = $classes->append(ArrayList::of([
233: vsprintf('fa-%dx', [$this->size])
234: ]));
235: }
236:
237: // Fixed-width class (fa-fw)
238: if ($this->fixedWidth) {
239: $classes = $classes->append(ArrayList::of(['fa-fw']));
240: }
241:
242: // List icons class (fa-li)
243: if ($this->list) {
244: $classes = $classes->append(ArrayList::of(['fa-li']));
245: }
246:
247: // Border class (fa-border)
248: if ($this->bordered) {
249: $classes = $classes->append(ArrayList::of(['fa-border']));
250: }
251:
252: // Pull classes (fa-pull-right, fa-pull-left)
253: if ($this->pull !== null && $this->pull !== '') {
254: $classes = $classes->append(ArrayList::of([
255: vsprintf('fa-pull-%s', [$this->pull])
256: ]));
257: }
258:
259: // Rotation classes
260: switch ($this->rotation) {
261: case 90:
262: $classes = $classes->append(ArrayList::of(['fa-rotate-90']));
263: break;
264: case 180:
265: $classes = $classes->append(ArrayList::of(['fa-rotate-180']));
266: break;
267: case 270:
268: $classes = $classes->append(ArrayList::of(['fa-rotate-270']));
269: break;
270: }
271:
272: // Flip classes
273: switch ($this->flip) {
274: case 'horizontal':
275: $classes = $classes->append(ArrayList::of([
276: 'fa-flip-horizontal'
277: ]));
278: break;
279: case 'vertical':
280: $classes = $classes->append(ArrayList::of([
281: 'fa-flip-horizontal'
282: ]));
283: break;
284: }
285:
286: return (new Italic([
287: 'class' => $classes->join(' '),
288: ]))->render();
289: }
290:
291: /**
292: * Get a safe HTML version of the contents of this object.
293: *
294: * @return SafeHtmlWrapper
295: */
296: public function getSafeHtml()
297: {
298: return Html::safe($this->render());
299: }
300: }
301: