Spaces:
No application file
No application file
File size: 1,420 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<?php
declare(strict_types=1);
namespace Mautic\LeadBundle\DataObject;
use Mautic\CoreBundle\Form\DataTransformer\BarStringTransformer;
use Mautic\LeadBundle\Exception\InvalidContactFieldTokenException;
/**
* A value object representation of a contact field token.
*/
class ContactFieldToken
{
private string $fieldAlias;
private ?string $defaultValue;
/**
* @throws InvalidContactFieldTokenException
*/
public function __construct(
private string $fullToken
) {
$this->parse(trim($fullToken));
}
public function getFullToken(): string
{
return $this->fullToken;
}
public function getFieldAlias(): string
{
return $this->fieldAlias;
}
public function getDefaultValue(): ?string
{
return $this->defaultValue;
}
private function parse(string $fullToken): void
{
preg_match('/^{contactfield=(.*?)}$/', $fullToken, $matches);
if (empty($matches[1])) {
throw new InvalidContactFieldTokenException("'{$fullToken}' is not a valid contact field token. A valid token example: '{contactfield=firstname|John}'");
}
$barStringTransformer = new BarStringTransformer();
$array = $barStringTransformer->reverseTransform($matches[1]);
$this->fieldAlias = $array[0];
$this->defaultValue = $array[1] ?? null;
}
}
|