1
0

SimpleSubstitutionTrait.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Julius Härtl <jus@bitgrid.net>
  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\Files_External\Config;
  26. /**
  27. * Trait SimpleSubstitutionTrait
  28. *
  29. * @package OCA\Files_External\Config
  30. * @since 16.0.0
  31. */
  32. trait SimpleSubstitutionTrait {
  33. /**
  34. * @var string the placeholder without $ prefix
  35. * @since 16.0.0
  36. */
  37. protected $placeholder;
  38. /** @var string */
  39. protected $sanitizedPlaceholder;
  40. /**
  41. * @param mixed $optionValue
  42. * @param string $replacement
  43. * @return mixed
  44. * @since 16.0.0
  45. */
  46. private function processInput($optionValue, string $replacement) {
  47. $this->checkPlaceholder();
  48. if (is_array($optionValue)) {
  49. foreach ($optionValue as &$value) {
  50. $value = $this->substituteIfString($value, $replacement);
  51. }
  52. } else {
  53. $optionValue = $this->substituteIfString($optionValue, $replacement);
  54. }
  55. return $optionValue;
  56. }
  57. /**
  58. * @throws \RuntimeException
  59. */
  60. protected function checkPlaceholder(): void {
  61. $this->sanitizedPlaceholder = trim(strtolower($this->placeholder));
  62. if (!(bool)\preg_match('/^[a-z0-9]*$/', $this->sanitizedPlaceholder)) {
  63. throw new \RuntimeException(sprintf(
  64. 'Invalid placeholder %s, only [a-z0-9] are allowed', $this->sanitizedPlaceholder
  65. ));
  66. }
  67. if ($this->sanitizedPlaceholder === '') {
  68. throw new \RuntimeException('Invalid empty placeholder');
  69. }
  70. }
  71. /**
  72. * @param mixed $value
  73. * @param string $replacement
  74. * @return mixed
  75. */
  76. protected function substituteIfString($value, string $replacement) {
  77. if (is_string($value)) {
  78. return str_ireplace('$' . $this->sanitizedPlaceholder, $replacement, $value);
  79. }
  80. return $value;
  81. }
  82. }