SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/DisplayConditions/DisplayConditionEnum.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 enumerations - where the set of parameter values is fixed and always equals or not equals
  19.  */
  20. abstract class DisplayConditionEnum 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. '==' => 'Is',
  33. '!=' => 'Is not',
  34. ];
  35. }
  36.  
  37.  
  38. /**
  39.   * Return the type of parameter for UI display
  40.   * Only two types are supported - 'text' or 'dropdown'
  41.   *
  42.   * @return string
  43.   */
  44. public function getParamType()
  45. {
  46. return 'dropdown';
  47. }
  48.  
  49.  
  50. /**
  51.   * Does a given condition match?
  52.   *
  53.   * Calls {@see DisplayConditionEnum::getCurrentValue} to get the current value
  54.   * And then does the comparison against $val based on $op
  55.   *
  56.   * @param array $env Environment, such as page id etc
  57.   * @param string $op string One of the keys from the array returned by {@see DisplayCondition::getOperators}
  58.   * @param string $val string Entered value OR one of the keys from {@see DisplayCondition::getParamValues}
  59.   * @return bool True of condition matches, false if it does not
  60.   */
  61. public function match(array $env, $op, $val)
  62. {
  63. $curr_val = $this->getCurrentValue($env);
  64. switch ($op) {
  65. case '==':
  66. return ($curr_val == $val);
  67. case '!=':
  68. return ($curr_val != $val);
  69. default:
  70. return false;
  71. }
  72. }
  73.  
  74.  
  75. /**
  76.   * Return the current value of the variable
  77.   * This is compared against the params returned by {@see DisplayCondition::getParamValues}
  78.   *
  79.   * @param array $env Environment, such as page id etc
  80.   * @return string
  81.   */
  82. abstract protected function getCurrentValue(array $env);
  83.  
  84. }
  85.