Validator.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\RichObjectStrings;
  7. use OCP\RichObjectStrings\Definitions;
  8. use OCP\RichObjectStrings\InvalidObjectExeption;
  9. use OCP\RichObjectStrings\IValidator;
  10. /**
  11. * Class Validator
  12. *
  13. * @package OCP\RichObjectStrings
  14. * @since 11.0.0
  15. */
  16. class Validator implements IValidator {
  17. /** @var Definitions */
  18. protected $definitions;
  19. /** @var array[] */
  20. protected $requiredParameters = [];
  21. /**
  22. * Constructor
  23. *
  24. * @param Definitions $definitions
  25. */
  26. public function __construct(Definitions $definitions) {
  27. $this->definitions = $definitions;
  28. }
  29. /**
  30. * @param string $subject
  31. * @param array[] $parameters
  32. * @throws InvalidObjectExeption
  33. * @since 11.0.0
  34. */
  35. public function validate($subject, array $parameters) {
  36. $matches = [];
  37. $result = preg_match_all('/\{([a-z0-9]+)\}/i', $subject, $matches);
  38. if ($result === false) {
  39. throw new InvalidObjectExeption();
  40. }
  41. if (!empty($matches[1])) {
  42. foreach ($matches[1] as $parameter) {
  43. if (!isset($parameters[$parameter])) {
  44. throw new InvalidObjectExeption('Parameter is undefined');
  45. }
  46. }
  47. }
  48. foreach ($parameters as $parameter) {
  49. if (!\is_array($parameter)) {
  50. throw new InvalidObjectExeption('Parameter is malformed');
  51. }
  52. $this->validateParameter($parameter);
  53. }
  54. }
  55. /**
  56. * @param array $parameter
  57. * @throws InvalidObjectExeption
  58. */
  59. protected function validateParameter(array $parameter) {
  60. if (!isset($parameter['type'])) {
  61. throw new InvalidObjectExeption('Object type is undefined');
  62. }
  63. $definition = $this->definitions->getDefinition($parameter['type']);
  64. $requiredParameters = $this->getRequiredParameters($parameter['type'], $definition);
  65. $missingKeys = array_diff($requiredParameters, array_keys($parameter));
  66. if (!empty($missingKeys)) {
  67. throw new InvalidObjectExeption('Object is invalid, missing keys:'.json_encode($missingKeys));
  68. }
  69. }
  70. /**
  71. * @param string $type
  72. * @param array $definition
  73. * @return string[]
  74. */
  75. protected function getRequiredParameters($type, array $definition) {
  76. if (isset($this->requiredParameters[$type])) {
  77. return $this->requiredParameters[$type];
  78. }
  79. $this->requiredParameters[$type] = [];
  80. foreach ($definition['parameters'] as $parameter => $data) {
  81. if ($data['required']) {
  82. $this->requiredParameters[$type][] = $parameter;
  83. }
  84. }
  85. return $this->requiredParameters[$type];
  86. }
  87. }