Conditional.php 5.0 KB

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