SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/DisplayConditions/DisplayConditionString.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 DisplayConditionString 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. 'c' => 'Contains',
  35. 'nc' => 'Doesn\'t contain',
  36. 'b' => 'Begins with',
  37. 'e' => 'Ends with',
  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. $curr_val = $this->getCurrentValue($env);
  68. switch ($op) {
  69. case '==':
  70. return ($curr_val == $val);
  71. case '!=':
  72. return ($curr_val != $val);
  73. case 'c':
  74. return (strpos($curr_val, $val) !== false);
  75. case 'nc':
  76. return (strpos($curr_val, $val) === false);
  77. case 'b':
  78. return (strpos($curr_val, $val) === 0);
  79. case 'e':
  80. return (substr($curr_val, 0 - strlen($val)) === $val);
  81. default:
  82. return false;
  83. }
  84. }
  85.  
  86.  
  87. /**
  88.   * Return the current value of the variable
  89.   * This is compared against the params returned by {@see DisplayCondition::getParamValues}
  90.   *
  91.   * @param array $env Environment, such as page id etc
  92.   * @return string
  93.   */
  94. abstract protected function getCurrentValue(array $env);
  95.  
  96. }
  97.