Validator.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OC\RichObjectStrings;
  22. use OCP\RichObjectStrings\Definitions;
  23. use OCP\RichObjectStrings\InvalidObjectExeption;
  24. use OCP\RichObjectStrings\IValidator;
  25. /**
  26. * Class Validator
  27. *
  28. * @package OCP\RichObjectStrings
  29. * @since 11.0.0
  30. */
  31. class Validator implements IValidator {
  32. /** @var Definitions */
  33. protected $definitions;
  34. /** @var array[] */
  35. protected $requiredParameters = [];
  36. /**
  37. * Constructor
  38. *
  39. * @param Definitions $definitions
  40. */
  41. public function __construct(Definitions $definitions) {
  42. $this->definitions = $definitions;
  43. }
  44. /**
  45. * @param string $subject
  46. * @param array[] $parameters
  47. * @throws InvalidObjectExeption
  48. * @since 11.0.0
  49. */
  50. public function validate($subject, array $parameters) {
  51. $matches = [];
  52. $result = preg_match_all('/\{([a-z0-9]+)\}/i', $subject, $matches);
  53. if ($result === false) {
  54. throw new InvalidObjectExeption();
  55. }
  56. if (!empty($matches[1])) {
  57. foreach ($matches[1] as $parameter) {
  58. if (!isset($parameters[$parameter])) {
  59. throw new InvalidObjectExeption('Parameter is undefined');
  60. } else {
  61. $this->validateParameter($parameters[$parameter]);
  62. }
  63. }
  64. }
  65. }
  66. /**
  67. * @param array $parameter
  68. * @throws InvalidObjectExeption
  69. */
  70. protected function validateParameter(array $parameter) {
  71. if (!isset($parameter['type'])) {
  72. throw new InvalidObjectExeption('Object type is undefined');
  73. }
  74. $definition = $this->definitions->getDefinition($parameter['type']);
  75. $requiredParameters = $this->getRequiredParameters($parameter['type'], $definition);
  76. $missingKeys = array_diff($requiredParameters, array_keys($parameter));
  77. if (!empty($missingKeys)) {
  78. throw new InvalidObjectExeption('Object is invalid');
  79. }
  80. }
  81. /**
  82. * @param string $type
  83. * @param array $definition
  84. * @return string[]
  85. */
  86. protected function getRequiredParameters($type, array $definition) {
  87. if (isset($this->requiredParameters[$type])) {
  88. return $this->requiredParameters[$type];
  89. }
  90. $this->requiredParameters[$type] = [];
  91. foreach ($definition['parameters'] as $parameter => $data) {
  92. if ($data['required']) {
  93. $this->requiredParameters[$type][] = $parameter;
  94. }
  95. }
  96. return $this->requiredParameters[$type];
  97. }
  98. }