SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/DisplayConditions/DisplayConditionInteger.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers\DisplayConditions;
  15.  
  16.  
  17. /**
  18.  * Display condition for integers
  19.  */
  20. abstract class DisplayConditionInteger extends DisplayCondition
  21. {
  22.  
  23. /**
  24.   * Return the list of operators for the condition value
  25.   * Common values would be 'equals', 'not equals', 'less than', 'contains', etc
  26.   *
  27.   * @return array Key is internal value (e.g. '=') and value is the label (e.g. 'Equals')
  28.   */
  29. public function getOperators()
  30. {
  31. return [
  32. '==' => 'Equals',
  33. '!=' => 'Not equals',
  34. '>' => 'Greater than',
  35. '>=' => 'Greater than or equal',
  36. '<' => 'Less than',
  37. '<=' => 'Less than or equal',
  38. ];
  39. }
  40.  
  41.  
  42. /**
  43.   * Return the type of parameter for UI display
  44.   * Only two types are supported - 'text' or 'dropdown'
  45.   *
  46.   * @return string
  47.   */
  48. public function getParamType()
  49. {
  50. return 'text';
  51. }
  52.  
  53.  
  54. /**
  55.   * Does a given condition match?
  56.   *
  57.   * Calls {@see DisplayConditionEnum::getCurrentValue} to get the current value
  58.   * And then does the comparison against $val based on $op
  59.   *
  60.   * @param array $env Environment, such as page id etc
  61.   * @param string $op string One of the keys from the array returned by {@see DisplayCondition::getOperators}
  62.   * @param string $val string Entered value OR one of the keys from {@see DisplayCondition::getParamValues}
  63.   * @return bool True of condition matches, false if it does not
  64.   */
  65. public function match(array $env, $op, $val)
  66. {
  67. $val = (int) $val;
  68. $curr_val = (int) $this->getCurrentValue($env);
  69. switch ($op) {
  70. case '==':
  71. return ($curr_val == $val);
  72. case '!=':
  73. return ($curr_val != $val);
  74. case '>':
  75. return ($curr_val > $val);
  76. case '>=':
  77. return ($curr_val >= $val);
  78. case '<':
  79. return ($curr_val < $val);
  80. case '<=':
  81. return ($curr_val <= $val);
  82. default:
  83. return false;
  84. }
  85. }
  86.  
  87.  
  88. /**
  89.   * Return the current value of the variable
  90.   * This is compared against the params returned by {@see DisplayCondition::getParamValues}
  91.   *
  92.   * @param array $env Environment, such as page id etc
  93.   * @return string
  94.   */
  95. abstract protected function getCurrentValue(array $env);
  96.  
  97. }
  98.