-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSpecification.php
More file actions
66 lines (58 loc) · 1.59 KB
/
Specification.php
File metadata and controls
66 lines (58 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* Copyright (c) Benjamin Ansbach - all rights reserved.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Written by Benjamin Ansbach <benjaminansbach@gmail.com>, 2019
*/
declare(strict_types = 1);
namespace Techworker\Ssml;
use Techworker\Ssml\Element\Speak;
use Techworker\Ssml\Specification\Exception\InvalidElementException;
abstract class Specification
{
/**
* Gets a list of element classes that are allowed in the specification.
*
* @return array
*/
abstract protected function getAllowedElements(): array;
/**
* Validates a single element.
*
* @param BaseElement $element
* @return mixed
*/
abstract protected function validateElement(BaseElement $element);
/**
* Validates a complete element tree.
*
* @param Speak $root
* @throws SsmlException
*/
public function validate(Speak $root)
{
$this->validateAllowed($root);
$this->validateElement($root);
foreach ($root->getIterator() as $el) {
$this->validateElement($el);
}
}
/**
* Validates a complete element tree.
*
* @param Speak $root
* @throws SsmlException
*/
protected function validateAllowed(Speak $root)
{
$allowedElements = $this->getAllowedElements();
foreach ($root->getIterator() as $el) {
if (!in_array(get_class($el), $allowedElements, true)) {
throw new InvalidElementException($el, 'alexa');
}
}
}
}