AbstractStringCheck.php 3.4 KB

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