Column.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter;
  3. use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
  4. use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter;
  5. class Column
  6. {
  7. const AUTOFILTER_FILTERTYPE_FILTER = 'filters';
  8. const AUTOFILTER_FILTERTYPE_CUSTOMFILTER = 'customFilters';
  9. // Supports no more than 2 rules, with an And/Or join criteria
  10. // if more than 1 rule is defined
  11. const AUTOFILTER_FILTERTYPE_DYNAMICFILTER = 'dynamicFilter';
  12. // Even though the filter rule is constant, the filtered data can vary
  13. // e.g. filtered by date = TODAY
  14. const AUTOFILTER_FILTERTYPE_TOPTENFILTER = 'top10';
  15. /**
  16. * Types of autofilter rules.
  17. *
  18. * @var string[]
  19. */
  20. private static $filterTypes = [
  21. // Currently we're not handling
  22. // colorFilter
  23. // extLst
  24. // iconFilter
  25. self::AUTOFILTER_FILTERTYPE_FILTER,
  26. self::AUTOFILTER_FILTERTYPE_CUSTOMFILTER,
  27. self::AUTOFILTER_FILTERTYPE_DYNAMICFILTER,
  28. self::AUTOFILTER_FILTERTYPE_TOPTENFILTER,
  29. ];
  30. // Multiple Rule Connections
  31. const AUTOFILTER_COLUMN_JOIN_AND = 'and';
  32. const AUTOFILTER_COLUMN_JOIN_OR = 'or';
  33. /**
  34. * Join options for autofilter rules.
  35. *
  36. * @var string[]
  37. */
  38. private static $ruleJoins = [
  39. self::AUTOFILTER_COLUMN_JOIN_AND,
  40. self::AUTOFILTER_COLUMN_JOIN_OR,
  41. ];
  42. /**
  43. * Autofilter.
  44. *
  45. * @var AutoFilter
  46. */
  47. private $parent;
  48. /**
  49. * Autofilter Column Index.
  50. *
  51. * @var string
  52. */
  53. private $columnIndex = '';
  54. /**
  55. * Autofilter Column Filter Type.
  56. *
  57. * @var string
  58. */
  59. private $filterType = self::AUTOFILTER_FILTERTYPE_FILTER;
  60. /**
  61. * Autofilter Multiple Rules And/Or.
  62. *
  63. * @var string
  64. */
  65. private $join = self::AUTOFILTER_COLUMN_JOIN_OR;
  66. /**
  67. * Autofilter Column Rules.
  68. *
  69. * @var array of Column\Rule
  70. */
  71. private $ruleset = [];
  72. /**
  73. * Autofilter Column Dynamic Attributes.
  74. *
  75. * @var array of mixed
  76. */
  77. private $attributes = [];
  78. /**
  79. * Create a new Column.
  80. *
  81. * @param string $pColumn Column (e.g. A)
  82. * @param AutoFilter $pParent Autofilter for this column
  83. */
  84. public function __construct($pColumn, AutoFilter $pParent = null)
  85. {
  86. $this->columnIndex = $pColumn;
  87. $this->parent = $pParent;
  88. }
  89. /**
  90. * Get AutoFilter Column Index.
  91. *
  92. * @return string
  93. */
  94. public function getColumnIndex()
  95. {
  96. return $this->columnIndex;
  97. }
  98. /**
  99. * Set AutoFilter Column Index.
  100. *
  101. * @param string $pColumn Column (e.g. A)
  102. *
  103. * @throws PhpSpreadsheetException
  104. *
  105. * @return Column
  106. */
  107. public function setColumnIndex($pColumn)
  108. {
  109. // Uppercase coordinate
  110. $pColumn = strtoupper($pColumn);
  111. if ($this->parent !== null) {
  112. $this->parent->testColumnInRange($pColumn);
  113. }
  114. $this->columnIndex = $pColumn;
  115. return $this;
  116. }
  117. /**
  118. * Get this Column's AutoFilter Parent.
  119. *
  120. * @return AutoFilter
  121. */
  122. public function getParent()
  123. {
  124. return $this->parent;
  125. }
  126. /**
  127. * Set this Column's AutoFilter Parent.
  128. *
  129. * @param AutoFilter $pParent
  130. *
  131. * @return Column
  132. */
  133. public function setParent(AutoFilter $pParent = null)
  134. {
  135. $this->parent = $pParent;
  136. return $this;
  137. }
  138. /**
  139. * Get AutoFilter Type.
  140. *
  141. * @return string
  142. */
  143. public function getFilterType()
  144. {
  145. return $this->filterType;
  146. }
  147. /**
  148. * Set AutoFilter Type.
  149. *
  150. * @param string $pFilterType
  151. *
  152. * @throws PhpSpreadsheetException
  153. *
  154. * @return Column
  155. */
  156. public function setFilterType($pFilterType)
  157. {
  158. if (!in_array($pFilterType, self::$filterTypes)) {
  159. throw new PhpSpreadsheetException('Invalid filter type for column AutoFilter.');
  160. }
  161. $this->filterType = $pFilterType;
  162. return $this;
  163. }
  164. /**
  165. * Get AutoFilter Multiple Rules And/Or Join.
  166. *
  167. * @return string
  168. */
  169. public function getJoin()
  170. {
  171. return $this->join;
  172. }
  173. /**
  174. * Set AutoFilter Multiple Rules And/Or.
  175. *
  176. * @param string $pJoin And/Or
  177. *
  178. * @throws PhpSpreadsheetException
  179. *
  180. * @return Column
  181. */
  182. public function setJoin($pJoin)
  183. {
  184. // Lowercase And/Or
  185. $pJoin = strtolower($pJoin);
  186. if (!in_array($pJoin, self::$ruleJoins)) {
  187. throw new PhpSpreadsheetException('Invalid rule connection for column AutoFilter.');
  188. }
  189. $this->join = $pJoin;
  190. return $this;
  191. }
  192. /**
  193. * Set AutoFilter Attributes.
  194. *
  195. * @param string[] $attributes
  196. *
  197. * @return Column
  198. */
  199. public function setAttributes(array $attributes)
  200. {
  201. $this->attributes = $attributes;
  202. return $this;
  203. }
  204. /**
  205. * Set An AutoFilter Attribute.
  206. *
  207. * @param string $pName Attribute Name
  208. * @param string $pValue Attribute Value
  209. *
  210. * @return Column
  211. */
  212. public function setAttribute($pName, $pValue)
  213. {
  214. $this->attributes[$pName] = $pValue;
  215. return $this;
  216. }
  217. /**
  218. * Get AutoFilter Column Attributes.
  219. *
  220. * @return string[]
  221. */
  222. public function getAttributes()
  223. {
  224. return $this->attributes;
  225. }
  226. /**
  227. * Get specific AutoFilter Column Attribute.
  228. *
  229. * @param string $pName Attribute Name
  230. *
  231. * @return string
  232. */
  233. public function getAttribute($pName)
  234. {
  235. if (isset($this->attributes[$pName])) {
  236. return $this->attributes[$pName];
  237. }
  238. return null;
  239. }
  240. /**
  241. * Get all AutoFilter Column Rules.
  242. *
  243. * @return Column\Rule[]
  244. */
  245. public function getRules()
  246. {
  247. return $this->ruleset;
  248. }
  249. /**
  250. * Get a specified AutoFilter Column Rule.
  251. *
  252. * @param int $pIndex Rule index in the ruleset array
  253. *
  254. * @return Column\Rule
  255. */
  256. public function getRule($pIndex)
  257. {
  258. if (!isset($this->ruleset[$pIndex])) {
  259. $this->ruleset[$pIndex] = new Column\Rule($this);
  260. }
  261. return $this->ruleset[$pIndex];
  262. }
  263. /**
  264. * Create a new AutoFilter Column Rule in the ruleset.
  265. *
  266. * @return Column\Rule
  267. */
  268. public function createRule()
  269. {
  270. $this->ruleset[] = new Column\Rule($this);
  271. return end($this->ruleset);
  272. }
  273. /**
  274. * Add a new AutoFilter Column Rule to the ruleset.
  275. *
  276. * @param Column\Rule $pRule
  277. *
  278. * @return Column
  279. */
  280. public function addRule(Column\Rule $pRule)
  281. {
  282. $pRule->setParent($this);
  283. $this->ruleset[] = $pRule;
  284. return $this;
  285. }
  286. /**
  287. * Delete a specified AutoFilter Column Rule
  288. * If the number of rules is reduced to 1, then we reset And/Or logic to Or.
  289. *
  290. * @param int $pIndex Rule index in the ruleset array
  291. *
  292. * @return Column
  293. */
  294. public function deleteRule($pIndex)
  295. {
  296. if (isset($this->ruleset[$pIndex])) {
  297. unset($this->ruleset[$pIndex]);
  298. // If we've just deleted down to a single rule, then reset And/Or joining to Or
  299. if (count($this->ruleset) <= 1) {
  300. $this->setJoin(self::AUTOFILTER_COLUMN_JOIN_OR);
  301. }
  302. }
  303. return $this;
  304. }
  305. /**
  306. * Delete all AutoFilter Column Rules.
  307. *
  308. * @return Column
  309. */
  310. public function clearRules()
  311. {
  312. $this->ruleset = [];
  313. $this->setJoin(self::AUTOFILTER_COLUMN_JOIN_OR);
  314. return $this;
  315. }
  316. /**
  317. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  318. */
  319. public function __clone()
  320. {
  321. $vars = get_object_vars($this);
  322. foreach ($vars as $key => $value) {
  323. if ($key === 'parent') {
  324. // Detach from autofilter parent
  325. $this->parent = null;
  326. } elseif ($key === 'ruleset') {
  327. // The columns array of \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\AutoFilter objects
  328. $this->ruleset = [];
  329. foreach ($value as $k => $v) {
  330. $cloned = clone $v;
  331. $cloned->setParent($this); // attach the new cloned Rule to this new cloned Autofilter Cloned object
  332. $this->ruleset[$k] = $cloned;
  333. }
  334. } elseif (is_object($value)) {
  335. $this->$key = clone $value;
  336. } else {
  337. $this->$key = $value;
  338. }
  339. }
  340. }
  341. }