|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\XML\Type; |
| 6 | + |
| 7 | +/** |
| 8 | + * Abstract class to be implemented by all types |
| 9 | + * |
| 10 | + * @package simplesamlphp/xml-common |
| 11 | + */ |
| 12 | +abstract class AbstractValueType implements ValueTypeInterface |
| 13 | +{ |
| 14 | + /** @var string */ |
| 15 | + protected string $content; |
| 16 | + |
| 17 | + |
| 18 | + /** |
| 19 | + * Set the content of the type. |
| 20 | + * |
| 21 | + * @param string $content |
| 22 | + */ |
| 23 | + protected function __construct(string $content) |
| 24 | + { |
| 25 | + $this->validateContent($content); |
| 26 | + $this->content = $content; |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + /** |
| 31 | + * Get the content. |
| 32 | + * |
| 33 | + * @return string |
| 34 | + */ |
| 35 | + public function getContent(): string |
| 36 | + { |
| 37 | + return $this->sanitizeContent($this->getRawContent()); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + /** |
| 42 | + * Get the raw content unsanitized content. |
| 43 | + * |
| 44 | + * @return string |
| 45 | + */ |
| 46 | + public function getRawContent(): string |
| 47 | + { |
| 48 | + return $this->content; |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * Sanitize the content of the element. |
| 54 | + * |
| 55 | + * @param string $content The value to go in the XML textContent |
| 56 | + * @throws \Exception on failure |
| 57 | + * @return string |
| 58 | + */ |
| 59 | + protected function sanitizeContent(string $content): string |
| 60 | + { |
| 61 | + /** |
| 62 | + * Perform no sanitation by default. |
| 63 | + * Override this method on the implementing class to perform content sanitation. |
| 64 | + */ |
| 65 | + return $content; |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + /** |
| 70 | + * Validate the content of the element. |
| 71 | + * |
| 72 | + * @param string $content The value to go in the XML textContent |
| 73 | + * @throws \Exception on failure |
| 74 | + * @return void |
| 75 | + */ |
| 76 | + protected function validateContent(/** @scrutinizer ignore-unused */ string $content): void |
| 77 | + { |
| 78 | + /** |
| 79 | + * Perform no validation by default. |
| 80 | + * Override this method on the implementing class to perform content validation. |
| 81 | + */ |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + /** |
| 86 | + * @param string $value |
| 87 | + * @return \SimpleSAML\XML\Type\ValueTypeInterface |
| 88 | + */ |
| 89 | + public static function fromString(string $value): static |
| 90 | + { |
| 91 | + return new static($value); |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + /** |
| 96 | + * Output the value as a string |
| 97 | + * |
| 98 | + * @return string |
| 99 | + */ |
| 100 | + public function __toString(): string |
| 101 | + { |
| 102 | + return $this->getContent(); |
| 103 | + } |
| 104 | +} |
0 commit comments