AbstractStringCheck.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /**
  14. * @param IL10N $l
  15. */
  16. public function __construct(
  17. protected IL10N $l,
  18. ) {
  19. }
  20. /**
  21. * @return string
  22. */
  23. abstract protected function getActualValue();
  24. /**
  25. * @param string $operator
  26. * @param string $value
  27. * @return bool
  28. */
  29. public function executeCheck($operator, $value) {
  30. $actualValue = $this->getActualValue();
  31. return $this->executeStringCheck($operator, $value, $actualValue);
  32. }
  33. /**
  34. * @param string $operator
  35. * @param string $checkValue
  36. * @param string $actualValue
  37. * @return bool
  38. */
  39. protected function executeStringCheck($operator, $checkValue, $actualValue) {
  40. if ($operator === 'is') {
  41. return $checkValue === $actualValue;
  42. } elseif ($operator === '!is') {
  43. return $checkValue !== $actualValue;
  44. } else {
  45. $match = $this->match($checkValue, $actualValue);
  46. if ($operator === 'matches') {
  47. return $match === 1;
  48. } else {
  49. return $match === 0;
  50. }
  51. }
  52. }
  53. /**
  54. * @param string $operator
  55. * @param string $value
  56. * @throws \UnexpectedValueException
  57. */
  58. public function validateCheck($operator, $value) {
  59. if (!in_array($operator, ['is', '!is', 'matches', '!matches'])) {
  60. throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
  61. }
  62. if (in_array($operator, ['matches', '!matches']) &&
  63. @preg_match($value, null) === false) {
  64. throw new \UnexpectedValueException($this->l->t('The given regular expression is invalid'), 2);
  65. }
  66. }
  67. public function supportedEntities(): array {
  68. // universal by default
  69. return [];
  70. }
  71. public function isAvailableForScope(int $scope): bool {
  72. // admin only by default
  73. return $scope === IManager::SCOPE_ADMIN;
  74. }
  75. /**
  76. * @param string $pattern
  77. * @param string $subject
  78. * @return int|bool
  79. */
  80. protected function match($pattern, $subject) {
  81. $patternHash = md5($pattern);
  82. $subjectHash = md5($subject);
  83. if (isset($this->matches[$patternHash][$subjectHash])) {
  84. return $this->matches[$patternHash][$subjectHash];
  85. }
  86. if (!isset($this->matches[$patternHash])) {
  87. $this->matches[$patternHash] = [];
  88. }
  89. $this->matches[$patternHash][$subjectHash] = preg_match($pattern, $subject);
  90. return $this->matches[$patternHash][$subjectHash];
  91. }
  92. }