| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace JeroenDesloovere\VCard; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use JeroenDesloovere\VCard\Exception\VCardException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use JeroenDesloovere\VCard\Property\Address; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use JeroenDesloovere\VCard\Property\FullName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use JeroenDesloovere\VCard\Property\Name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use JeroenDesloovere\VCard\Property\NodeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use JeroenDesloovere\VCard\Property\Note; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use JeroenDesloovere\VCard\Property\Parameter\Kind; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use JeroenDesloovere\VCard\Property\Parameter\PropertyParameterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use JeroenDesloovere\VCard\Property\Parameter\Revision; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use JeroenDesloovere\VCard\Property\Parameter\Type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use JeroenDesloovere\VCard\Property\Parameter\Version; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use JeroenDesloovere\VCard\Property\PropertyInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | final class VCard | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public const POSSIBLE_VALUES = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         // All possible property parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         Version::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         Revision::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         Kind::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         Type::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         // All possible properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         Name::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         FullName::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         Address::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         Note::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var PropertyParameterInterface[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private $parameters = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @var PropertyInterface[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     private $properties = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function __construct(Kind $kind = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->add($kind ?? Kind::individual()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function add(NodeInterface $node): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if (array_key_exists(get_class($node), self::POSSIBLE_VALUES)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             throw VCardException::forNotAllowedNode($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         if ($node instanceof PropertyInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             if (!$node->isAllowedMultipleTimes() && $this->hasPropertyByClassName(get_class($node))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 throw VCardException::forExistingProperty($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $this->properties[] = $node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if ($node instanceof PropertyParameterInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             if ($this->hasPropertyByClassName(get_class($node))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 throw VCardException::forExistingPropertyParameter($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $this->parameters[] = $node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     public function getKind(): Kind | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         return $this->getParameters(Kind::class)[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public function getParameters(string $filterByPropertyParameterClass = null): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if ($filterByPropertyParameterClass === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             return $this->parameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return array_filter($this->parameters, function (PropertyParameterInterface $parameter) use ($filterByPropertyParameterClass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             return $parameter instanceof $filterByPropertyParameterClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function getProperties(string $filterByPropertyClass = null): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         if ($filterByPropertyClass === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             return $this->properties; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return array_filter($this->properties, function (PropertyInterface $property) use ($filterByPropertyClass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             return $property instanceof $filterByPropertyClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public function hasParameterByClassName(string $filterByParameterClass): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return count($this->getParameters($filterByParameterClass)) > 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     public function hasPropertyByClassName(string $filterByPropertyClass): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         return count($this->getProperties($filterByPropertyClass)) > 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 111 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |  |