| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\OpenSpout\Common\Entity; |
| 4 |
|
| 5 |
use FluentCart\OpenSpout\Common\Entity\Style\Style; |
| 6 |
use FluentCart\OpenSpout\Common\Helper\CellTypeHelper; |
| 7 |
class Cell |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Numeric cell type (whole numbers, fractional numbers, dates). |
| 11 |
*/ |
| 12 |
public const TYPE_NUMERIC = 0; |
| 13 |
/** |
| 14 |
* String (text) cell type. |
| 15 |
*/ |
| 16 |
public const TYPE_STRING = 1; |
| 17 |
/** |
| 18 |
* Formula cell type |
| 19 |
* Not used at the moment. |
| 20 |
*/ |
| 21 |
public const TYPE_FORMULA = 2; |
| 22 |
/** |
| 23 |
* Empty cell type. |
| 24 |
*/ |
| 25 |
public const TYPE_EMPTY = 3; |
| 26 |
/** |
| 27 |
* Boolean cell type. |
| 28 |
*/ |
| 29 |
public const TYPE_BOOLEAN = 4; |
| 30 |
/** |
| 31 |
* Date cell type. |
| 32 |
*/ |
| 33 |
public const TYPE_DATE = 5; |
| 34 |
/** |
| 35 |
* Error cell type. |
| 36 |
*/ |
| 37 |
public const TYPE_ERROR = 6; |
| 38 |
/** |
| 39 |
* The value of this cell. |
| 40 |
* |
| 41 |
* @var null|mixed |
| 42 |
*/ |
| 43 |
protected $value; |
| 44 |
/** |
| 45 |
* The cell type. |
| 46 |
* |
| 47 |
* @var null|int |
| 48 |
*/ |
| 49 |
protected $type; |
| 50 |
/** |
| 51 |
* The cell style. |
| 52 |
* |
| 53 |
* @var Style |
| 54 |
*/ |
| 55 |
protected $style; |
| 56 |
/** |
| 57 |
* @param null|mixed $value |
| 58 |
*/ |
| 59 |
public function __construct($value, Style $style = null) |
| 60 |
{ |
| 61 |
$this->setValue($value); |
| 62 |
$this->setStyle($style); |
| 63 |
} |
| 64 |
/** |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public function __toString() |
| 68 |
{ |
| 69 |
return (string) $this->getValue(); |
| 70 |
} |
| 71 |
/** |
| 72 |
* @param null|mixed $value |
| 73 |
*/ |
| 74 |
public function setValue($value) |
| 75 |
{ |
| 76 |
$this->value = $value; |
| 77 |
$this->type = $this->detectType($value); |
| 78 |
} |
| 79 |
/** |
| 80 |
* @return null|mixed |
| 81 |
*/ |
| 82 |
public function getValue() |
| 83 |
{ |
| 84 |
return !$this->isError() ? $this->value : null; |
| 85 |
} |
| 86 |
/** |
| 87 |
* @return mixed |
| 88 |
*/ |
| 89 |
public function getValueEvenIfError() |
| 90 |
{ |
| 91 |
return $this->value; |
| 92 |
} |
| 93 |
/** |
| 94 |
* @param null|Style $style |
| 95 |
*/ |
| 96 |
public function setStyle($style) |
| 97 |
{ |
| 98 |
$this->style = $style ?: new Style(); |
| 99 |
} |
| 100 |
/** |
| 101 |
* @return Style |
| 102 |
*/ |
| 103 |
public function getStyle() |
| 104 |
{ |
| 105 |
return $this->style; |
| 106 |
} |
| 107 |
/** |
| 108 |
* @return null|int |
| 109 |
*/ |
| 110 |
public function getType() |
| 111 |
{ |
| 112 |
return $this->type; |
| 113 |
} |
| 114 |
/** |
| 115 |
* @param int $type |
| 116 |
*/ |
| 117 |
public function setType($type) |
| 118 |
{ |
| 119 |
$this->type = $type; |
| 120 |
} |
| 121 |
/** |
| 122 |
* @return bool |
| 123 |
*/ |
| 124 |
public function isBoolean() |
| 125 |
{ |
| 126 |
return self::TYPE_BOOLEAN === $this->type; |
| 127 |
} |
| 128 |
/** |
| 129 |
* @return bool |
| 130 |
*/ |
| 131 |
public function isEmpty() |
| 132 |
{ |
| 133 |
return self::TYPE_EMPTY === $this->type; |
| 134 |
} |
| 135 |
/** |
| 136 |
* @return bool |
| 137 |
*/ |
| 138 |
public function isNumeric() |
| 139 |
{ |
| 140 |
return self::TYPE_NUMERIC === $this->type; |
| 141 |
} |
| 142 |
/** |
| 143 |
* @return bool |
| 144 |
*/ |
| 145 |
public function isString() |
| 146 |
{ |
| 147 |
return self::TYPE_STRING === $this->type; |
| 148 |
} |
| 149 |
/** |
| 150 |
* @return bool |
| 151 |
*/ |
| 152 |
public function isDate() |
| 153 |
{ |
| 154 |
return self::TYPE_DATE === $this->type; |
| 155 |
} |
| 156 |
/** |
| 157 |
* @return bool |
| 158 |
*/ |
| 159 |
public function isFormula() |
| 160 |
{ |
| 161 |
return self::TYPE_FORMULA === $this->type; |
| 162 |
} |
| 163 |
/** |
| 164 |
* @return bool |
| 165 |
*/ |
| 166 |
public function isError() |
| 167 |
{ |
| 168 |
return self::TYPE_ERROR === $this->type; |
| 169 |
} |
| 170 |
/** |
| 171 |
* Get the current value type. |
| 172 |
* |
| 173 |
* @param null|mixed $value |
| 174 |
* |
| 175 |
* @return int |
| 176 |
*/ |
| 177 |
protected function detectType($value) |
| 178 |
{ |
| 179 |
if (CellTypeHelper::isBoolean($value)) { |
| 180 |
return self::TYPE_BOOLEAN; |
| 181 |
} |
| 182 |
if (CellTypeHelper::isEmpty($value)) { |
| 183 |
return self::TYPE_EMPTY; |
| 184 |
} |
| 185 |
if (CellTypeHelper::isNumeric($value)) { |
| 186 |
return self::TYPE_NUMERIC; |
| 187 |
} |
| 188 |
if (CellTypeHelper::isDateTimeOrDateInterval($value)) { |
| 189 |
return self::TYPE_DATE; |
| 190 |
} |
| 191 |
if (CellTypeHelper::isFormula($value)) { |
| 192 |
return self::TYPE_FORMULA; |
| 193 |
} |
| 194 |
if (CellTypeHelper::isNonEmptyString($value)) { |
| 195 |
return self::TYPE_STRING; |
| 196 |
} |
| 197 |
return self::TYPE_ERROR; |
| 198 |
} |
| 199 |
} |
| 200 |
|