PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/phpspreadsheet/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Worksheet/AutoFilter/Column.php

https://bitbucket.org/moodle/moodle
PHP | 404 lines | 184 code | 49 blank | 171 comment | 14 complexity | 210d949dc8a90a3be804f36b34932bb9 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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 null|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 Column\Rule[]
  70. */
  71. private $ruleset = [];
  72. /**
  73. * Autofilter Column Dynamic Attributes.
  74. *
  75. * @var mixed[]
  76. */
  77. private $attributes = [];
  78. /**
  79. * Create a new Column.
  80. *
  81. * @param string $column Column (e.g. A)
  82. * @param AutoFilter $parent Autofilter for this column
  83. */
  84. public function __construct($column, ?AutoFilter $parent = null)
  85. {
  86. $this->columnIndex = $column;
  87. $this->parent = $parent;
  88. }
  89. public function setEvaluatedFalse(): void
  90. {
  91. if ($this->parent !== null) {
  92. $this->parent->setEvaluated(false);
  93. }
  94. }
  95. /**
  96. * Get AutoFilter column index as string eg: 'A'.
  97. *
  98. * @return string
  99. */
  100. public function getColumnIndex()
  101. {
  102. return $this->columnIndex;
  103. }
  104. /**
  105. * Set AutoFilter column index as string eg: 'A'.
  106. *
  107. * @param string $column Column (e.g. A)
  108. *
  109. * @return $this
  110. */
  111. public function setColumnIndex($column)
  112. {
  113. $this->setEvaluatedFalse();
  114. // Uppercase coordinate
  115. $column = strtoupper($column);
  116. if ($this->parent !== null) {
  117. $this->parent->testColumnInRange($column);
  118. }
  119. $this->columnIndex = $column;
  120. return $this;
  121. }
  122. /**
  123. * Get this Column's AutoFilter Parent.
  124. *
  125. * @return null|AutoFilter
  126. */
  127. public function getParent()
  128. {
  129. return $this->parent;
  130. }
  131. /**
  132. * Set this Column's AutoFilter Parent.
  133. *
  134. * @return $this
  135. */
  136. public function setParent(?AutoFilter $parent = null)
  137. {
  138. $this->setEvaluatedFalse();
  139. $this->parent = $parent;
  140. return $this;
  141. }
  142. /**
  143. * Get AutoFilter Type.
  144. *
  145. * @return string
  146. */
  147. public function getFilterType()
  148. {
  149. return $this->filterType;
  150. }
  151. /**
  152. * Set AutoFilter Type.
  153. *
  154. * @param string $filterType
  155. *
  156. * @return $this
  157. */
  158. public function setFilterType($filterType)
  159. {
  160. $this->setEvaluatedFalse();
  161. if (!in_array($filterType, self::$filterTypes)) {
  162. throw new PhpSpreadsheetException('Invalid filter type for column AutoFilter.');
  163. }
  164. if ($filterType === self::AUTOFILTER_FILTERTYPE_CUSTOMFILTER && count($this->ruleset) > 2) {
  165. throw new PhpSpreadsheetException('No more than 2 rules are allowed in a Custom Filter');
  166. }
  167. $this->filterType = $filterType;
  168. return $this;
  169. }
  170. /**
  171. * Get AutoFilter Multiple Rules And/Or Join.
  172. *
  173. * @return string
  174. */
  175. public function getJoin()
  176. {
  177. return $this->join;
  178. }
  179. /**
  180. * Set AutoFilter Multiple Rules And/Or.
  181. *
  182. * @param string $join And/Or
  183. *
  184. * @return $this
  185. */
  186. public function setJoin($join)
  187. {
  188. $this->setEvaluatedFalse();
  189. // Lowercase And/Or
  190. $join = strtolower($join);
  191. if (!in_array($join, self::$ruleJoins)) {
  192. throw new PhpSpreadsheetException('Invalid rule connection for column AutoFilter.');
  193. }
  194. $this->join = $join;
  195. return $this;
  196. }
  197. /**
  198. * Set AutoFilter Attributes.
  199. *
  200. * @param mixed[] $attributes
  201. *
  202. * @return $this
  203. */
  204. public function setAttributes($attributes)
  205. {
  206. $this->setEvaluatedFalse();
  207. $this->attributes = $attributes;
  208. return $this;
  209. }
  210. /**
  211. * Set An AutoFilter Attribute.
  212. *
  213. * @param string $name Attribute Name
  214. * @param string $value Attribute Value
  215. *
  216. * @return $this
  217. */
  218. public function setAttribute($name, $value)
  219. {
  220. $this->setEvaluatedFalse();
  221. $this->attributes[$name] = $value;
  222. return $this;
  223. }
  224. /**
  225. * Get AutoFilter Column Attributes.
  226. *
  227. * @return int[]|string[]
  228. */
  229. public function getAttributes()
  230. {
  231. return $this->attributes;
  232. }
  233. /**
  234. * Get specific AutoFilter Column Attribute.
  235. *
  236. * @param string $name Attribute Name
  237. *
  238. * @return null|int|string
  239. */
  240. public function getAttribute($name)
  241. {
  242. if (isset($this->attributes[$name])) {
  243. return $this->attributes[$name];
  244. }
  245. return null;
  246. }
  247. public function ruleCount(): int
  248. {
  249. return count($this->ruleset);
  250. }
  251. /**
  252. * Get all AutoFilter Column Rules.
  253. *
  254. * @return Column\Rule[]
  255. */
  256. public function getRules()
  257. {
  258. return $this->ruleset;
  259. }
  260. /**
  261. * Get a specified AutoFilter Column Rule.
  262. *
  263. * @param int $index Rule index in the ruleset array
  264. *
  265. * @return Column\Rule
  266. */
  267. public function getRule($index)
  268. {
  269. if (!isset($this->ruleset[$index])) {
  270. $this->ruleset[$index] = new Column\Rule($this);
  271. }
  272. return $this->ruleset[$index];
  273. }
  274. /**
  275. * Create a new AutoFilter Column Rule in the ruleset.
  276. *
  277. * @return Column\Rule
  278. */
  279. public function createRule()
  280. {
  281. $this->setEvaluatedFalse();
  282. if ($this->filterType === self::AUTOFILTER_FILTERTYPE_CUSTOMFILTER && count($this->ruleset) >= 2) {
  283. throw new PhpSpreadsheetException('No more than 2 rules are allowed in a Custom Filter');
  284. }
  285. $this->ruleset[] = new Column\Rule($this);
  286. return end($this->ruleset);
  287. }
  288. /**
  289. * Add a new AutoFilter Column Rule to the ruleset.
  290. *
  291. * @return $this
  292. */
  293. public function addRule(Column\Rule $rule)
  294. {
  295. $this->setEvaluatedFalse();
  296. $rule->setParent($this);
  297. $this->ruleset[] = $rule;
  298. return $this;
  299. }
  300. /**
  301. * Delete a specified AutoFilter Column Rule
  302. * If the number of rules is reduced to 1, then we reset And/Or logic to Or.
  303. *
  304. * @param int $index Rule index in the ruleset array
  305. *
  306. * @return $this
  307. */
  308. public function deleteRule($index)
  309. {
  310. $this->setEvaluatedFalse();
  311. if (isset($this->ruleset[$index])) {
  312. unset($this->ruleset[$index]);
  313. // If we've just deleted down to a single rule, then reset And/Or joining to Or
  314. if (count($this->ruleset) <= 1) {
  315. $this->setJoin(self::AUTOFILTER_COLUMN_JOIN_OR);
  316. }
  317. }
  318. return $this;
  319. }
  320. /**
  321. * Delete all AutoFilter Column Rules.
  322. *
  323. * @return $this
  324. */
  325. public function clearRules()
  326. {
  327. $this->setEvaluatedFalse();
  328. $this->ruleset = [];
  329. $this->setJoin(self::AUTOFILTER_COLUMN_JOIN_OR);
  330. return $this;
  331. }
  332. /**
  333. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  334. */
  335. public function __clone()
  336. {
  337. $vars = get_object_vars($this);
  338. foreach ($vars as $key => $value) {
  339. if ($key === 'parent') {
  340. // Detach from autofilter parent
  341. $this->parent = null;
  342. } elseif ($key === 'ruleset') {
  343. // The columns array of \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\AutoFilter objects
  344. $this->ruleset = [];
  345. foreach ($value as $k => $v) {
  346. $cloned = clone $v;
  347. $cloned->setParent($this); // attach the new cloned Rule to this new cloned Autofilter Cloned object
  348. $this->ruleset[$k] = $cloned;
  349. }
  350. } else {
  351. $this->$key = $value;
  352. }
  353. }
  354. }
  355. }