Conditional.php 5.0 KB

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