Skip to content

Commit

Permalink
Fixes #112
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroendesloovere committed Jan 3, 2018
1 parent 8ddc0bb commit d190f02
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Exception/VCardException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace JeroenDesloovere\VCard\Exception;

use JeroenDesloovere\VCard\Property\NodeInterface;
use JeroenDesloovere\VCard\Property\Parameter\Kind;
use JeroenDesloovere\VCard\Property\Parameter\PropertyParameterInterface;
use JeroenDesloovere\VCard\Property\PropertyInterface;
use JeroenDesloovere\VCard\VCard;
Expand Down Expand Up @@ -30,4 +31,11 @@ public static function forNotAllowedNode(NodeInterface $node): self
. implode(', ', VCard::POSSIBLE_VALUES)
);
}

public static function forNotAllowedPropertyOnVCardKind(PropertyInterface $property, Kind $kind): self
{
return new self(
'The property "' . get_class($property) . '" you are trying to add can only be added to vCard\'s of the ' . $kind->__toString() . ' kind.'
);
}
}
14 changes: 13 additions & 1 deletion src/VCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ final class VCard
Anniversary::class,
];

protected const ONLY_APPLY_TO_INDIVIDUAL_KIND = [
Anniversary::class,
Gender::class,
];

/**
* @var PropertyParameterInterface[]
*/
Expand All @@ -53,7 +58,7 @@ public function __construct(Kind $kind = null)

public function add(NodeInterface $node): self
{
if (array_key_exists(get_class($node), self::POSSIBLE_VALUES)) {
if (!in_array(get_class($node), self::POSSIBLE_VALUES)) {
throw VCardException::forNotAllowedNode($node);
}

Expand All @@ -71,15 +76,22 @@ public function add(NodeInterface $node): self

private function addProperty(PropertyInterface $property): void
{
// Property is not allowed multiple times
if (!$property->isAllowedMultipleTimes() && $this->hasPropertyByClassName(get_class($property))) {
throw VCardException::forExistingProperty($property);
}

// Property must only be applied to "individual" kind
if (!$this->getKind()->isIndividual() && in_array(get_class($property), self::ONLY_APPLY_TO_INDIVIDUAL_KIND)) {
throw VCardException::forNotAllowedPropertyOnVCardKind($property, Kind::individual());
}

$this->properties[] = $property;
}

private function addPropertyParameter(PropertyParameterInterface $propertyParameter): void
{
// Property is not allowed multiple times
if ($this->hasPropertyByClassName(get_class($propertyParameter))) {
throw VCardException::forExistingPropertyParameter($propertyParameter);
}
Expand Down

0 comments on commit d190f02

Please sign in to comment.