AbstractStringCheck.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\WorkflowEngine\Check;
  7. use OCP\IL10N;
  8. use OCP\WorkflowEngine\ICheck;
  9. use OCP\WorkflowEngine\IManager;
  10. abstract class AbstractStringCheck implements ICheck {
  11. /** @var array[] Nested array: [Pattern => [ActualValue => Regex Result]] */
  12. protected $matches;
  13. /** @var IL10N */
  14. protected $l;
  15. /**
  16. * @param IL10N $l
  17. */
  18. public function __construct(IL10N $l) {
  19. $this->l = $l;
  20. }
  21. /**
  22. * @return string
  23. */
  24. abstract protected function getActualValue();
  25. /**
  26. * @param string $operator
  27. * @param string $value
  28. * @return bool
  29. */
  30. public function executeCheck($operator, $value) {
  31. $actualValue = $this->getActualValue();
  32. return $this->executeStringCheck($operator, $value, $actualValue);
  33. }
  34. /**
  35. * @param string $operator
  36. * @param string $checkValue
  37. * @param string $actualValue
  38. * @return bool
  39. */
  40. protected function executeStringCheck($operator, $checkValue, $actualValue) {
  41. if ($operator === 'is') {
  42. return $checkValue === $actualValue;
  43. } elseif ($operator === '!is') {
  44. return $checkValue !== $actualValue;
  45. } else {
  46. $match = $this->match($checkValue, $actualValue);
  47. if ($operator === 'matches') {
  48. return $match === 1;
  49. } else {
  50. return $match === 0;
  51. }
  52. }
  53. }
  54. /**
  55. * @param string $operator
  56. * @param string $value
  57. * @throws \UnexpectedValueException
  58. */
  59. public function validateCheck($operator, $value) {
  60. if (!in_array($operator, ['is', '!is', 'matches', '!matches'])) {
  61. throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
  62. }
  63. if (in_array($operator, ['matches', '!matches']) &&
  64. @preg_match($value, null) === false) {
  65. throw new \UnexpectedValueException($this->l->t('The given regular expression is invalid'), 2);
  66. }
  67. }
  68. public function supportedEntities(): array {
  69. // universal by default
  70. return [];
  71. }
  72. public function isAvailableForScope(int $scope): bool {
  73. // admin only by default
  74. return $scope === IManager::SCOPE_ADMIN;
  75. }
  76. /**
  77. * @param string $pattern
  78. * @param string $subject
  79. * @return int|bool
  80. */
  81. protected function match($pattern, $subject) {
  82. $patternHash = md5($pattern);
  83. $subjectHash = md5($subject);
  84. if (isset($this->matches[$patternHash][$subjectHash])) {
  85. return $this->matches[$patternHash][$subjectHash];
  86. }
  87. if (!isset($this->matches[$patternHash])) {
  88. $this->matches[$patternHash] = [];
  89. }
  90. $this->matches[$patternHash][$subjectHash] = preg_match($pattern, $subject);
  91. return $this->matches[$patternHash][$subjectHash];
  92. }
  93. }