Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vCard 4.0: fix Name Property #146

Open
wants to merge 13 commits into
base: 2.0.0-dev
Choose a base branch
from
Open
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]->getFirstName(), $found_name[0]->getAdditional(), $found_name[0]->getLastName(), $found_name[0]->getSuffix()))));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be made more readable, for example something like:

$found_fullname[] = new FullName(implode(' ', array_filter(array(
    $found_name[0]->getPrefix(), 
    $found_name[0]->getFirstName(), 
    $found_name[0]->getAdditional(), 
    $found_name[0]->getLastName(), 
    $found_name[0]->getSuffix(),
))));

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will fix this for helveticadomes.

}
}
$array = array_merge($found_fullname, $found_name, $others);
$this->properties = $array;

return $this->properties;
}

Expand Down
Loading