Skip to content

Commit

Permalink
vcard field alias
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreLebedel committed Oct 25, 2024
1 parent 46770ba commit 6633c0d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/VCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class VCard

public function __construct()
{
$this->formattedData = new stdClass;
$this->formattedData = new stdClass();
$this->rawData = new stdClass;
$this->unexpectedData = new stdClass;
}

public function getVersion(): string
{
return $this->formattedData->version;
return $this->formattedData->version ?? '4.0';
}
}
24 changes: 21 additions & 3 deletions src/VCardField.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class VCardField

public mixed $formattedValue = null;

public ?string $formattedName = null;

public mixed $rawValue = null;

public array $attributes = [];
Expand All @@ -40,6 +42,7 @@ public function __construct(public string $rawContents)
$this->value = $value;
$this->attributes = explode(';', $nameAttributes);
$this->name = mb_strtolower($this->attributes[0]);
$this->formattedName = $this->name;
array_shift($this->attributes);

$this->parseAttributes();
Expand Down Expand Up @@ -84,6 +87,15 @@ protected function parseAttributes()
$this->attributes = $newAttributes;
}

public function version(): self
{
$this->isString = true;
$this->formattedValue = $this->value;
$this->rawValue = $this->value;

return $this;
}

public function string(): self
{
$this->isString = true;
Expand Down Expand Up @@ -225,6 +237,12 @@ public function timezone(): self
return $this;
}

public function as(string $formattedName) :self
{
$this->formattedName = $formattedName;
return $this;
}

public function in(array $stringPossibilities): self
{
if (! is_string($this->value) || ! in_array($this->value, $stringPossibilities)) {
Expand Down Expand Up @@ -313,13 +331,13 @@ public function render(VCard $vCard)

if ($this->isMultiple) {
if (is_string($this->formattedValue)) {
$vCard->formattedData->{$this->name} = explode(',', $this->formattedValue);
$vCard->formattedData->{$this->formattedName} = explode(',', $this->formattedValue);
} else {
$vCard->formattedData->{$this->name}[] = $this->formattedValue;
$vCard->formattedData->{$this->formattedName}[] = $this->formattedValue;
}

} else {
$vCard->formattedData->{$this->name} = $this->formattedValue;
$vCard->formattedData->{$this->formattedName} = $this->formattedValue;

}
}
Expand Down
20 changes: 10 additions & 10 deletions src/VCardParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ protected function parseLine(int $lineNumber, string $lineContents): void
'region',
'postalCode',
'country',
])->multiple()->addAttribute('type', ['dom', 'intl', 'postal', 'parcel', 'home', 'work', 'pref'])->addAttribute('label'),
])->multiple()->addAttribute('type', ['dom', 'intl', 'postal', 'parcel', 'home', 'work', 'pref'])->addAttribute('label')->as('addresses'),
'agent' => $field->string(),
'anniversary' => $field->datetime(),
'bday' => $field->datetime(),
'bday' => $field->datetime()->as('birthday'),
'caladruri' => $field->uri(),
'caluri' => $field->uri()->addAttribute('type'),
'categories' => $field->string()->multiple()->addAttribute('type'),
Expand All @@ -173,9 +173,9 @@ protected function parseLine(int $lineNumber, string $lineContents): void
'pid',
'uri',
])->multiple(),
'email' => $field->object()->multiple()->addAttribute('type'),
'email' => $field->object()->multiple()->addAttribute('type')->as('emails'),
'fburl' => $field->uri()->addAttribute('type'),
'fn' => $field->string()->addAttribute('type'),
'fn' => $field->string()->addAttribute('type')->as('fullName'),
'gender' => $field->string(),
'geo' => $field->coordinates()->addAttribute('type'),
'impp' => $field->object()->multiple()->addAttribute('type', ['personal', 'business', 'home', 'work', 'mobile', 'pref']),
Expand All @@ -189,8 +189,8 @@ protected function parseLine(int $lineNumber, string $lineContents): void
'region',
'postalCode',
'country',
])->multiple()->addAttribute('type', ['dom', 'intl', 'postal', 'parcel', 'home', 'work', 'pref']),
'lang' => $field->object()->multiple()->addAttribute('type'),
])->multiple()->addAttribute('type', ['dom', 'intl', 'postal', 'parcel', 'home', 'work', 'pref'])->as('labels'),
'lang' => $field->object()->multiple()->addAttribute('type')->as('langs'),
'logo' => $field->uri()->addAttribute('type'),
'mailer' => $field->string(),
'member' => $field->uri(),
Expand All @@ -200,8 +200,8 @@ protected function parseLine(int $lineNumber, string $lineContents): void
'middleName',
'namePrefix',
'nameSuffix',
]),
'nickname' => $field->string()->multiple()->addAttribute('type'),
])->as('name'),
'nickname' => $field->string()->multiple()->addAttribute('type')->as('nicknames'),
'note' => $field->string()->addAttribute('type'),
'org' => $field->array([
'name',
Expand All @@ -217,12 +217,12 @@ protected function parseLine(int $lineNumber, string $lineContents): void
'sort-string' => $field->string(),
'sound' => $field->uri()->addAttribute('type'),
'source' => $field->uri(),
'tel' => $field->object()->multiple()->addAttribute('type', ['home', 'msg', 'work', 'pref', 'voice', 'fax', 'cell', 'video', 'pager', 'bbs', 'modem', 'car', 'isdn', 'pcs']),
'tel' => $field->object()->multiple()->addAttribute('type', ['home', 'msg', 'work', 'pref', 'voice', 'fax', 'cell', 'video', 'pager', 'bbs', 'modem', 'car', 'isdn', 'pcs'])->as('phones'),
'title' => $field->string()->addAttribute('type'),
'tz' => $field->timezone()->addAttribute('type'),
'uid' => $field->string()->ltrim(['urn:uuid:']),
'url' => $field->uri()->addAttribute('type'),
'version' => $field->string(),
'version' => $field->version(),
'xml' => $field->string(),
default => $field->unexpected(),
};
Expand Down

0 comments on commit 6633c0d

Please sign in to comment.