Skip to content

Commit

Permalink
fix Name Property (wrong order: FN and N have different orders!) mand…
Browse files Browse the repository at this point in the history
…atory check for fullname
  • Loading branch information
helveticadomes committed Mar 5, 2019
1 parent 4220a45 commit 17d55da
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/Exception/VCardException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/Property/NameParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/Property/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions src/VCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 17d55da

Please sign in to comment.