| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace SimpleSAML\SAML2\XML\mdui; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use DOMElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SimpleSAML\SAML2\Assert\Assert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SimpleSAML\SAML2\Exception\{ArrayValidationException, ProtocolViolationException}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SimpleSAML\SAML2\Type\ListOfStringsValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SimpleSAML\XML\ArrayizableElementInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SimpleSAML\XML\TypedTextContentTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use SimpleSAML\XMLSchema\Type\LanguageValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use function array_key_first; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Class for handling the Keywords metadata extensions for login and discovery user interface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @link: http://docs.oasis-open.org/security/saml/Post2.0/sstc-saml-metadata-ui/v1.0/sstc-saml-metadata-ui-v1.0.pdf | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @package simplesamlphp/saml2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | final class Keywords extends AbstractMduiElement implements | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     ArrayizableElementInterface, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     SchemaValidatableElementInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     use SchemaValidatableElementTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     use TypedTextContentTrait; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public const TEXTCONTENT_TYPE = ListOfStringsValue::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Initialize a Keywords. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param \SimpleSAML\XMLSchema\Type\LanguageValue $lang | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param \SimpleSAML\SAML2\Type\ListOfStringsValue $keywords | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         protected LanguageValue $lang, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         ListOfStringsValue $keywords, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->setContent($keywords); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * Collect the value of the lang-property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return \SimpleSAML\XMLSchema\Type\LanguageValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function getLanguage(): LanguageValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         return $this->lang; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * Convert XML into a Keywords | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @param \DOMElement $xml The XML element we should load | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @return static | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      *   if the qualified name of the supplied element is wrong | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      *   if the supplied element is missing one of the mandatory attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public static function fromXML(DOMElement $xml): static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         Assert::same($xml->localName, 'Keywords', InvalidDOMElementException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         Assert::same($xml->namespaceURI, Keywords::NS, InvalidDOMElementException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         Assert::stringNotEmpty($xml->textContent, 'Missing value for Keywords.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $lang = self::getAttribute($xml, 'xml:lang', LanguageValue::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $Keywords = ListOfStringsValue::fromString($xml->textContent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return new static($lang, $Keywords); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * Convert this Keywords to XML. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param \DOMElement|null $parent The element we should append this Keywords to. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return \DOMElement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function toXML(?DOMElement $parent = null): DOMElement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         /** @psalm-var \DOMDocument $e->ownerDocument */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $e = $this->instantiateParentElement($parent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $e->setAttribute('xml:lang', $this->getLanguage()->getValue()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $e->textContent = $this->getContent()->getValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * Create a class from an array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     public static function fromArray(array $data): static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         Assert::notEmpty($data, ArrayValidationException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         Assert::count($data, 1, ArrayValidationException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $lang = LanguageValue::fromString(array_key_first($data)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $keywords = $data[$lang->getValue()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         Assert::allNotContains($keywords, '+', ProtocolViolationException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         return new static($lang, ListOfStringsValue::fromArray($keywords)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * Create an array from this class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     public function toArray(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         return [$this->getLanguage()->getValue() => $this->getContent()->toArray()]; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |  |