From 0404f6f1b95cb95f5b2e090ba61a9615d0c6fbac Mon Sep 17 00:00:00 2001 From: helveticadomes Date: Mon, 4 Mar 2019 10:53:59 +0100 Subject: [PATCH] fix Name Property (wrong order: FN and N have different orders!) mandatory check for fullname --- src/Exception/VCardException.php | 7 +++++++ src/Parser/Property/NameParser.php | 2 +- src/Property/Name.php | 4 ++-- src/VCard.php | 29 +++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/Exception/VCardException.php b/src/Exception/VCardException.php index 5a33f77..01302a4 100644 --- a/src/Exception/VCardException.php +++ b/src/Exception/VCardException.php @@ -12,6 +12,13 @@ class VCardException extends \Exception { + public static function forRequiredProperty(PropertyInterface $property): self + { + return new self( + 'The property "' . get_class($property) . '" is mandatory, and you need to add it once.' + ); + } + public static function forExistingProperty(PropertyInterface $property): self { return new self( diff --git a/src/Parser/Property/NameParser.php b/src/Parser/Property/NameParser.php index a91233e..39ae37a 100644 --- a/src/Parser/Property/NameParser.php +++ b/src/Parser/Property/NameParser.php @@ -11,7 +11,7 @@ final class NameParser extends PropertyParser implements NodeParserInterface { public function parseVcfString(string $value, array $parameters = []): NodeInterface { - @list($firstName, $additional, $lastName, $prefix, $suffix) = explode(';', $value); + @list($lastName, $firstName, $additional, $prefix, $suffix) = explode(';', $value); $this->convertEmptyStringToNull([ $lastName, diff --git a/src/Property/Name.php b/src/Property/Name.php index 632dca3..2dca144 100644 --- a/src/Property/Name.php +++ b/src/Property/Name.php @@ -13,13 +13,13 @@ final class Name implements PropertyInterface, NodeInterface { /** @var null|string */ - private $additional; + private $lastName; /** @var null|string */ private $firstName; /** @var null|string */ - private $lastName; + private $additional; /** @var null|string */ private $prefix; diff --git a/src/VCard.php b/src/VCard.php index ae7b9a5..790d405 100644 --- a/src/VCard.php +++ b/src/VCard.php @@ -160,9 +160,38 @@ public function getParameters(string $filterByPropertyParameterClass = null): ar }); } + /** + * @param string $forPropertyClass + * @return array + * @throws VCardException + */ public function getProperties(string $forPropertyClass = null): array { if ($forPropertyClass === null) { + $array = $this->properties; + //make empty array var for each required Property or Property you need to work with + $found_fullname = $found_name = $others = []; + foreach ($array as $value) { + //search if property exist and add it to the defined array var, else add it to the others array + if ($value instanceof FullName) { + $found_fullname[] = $value; + } elseif ($value instanceof Name) { + $found_name[] = $value; + } else { + $others[] = $value; + } + } + //check for empty and throw exception, if it can not be generated by existing fields + if (count($found_fullname) == 0) { + if (count($found_name) == 0) { + throw VCardException::forRequiredProperty(new FullName('NoName')); + } else { + $found_fullname[] = new FullName(implode(' ', array_filter(array($found_name[0]->getPrefix(), $found_name[0]->getLastName(), $found_name[0]->getAdditional(), $found_name[0]->getFirstName(), $found_name[0]->getSuffix())))); + } + } + $array = array_merge($found_fullname, $found_name, $others); + $this->properties = $array; + return $this->properties; }