Validator.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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 OC\RichObjectStrings;
  26. use OCP\RichObjectStrings\Definitions;
  27. use OCP\RichObjectStrings\InvalidObjectExeption;
  28. use OCP\RichObjectStrings\IValidator;
  29. /**
  30. * Class Validator
  31. *
  32. * @package OCP\RichObjectStrings
  33. * @since 11.0.0
  34. */
  35. class Validator implements IValidator {
  36. /** @var Definitions */
  37. protected $definitions;
  38. /** @var array[] */
  39. protected $requiredParameters = [];
  40. /**
  41. * Constructor
  42. *
  43. * @param Definitions $definitions
  44. */
  45. public function __construct(Definitions $definitions) {
  46. $this->definitions = $definitions;
  47. }
  48. /**
  49. * @param string $subject
  50. * @param array[] $parameters
  51. * @throws InvalidObjectExeption
  52. * @since 11.0.0
  53. */
  54. public function validate($subject, array $parameters) {
  55. $matches = [];
  56. $result = preg_match_all('/\{([a-z0-9]+)\}/i', $subject, $matches);
  57. if ($result === false) {
  58. throw new InvalidObjectExeption();
  59. }
  60. if (!empty($matches[1])) {
  61. foreach ($matches[1] as $parameter) {
  62. if (!isset($parameters[$parameter])) {
  63. throw new InvalidObjectExeption('Parameter is undefined');
  64. }
  65. }
  66. }
  67. foreach ($parameters as $parameter) {
  68. if (!\is_array($parameter)) {
  69. throw new InvalidObjectExeption('Parameter is malformed');
  70. }
  71. $this->validateParameter($parameter);
  72. }
  73. }
  74. /**
  75. * @param array $parameter
  76. * @throws InvalidObjectExeption
  77. */
  78. protected function validateParameter(array $parameter) {
  79. if (!isset($parameter['type'])) {
  80. throw new InvalidObjectExeption('Object type is undefined');
  81. }
  82. $definition = $this->definitions->getDefinition($parameter['type']);
  83. $requiredParameters = $this->getRequiredParameters($parameter['type'], $definition);
  84. $missingKeys = array_diff($requiredParameters, array_keys($parameter));
  85. if (!empty($missingKeys)) {
  86. throw new InvalidObjectExeption('Object is invalid');
  87. }
  88. }
  89. /**
  90. * @param string $type
  91. * @param array $definition
  92. * @return string[]
  93. */
  94. protected function getRequiredParameters($type, array $definition) {
  95. if (isset($this->requiredParameters[$type])) {
  96. return $this->requiredParameters[$type];
  97. }
  98. $this->requiredParameters[$type] = [];
  99. foreach ($definition['parameters'] as $parameter => $data) {
  100. if ($data['required']) {
  101. $this->requiredParameters[$type][] = $parameter;
  102. }
  103. }
  104. return $this->requiredParameters[$type];
  105. }
  106. }