Conditional.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Style;
  3. use PhpOffice\PhpSpreadsheet\IComparable;
  4. use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalDataBar;
  5. class Conditional implements IComparable
  6. {
  7. // Condition types
  8. const CONDITION_NONE = 'none';
  9. const CONDITION_CELLIS = 'cellIs';
  10. const CONDITION_CONTAINSTEXT = 'containsText';
  11. const CONDITION_EXPRESSION = 'expression';
  12. const CONDITION_CONTAINSBLANKS = 'containsBlanks';
  13. const CONDITION_NOTCONTAINSBLANKS = 'notContainsBlanks';
  14. const CONDITION_DATABAR = 'dataBar';
  15. const CONDITION_NOTCONTAINSTEXT = 'notContainsText';
  16. private const CONDITION_TYPES = [
  17. self::CONDITION_CELLIS,
  18. self::CONDITION_CONTAINSBLANKS,
  19. self::CONDITION_CONTAINSTEXT,
  20. self::CONDITION_DATABAR,
  21. self::CONDITION_EXPRESSION,
  22. self::CONDITION_NONE,
  23. self::CONDITION_NOTCONTAINSBLANKS,
  24. self::CONDITION_NOTCONTAINSTEXT,
  25. ];
  26. // Operator types
  27. const OPERATOR_NONE = '';
  28. const OPERATOR_BEGINSWITH = 'beginsWith';
  29. const OPERATOR_ENDSWITH = 'endsWith';
  30. const OPERATOR_EQUAL = 'equal';
  31. const OPERATOR_GREATERTHAN = 'greaterThan';
  32. const OPERATOR_GREATERTHANOREQUAL = 'greaterThanOrEqual';
  33. const OPERATOR_LESSTHAN = 'lessThan';
  34. const OPERATOR_LESSTHANOREQUAL = 'lessThanOrEqual';
  35. const OPERATOR_NOTEQUAL = 'notEqual';
  36. const OPERATOR_CONTAINSTEXT = 'containsText';
  37. const OPERATOR_NOTCONTAINS = 'notContains';
  38. const OPERATOR_BETWEEN = 'between';
  39. const OPERATOR_NOTBETWEEN = 'notBetween';
  40. /**
  41. * Condition type.
  42. *
  43. * @var string
  44. */
  45. private $conditionType = self::CONDITION_NONE;
  46. /**
  47. * Operator type.
  48. *
  49. * @var string
  50. */
  51. private $operatorType = self::OPERATOR_NONE;
  52. /**
  53. * Text.
  54. *
  55. * @var string
  56. */
  57. private $text;
  58. /**
  59. * Stop on this condition, if it matches.
  60. *
  61. * @var bool
  62. */
  63. private $stopIfTrue = false;
  64. /**
  65. * Condition.
  66. *
  67. * @var string[]
  68. */
  69. private $condition = [];
  70. /**
  71. * @var ConditionalDataBar
  72. */
  73. private $dataBar;
  74. /**
  75. * Style.
  76. *
  77. * @var Style
  78. */
  79. private $style;
  80. /**
  81. * Create a new Conditional.
  82. */
  83. public function __construct()
  84. {
  85. // Initialise values
  86. $this->style = new Style(false, true);
  87. }
  88. /**
  89. * Get Condition type.
  90. *
  91. * @return string
  92. */
  93. public function getConditionType()
  94. {
  95. return $this->conditionType;
  96. }
  97. /**
  98. * Set Condition type.
  99. *
  100. * @param string $type Condition type, see self::CONDITION_*
  101. *
  102. * @return $this
  103. */
  104. public function setConditionType($type)
  105. {
  106. $this->conditionType = $type;
  107. return $this;
  108. }
  109. /**
  110. * Get Operator type.
  111. *
  112. * @return string
  113. */
  114. public function getOperatorType()
  115. {
  116. return $this->operatorType;
  117. }
  118. /**
  119. * Set Operator type.
  120. *
  121. * @param string $type Conditional operator type, see self::OPERATOR_*
  122. *
  123. * @return $this
  124. */
  125. public function setOperatorType($type)
  126. {
  127. $this->operatorType = $type;
  128. return $this;
  129. }
  130. /**
  131. * Get text.
  132. *
  133. * @return string
  134. */
  135. public function getText()
  136. {
  137. return $this->text;
  138. }
  139. /**
  140. * Set text.
  141. *
  142. * @param string $text
  143. *
  144. * @return $this
  145. */
  146. public function setText($text)
  147. {
  148. $this->text = $text;
  149. return $this;
  150. }
  151. /**
  152. * Get StopIfTrue.
  153. *
  154. * @return bool
  155. */
  156. public function getStopIfTrue()
  157. {
  158. return $this->stopIfTrue;
  159. }
  160. /**
  161. * Set StopIfTrue.
  162. *
  163. * @param bool $stopIfTrue
  164. *
  165. * @return $this
  166. */
  167. public function setStopIfTrue($stopIfTrue)
  168. {
  169. $this->stopIfTrue = $stopIfTrue;
  170. return $this;
  171. }
  172. /**
  173. * Get Conditions.
  174. *
  175. * @return string[]
  176. */
  177. public function getConditions()
  178. {
  179. return $this->condition;
  180. }
  181. /**
  182. * Set Conditions.
  183. *
  184. * @param bool|float|int|string|string[] $conditions Condition
  185. *
  186. * @return $this
  187. */
  188. public function setConditions($conditions)
  189. {
  190. if (!is_array($conditions)) {
  191. $conditions = [$conditions];
  192. }
  193. $this->condition = $conditions;
  194. return $this;
  195. }
  196. /**
  197. * Add Condition.
  198. *
  199. * @param string $condition Condition
  200. *
  201. * @return $this
  202. */
  203. public function addCondition($condition)
  204. {
  205. $this->condition[] = $condition;
  206. return $this;
  207. }
  208. /**
  209. * Get Style.
  210. *
  211. * @return Style
  212. */
  213. public function getStyle()
  214. {
  215. return $this->style;
  216. }
  217. /**
  218. * Set Style.
  219. *
  220. * @param Style $style
  221. *
  222. * @return $this
  223. */
  224. public function setStyle(?Style $style = null)
  225. {
  226. $this->style = $style;
  227. return $this;
  228. }
  229. /**
  230. * get DataBar.
  231. *
  232. * @return null|ConditionalDataBar
  233. */
  234. public function getDataBar()
  235. {
  236. return $this->dataBar;
  237. }
  238. /**
  239. * set DataBar.
  240. *
  241. * @return $this
  242. */
  243. public function setDataBar(ConditionalDataBar $dataBar)
  244. {
  245. $this->dataBar = $dataBar;
  246. return $this;
  247. }
  248. /**
  249. * Get hash code.
  250. *
  251. * @return string Hash code
  252. */
  253. public function getHashCode()
  254. {
  255. return md5(
  256. $this->conditionType .
  257. $this->operatorType .
  258. implode(';', $this->condition) .
  259. $this->style->getHashCode() .
  260. __CLASS__
  261. );
  262. }
  263. /**
  264. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  265. */
  266. public function __clone()
  267. {
  268. $vars = get_object_vars($this);
  269. foreach ($vars as $key => $value) {
  270. if (is_object($value)) {
  271. $this->$key = clone $value;
  272. } else {
  273. $this->$key = $value;
  274. }
  275. }
  276. }
  277. /**
  278. * Verify if param is valid condition type.
  279. */
  280. public static function isValidConditionType(string $type): bool
  281. {
  282. return in_array($type, self::CONDITION_TYPES);
  283. }
  284. }