Mark V
1: <?php
2: namespace MOC\V\Component\Document\Component\Bridge\Repository\PhpExcel;
3:
4: use MOC\V\Component\Document\Component\Exception\ComponentException;
5:
6: /**
7: * Class Style
8: *
9: * @package MOC\V\Component\Document\Component\Bridge\Repository\PhpExcel
10: */
11: class Style
12: {
13:
14: /** @var null|\PHPExcel_Worksheet $Worksheet */
15: private $Worksheet = null;
16: /** @var null|Cell $CellTL Cell Top-Left */
17: private $CellTL = null;
18: /** @var null|Cell $CellTL Cell Bottom-Right */
19: private $CellBR = null;
20:
21: /**
22: * Style constructor.
23: *
24: * @param \PHPExcel_Worksheet $Worksheet
25: * @param Cell $CellTopLeft Cell Single or Top-Left
26: * @param Cell|null $CellBottomRight Cell Bottom-Right
27: */
28: public function __construct(\PHPExcel_Worksheet $Worksheet, Cell $CellTopLeft, Cell $CellBottomRight = null)
29: {
30:
31: $this->Worksheet = $Worksheet;
32: $this->CellTL = $CellTopLeft;
33: $this->CellBR = $CellBottomRight;
34: }
35:
36: /**
37: * @param float|int $Value [-1 = Auto]
38: *
39: * @return $this
40: */
41: public function setColumnWidth($Value = -1)
42: {
43:
44: $ColumnRange = $this->getRangeColumnNameList();
45: foreach ($ColumnRange as $ColumnName) {
46: if (-1 == $Value) {
47: $this->Worksheet->getColumnDimension($ColumnName)->setAutoSize(true);
48: } else {
49: $this->Worksheet->getColumnDimension($ColumnName)->setAutoSize(false);
50: $this->Worksheet->getColumnDimension($ColumnName)->setWidth((float)$Value);
51: }
52: }
53: return $this;
54: }
55:
56: /**
57: * @return array Cell & Range: array( 'A', ... )
58: */
59: private function getRangeColumnNameList()
60: {
61:
62: if (null === $this->CellBR) {
63: return array($this->CellTL->getColumnName());
64: } else {
65: return range($this->CellTL->getColumnName(), $this->CellBR->getColumnName());
66: }
67: }
68:
69: /**
70: * @return float|int|array Cell: float | -1, Range: array( 'A' => float, 'B' => -1, ... ) [-1 = Auto]
71: */
72: public function getColumnWidth()
73: {
74:
75: $Result = array();
76: $ColumnRange = $this->getRangeColumnNameList();
77: foreach ($ColumnRange as $ColumnName) {
78: $Result[$ColumnName] = $this->Worksheet->getColumnDimension($ColumnName)->getWidth();
79: }
80: if (count($Result) == 1) {
81: return current($Result);
82: } else {
83: return $Result;
84: }
85: }
86:
87: /**
88: * @param bool $Toggle
89: *
90: * @return $this
91: */
92: public function setFontBold($Toggle = true)
93: {
94:
95: $this->Worksheet->getStyle($this->getRangeName())->getFont()->setBold($Toggle);
96: return $this;
97: }
98:
99: /**
100: * @return string 'A1:C2'
101: */
102: private function getRangeName()
103: {
104:
105: if (null === $this->CellBR) {
106: return (string)$this->CellTL->getCellName();
107: } else {
108: return (string)$this->CellTL->getCellName().':'.$this->CellBR->getCellName();
109: }
110: }
111:
112: /**
113: * @param float|int $Size
114: *
115: * @return $this
116: */
117: public function setFontSize($Size = 11)
118: {
119:
120: $this->Worksheet->getStyle($this->getRangeName())->getFont()->setSize($Size);
121: return $this;
122: }
123:
124: /**
125: * @return bool|array Cell: bool, Range: array( 'A1' => bool, 'B2' => bool, ... )
126: */
127: public function getFontBold()
128: {
129:
130: $Result = array();
131: $CellRange = $this->getRangeCellList();
132: foreach ($CellRange as $CellName) {
133: $Result[$CellName] = $this->Worksheet->getStyle($CellName)->getFont()->getBold();
134: }
135: if (count($Result) == 1) {
136: return current($Result);
137: } else {
138: return $Result;
139: }
140: }
141:
142: /**
143: * @return array Cell & Range: array( A1, A2, B1, ... )
144: */
145: private function getRangeCellList()
146: {
147:
148: $Result = array();
149: if (null === $this->CellBR) {
150: $Result = array($this->CellTL->getCellName());
151: } else {
152: $ColumnRange = $this->getRangeColumnNameList();
153: $RowList = $this->getRangeRowList();
154: foreach ($ColumnRange as $ColumnName) {
155: foreach ($RowList as $Index) {
156: array_push($Result, $ColumnName.$Index);
157: }
158: }
159: }
160: return $Result;
161: }
162:
163: /**
164: * @return array Cell & Range: array( 1, ... )
165: */
166: private function getRangeRowList()
167: {
168:
169: if (null === $this->CellBR) {
170: return array($this->CellTL->getRow());
171: } else {
172: return range($this->CellTL->getRow(), $this->CellBR->getRow());
173: }
174: }
175:
176: /**
177: * @return float|array Cell: float, Range: array( 'A1' => float, 'B2' => float, ... )
178: */
179: public function getFontSize()
180: {
181:
182: $Result = array();
183: $CellRange = $this->getRangeCellList();
184: foreach ($CellRange as $CellName) {
185: $Result[$CellName] = (float)$this->Worksheet->getStyle($CellName)->getFont()->getSize();
186: }
187: if (count($Result) == 1) {
188: return current($Result);
189: } else {
190: return $Result;
191: }
192: }
193:
194: /**
195: * @return $this
196: * @throws ComponentException
197: */
198: public function mergeCells()
199: {
200:
201: if (null !== $this->CellBR) {
202: try {
203: $this->Worksheet->mergeCells($this->getRangeName());
204: } catch (\Exception $Exception) {
205: throw new ComponentException($Exception->getMessage(), $Exception->getCode(), $Exception);
206: }
207: }
208: return $this;
209: }
210:
211: /**
212: * @return $this
213: */
214: public function setAlignmentLeft()
215: {
216:
217: $this->Worksheet->getStyle($this->getRangeName())->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
218: return $this;
219: }
220:
221: /**
222: * @return $this
223: */
224: public function setAlignmentCenter()
225: {
226:
227: $this->Worksheet->getStyle($this->getRangeName())->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
228: return $this;
229: }
230:
231: /**
232: * @return $this
233: */
234: public function setAlignmentRight()
235: {
236:
237: $this->Worksheet->getStyle($this->getRangeName())->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
238: return $this;
239: }
240:
241: /**
242: * @return $this
243: */
244: public function setAlignmentTop()
245: {
246:
247: $this->Worksheet->getStyle($this->getRangeName())->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_TOP);
248: return $this;
249: }
250:
251: /**
252: * @return $this
253: */
254: public function setAlignmentMiddle()
255: {
256:
257: $this->Worksheet->getStyle($this->getRangeName())->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
258: return $this;
259: }
260:
261: /**
262: * @return $this
263: */
264: public function setAlignmentBottom()
265: {
266:
267: $this->Worksheet->getStyle($this->getRangeName())->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_BOTTOM);
268: return $this;
269: }
270:
271: /**
272: * @param int $Size 0 = None, 1 = Thin, 2 = Medium, 3 = Thick
273: *
274: * @return $this
275: * @throws \PHPExcel_Exception
276: */
277: public function setBorderAll($Size = 1)
278: {
279:
280: $this->Worksheet->getStyle($this->getRangeName())->getBorders()->getAllBorders()->setBorderStyle($this->getBorderSize($Size));
281: $this->Worksheet->getStyle($this->getRangeName())->getBorders()->getAllBorders()->setColor(new \PHPExcel_Style_Color());
282: return $this;
283: }
284:
285: /**
286: * @param $Value
287: *
288: * @return string
289: */
290: private function getBorderSize($Value)
291: {
292:
293: switch ((int)$Value) {
294: case 0:
295: return \PHPExcel_Style_Border::BORDER_NONE;
296: case 1:
297: return \PHPExcel_Style_Border::BORDER_THIN;
298: case 2:
299: return \PHPExcel_Style_Border::BORDER_MEDIUM;
300: case 3:
301: return \PHPExcel_Style_Border::BORDER_THICK;
302: default:
303: return \PHPExcel_Style_Border::BORDER_THIN;
304: }
305: }
306:
307: /**
308: * @param int $Size 0 = None, 1 = Thin, 2 = Medium, 3 = Thick
309: *
310: * @return $this
311: */
312: public function setBorderVertical($Size = 1)
313: {
314:
315: $this->Worksheet->getStyle($this->getRangeName())->getBorders()->getVertical()->setBorderStyle($this->getBorderSize($Size));
316: $this->Worksheet->getStyle($this->getRangeName())->getBorders()->getVertical()->setColor(new \PHPExcel_Style_Color());
317: return $this;
318: }
319:
320: /**
321: * @param int $Size 0 = None, 1 = Thin, 2 = Medium, 3 = Thick
322: *
323: * @return $this
324: */
325: public function setBorderHorizontal($Size = 1)
326: {
327:
328: $this->Worksheet->getStyle($this->getRangeName())->getBorders()->getHorizontal()->setBorderStyle($this->getBorderSize($Size));
329: $this->Worksheet->getStyle($this->getRangeName())->getBorders()->getHorizontal()->setColor(new \PHPExcel_Style_Color());
330: return $this;
331: }
332:
333: /**
334: * @param int $Size 0 = None, 1 = Thin, 2 = Medium, 3 = Thick
335: *
336: * @return $this
337: */
338: public function setBorderOutline($Size = 1)
339: {
340:
341: $this->setBorderTop($Size);
342: $this->setBorderRight($Size);
343: $this->setBorderBottom($Size);
344: $this->setBorderLeft($Size);
345: return $this;
346: }
347:
348: /**
349: * @param int $Size 0 = None, 1 = Thin, 2 = Medium, 3 = Thick
350: *
351: * @return $this
352: */
353: public function setBorderTop($Size = 1)
354: {
355:
356: $this->Worksheet->getStyle($this->getRangeName())->getBorders()->getTop()->setBorderStyle($this->getBorderSize($Size));
357: $this->Worksheet->getStyle($this->getRangeName())->getBorders()->getTop()->setColor(new \PHPExcel_Style_Color());
358: return $this;
359: }
360:
361: /**
362: * @param int $Size 0 = None, 1 = Thin, 2 = Medium, 3 = Thick
363: *
364: * @return $this
365: */
366: public function setBorderRight($Size = 1)
367: {
368:
369: $this->Worksheet->getStyle($this->getRangeName())->getBorders()->getRight()->setBorderStyle($this->getBorderSize($Size));
370: $this->Worksheet->getStyle($this->getRangeName())->getBorders()->getRight()->setColor(new \PHPExcel_Style_Color());
371: return $this;
372: }
373:
374: /**
375: * @param int $Size 0 = None, 1 = Thin, 2 = Medium, 3 = Thick
376: *
377: * @return $this
378: */
379: public function setBorderBottom($Size = 1)
380: {
381:
382: $this->Worksheet->getStyle($this->getRangeName())->getBorders()->getBottom()->setBorderStyle($this->getBorderSize($Size));
383: $this->Worksheet->getStyle($this->getRangeName())->getBorders()->getBottom()->setColor(new \PHPExcel_Style_Color());
384: return $this;
385: }
386:
387: /**
388: * @param int $Size 0 = None, 1 = Thin, 2 = Medium, 3 = Thick
389: *
390: * @return $this
391: */
392: public function setBorderLeft($Size = 1)
393: {
394:
395: $this->Worksheet->getStyle($this->getRangeName())->getBorders()->getLeft()->setBorderStyle($this->getBorderSize($Size));
396: $this->Worksheet->getStyle($this->getRangeName())->getBorders()->getLeft()->setColor(new \PHPExcel_Style_Color());
397: return $this;
398: }
399: }
400: