| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\OpenSpout\Common\Entity; |
| 4 |
|
| 5 |
use FluentCart\OpenSpout\Common\Entity\Style\Style; |
| 6 |
class Row |
| 7 |
{ |
| 8 |
/** |
| 9 |
* The cells in this row. |
| 10 |
* |
| 11 |
* @var Cell[] |
| 12 |
*/ |
| 13 |
protected $cells = []; |
| 14 |
/** |
| 15 |
* The row style. |
| 16 |
* |
| 17 |
* @var Style |
| 18 |
*/ |
| 19 |
protected $style; |
| 20 |
/** |
| 21 |
* Row height (default is 15). |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $height = '15'; |
| 26 |
/** |
| 27 |
* Row constructor. |
| 28 |
* |
| 29 |
* @param Cell[] $cells |
| 30 |
* @param null|Style $style |
| 31 |
*/ |
| 32 |
public function __construct(array $cells, $style) |
| 33 |
{ |
| 34 |
$this->setCells($cells)->setStyle($style); |
| 35 |
} |
| 36 |
/** |
| 37 |
* @return Cell[] $cells |
| 38 |
*/ |
| 39 |
public function getCells() |
| 40 |
{ |
| 41 |
return $this->cells; |
| 42 |
} |
| 43 |
/** |
| 44 |
* @param Cell[] $cells |
| 45 |
* |
| 46 |
* @return Row |
| 47 |
*/ |
| 48 |
public function setCells(array $cells) |
| 49 |
{ |
| 50 |
$this->cells = []; |
| 51 |
foreach ($cells as $cell) { |
| 52 |
$this->addCell($cell); |
| 53 |
} |
| 54 |
return $this; |
| 55 |
} |
| 56 |
/** |
| 57 |
* @param int $cellIndex |
| 58 |
* |
| 59 |
* @return Row |
| 60 |
*/ |
| 61 |
public function setCellAtIndex(Cell $cell, $cellIndex) |
| 62 |
{ |
| 63 |
$this->cells[$cellIndex] = $cell; |
| 64 |
return $this; |
| 65 |
} |
| 66 |
/** |
| 67 |
* @param int $cellIndex |
| 68 |
* |
| 69 |
* @return null|Cell |
| 70 |
*/ |
| 71 |
public function getCellAtIndex($cellIndex) |
| 72 |
{ |
| 73 |
return $this->cells[$cellIndex] ?? null; |
| 74 |
} |
| 75 |
/** |
| 76 |
* @return Row |
| 77 |
*/ |
| 78 |
public function addCell(Cell $cell) |
| 79 |
{ |
| 80 |
$this->cells[] = $cell; |
| 81 |
return $this; |
| 82 |
} |
| 83 |
/** |
| 84 |
* @return int |
| 85 |
*/ |
| 86 |
public function getNumCells() |
| 87 |
{ |
| 88 |
// When using "setCellAtIndex", it's possible to |
| 89 |
// have "$this->cells" contain holes. |
| 90 |
if (empty($this->cells)) { |
| 91 |
return 0; |
| 92 |
} |
| 93 |
return \max(\array_keys($this->cells)) + 1; |
| 94 |
} |
| 95 |
/** |
| 96 |
* @return Style |
| 97 |
*/ |
| 98 |
public function getStyle() |
| 99 |
{ |
| 100 |
return $this->style; |
| 101 |
} |
| 102 |
/** |
| 103 |
* @param null|Style $style |
| 104 |
* |
| 105 |
* @return Row |
| 106 |
*/ |
| 107 |
public function setStyle($style) |
| 108 |
{ |
| 109 |
$this->style = $style ?: new Style(); |
| 110 |
return $this; |
| 111 |
} |
| 112 |
/** |
| 113 |
* @return array The row values, as array |
| 114 |
*/ |
| 115 |
public function toArray() |
| 116 |
{ |
| 117 |
return \array_map(function (Cell $cell) { |
| 118 |
return $cell->getValue(); |
| 119 |
}, $this->cells); |
| 120 |
} |
| 121 |
/** |
| 122 |
* Set row height. |
| 123 |
* |
| 124 |
* @param string $height |
| 125 |
* |
| 126 |
* @return Row |
| 127 |
*/ |
| 128 |
public function setHeight($height) |
| 129 |
{ |
| 130 |
$this->height = $height; |
| 131 |
return $this; |
| 132 |
} |
| 133 |
/** |
| 134 |
* Returns row height. |
| 135 |
* |
| 136 |
* @return string |
| 137 |
*/ |
| 138 |
public function getHeight() |
| 139 |
{ |
| 140 |
return $this->height; |
| 141 |
} |
| 142 |
} |
| 143 |
|