1
0

AbstractStringCheck.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\WorkflowEngine\Check;
  22. use OCP\Files\Storage\IStorage;
  23. use OCP\IL10N;
  24. use OCP\WorkflowEngine\ICheck;
  25. abstract class AbstractStringCheck implements ICheck {
  26. /** @var array[] Nested array: [Pattern => [ActualValue => Regex Result]] */
  27. protected $matches;
  28. /** @var IL10N */
  29. protected $l;
  30. /**
  31. * @param IL10N $l
  32. */
  33. public function __construct(IL10N $l) {
  34. $this->l = $l;
  35. }
  36. /**
  37. * @param IStorage $storage
  38. * @param string $path
  39. */
  40. public function setFileInfo(IStorage $storage, $path) {
  41. // Nothing changes here with a different path
  42. }
  43. /**
  44. * @return string
  45. */
  46. abstract protected function getActualValue();
  47. /**
  48. * @param string $operator
  49. * @param string $value
  50. * @return bool
  51. */
  52. public function executeCheck($operator, $value) {
  53. $actualValue = $this->getActualValue();
  54. return $this->executeStringCheck($operator, $value, $actualValue);
  55. }
  56. /**
  57. * @param string $operator
  58. * @param string $checkValue
  59. * @param string $actualValue
  60. * @return bool
  61. */
  62. protected function executeStringCheck($operator, $checkValue, $actualValue) {
  63. if ($operator === 'is') {
  64. return $checkValue === $actualValue;
  65. } else if ($operator === '!is') {
  66. return $checkValue !== $actualValue;
  67. } else {
  68. $match = $this->match($checkValue, $actualValue);
  69. if ($operator === 'matches') {
  70. return $match === 1;
  71. } else {
  72. return $match === 0;
  73. }
  74. }
  75. }
  76. /**
  77. * @param string $operator
  78. * @param string $value
  79. * @throws \UnexpectedValueException
  80. */
  81. public function validateCheck($operator, $value) {
  82. if (!in_array($operator, ['is', '!is', 'matches', '!matches'])) {
  83. throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
  84. }
  85. if (in_array($operator, ['matches', '!matches']) &&
  86. @preg_match($value, null) === false) {
  87. throw new \UnexpectedValueException($this->l->t('The given regular expression is invalid'), 2);
  88. }
  89. }
  90. /**
  91. * @param string $pattern
  92. * @param string $subject
  93. * @return int|bool
  94. */
  95. protected function match($pattern, $subject) {
  96. $patternHash = md5($pattern);
  97. $subjectHash = md5($subject);
  98. if (isset($this->matches[$patternHash][$subjectHash])) {
  99. return $this->matches[$patternHash][$subjectHash];
  100. }
  101. if (!isset($this->matches[$patternHash])) {
  102. $this->matches[$patternHash] = [];
  103. }
  104. $this->matches[$patternHash][$subjectHash] = preg_match($pattern, $subject);
  105. return $this->matches[$patternHash][$subjectHash];
  106. }
  107. }