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

Parse field value by provided type #3

Merged
merged 8 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"require": {
"php": "~8.2",
"jeckel-lab/etl": "dev-main",
"jeckel-lab/identity-contract": "^2.0"
"jeckel-lab/identity-contract": "^2.0",
"ext-intl": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.34",
Expand Down
69 changes: 69 additions & 0 deletions src/Parser/FieldParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace JeckelLab\MauticWebhookParser\Parser;

use DateTimeImmutable;
use JeckelLab\MauticWebhookParser\ValueObject\Email;
use JeckelLab\MauticWebhookParser\ValueObject\Locale;
use LogicException;

use function JeckelLab\MauticWebhookParser\toNullableDateTime;

final readonly class FieldParser
{
/**
* @param array{alias: string, group: string, id: int, label: string, type: string, value: mixed} $fieldData
* @return mixed
*/
public function parseField(array $fieldData): mixed
{
return match($fieldData['type']) {
'country', 'lookup', 'region', 'select', 'tel', 'text', 'timezone', 'url' => $this->parseTextField($fieldData['value']),
'number' => $this->parseNumberField($fieldData['value']),
'datetime' => $this->parseDateTimeField($fieldData['value']),
'boolean' => $this->parseBooleanField($fieldData['value']),
'email' => $this->parseEmailField($fieldData['value']),
'multiselect' => $this->parseMultiselectField($fieldData['value']),
'locale' => $this->parseLocaleField($fieldData['value']),
default => null
};
}

private function parseTextField(mixed $value): ?string
{
return is_string($value) ? $value : null;
}

private function parseNumberField(mixed $value): ?int
{
return is_int($value) ? $value : null;
}

private function parseDateTimeField(mixed $value): ?DateTimeImmutable
{
return toNullableDateTime($value);
}

private function parseBooleanField(mixed $value): ?bool
{
return is_bool($value) ? $value : null;
}

private function parseEmailField(mixed $value): ?Email
{
return is_string($value) ? new Email($value) : null;
}

/**
* @return string[]|null
*/
private function parseMultiselectField(mixed $value): ?array
{
return is_string($value) ? explode('|', $value) : null;
}

private function parseLocaleField(mixed $value): ?Locale
{
return is_string($value) ? new Locale($value) : null;
}
}
10 changes: 10 additions & 0 deletions src/ValueObject/Email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace JeckelLab\MauticWebhookParser\ValueObject;

final readonly class Email
{
public function __construct(
public string $email
) {}
}
28 changes: 28 additions & 0 deletions src/ValueObject/Locale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* @author: Julien Mercier-Rojas <julien@jeckel-lab.fr>
* Created at: 18/10/2023
*/

declare(strict_types=1);

namespace JeckelLab\MauticWebhookParser\ValueObject;

use JeckelLab\MauticWebhookParser\Exception\InvalidArgumentException;
use ResourceBundle;

final readonly class Locale
{
public function __construct(
public string $locale
) {
static $locales = null;
if ($locales === null) {
$locales = ResourceBundle::getLocales('');
}
if (!in_array($locale, $locales, true)) {
throw new InvalidArgumentException("Invalid locale $locale provided");
}
}
}
160 changes: 160 additions & 0 deletions tests/Parser/FieldParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

namespace JeckelLab\MauticWebhookParser\Tests\Parser;

use DateTimeImmutable;
use JeckelLab\MauticWebhookParser\Parser\FieldParser;
use JeckelLab\MauticWebhookParser\ValueObject\Email;
use JeckelLab\MauticWebhookParser\ValueObject\Locale;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;

/**
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
class FieldParserTest extends TestCase
{
#[TestWith(['country', 'my country value'])]
#[TestWith(['lookup', 'Mr.'])]
#[TestWith(['region', 'my region value'])]
#[TestWith(['select', 'my select value'])]
#[TestWith(['tel', '01 23 45 67 89'])]
#[TestWith(['text', 'my text value'])]
#[TestWith(['timezone', 'Europe/Paris'])]
#[TestWith(['url', 'https://jeckel-lab.fr'])]
#[TestDox('Parse field of type $type with value $value should return string')]
public function testParseFieldWhichShouldReturnString(string $type, string $value): void
{
$data = [
'alias' => 'address1',
'group' => 'core',
'id' => 10,
'label' => 'Address Line 1',
'type' => $type,
'value' => $value
];
$parsedData = (new FieldParser())->parseField($data);
self::assertIsString($parsedData);
self::assertSame($value, $parsedData);
}

public function testParseNumberFieldReturnInt(): void
{
$data = [
'alias' => 'attribution',
'group' => 'core',
'id' => 18,
'label' => 'Attribution',
'type' => 'number',
'value' => 32
];
$parsedData = (new FieldParser())->parseField($data);
self::assertIsInt($parsedData);
self::assertSame(32, $parsedData);
}

public function testParseDatetimeFieldReturnDateTimeImmutable(): void
{
$data = [
'alias' => 'attribution_date',
'group' => 'core',
'id' => 17,
'label' => 'Attribution Date',
'type' => 'datetime',
'value' => "2017-06-14 11:30:00"
];
$parsedData = (new FieldParser())->parseField($data);
self::assertInstanceOf(DateTimeImmutable::class, $parsedData);
self::assertEquals("2017-06-14 11:30:00", $parsedData->format('Y-m-d H:i:s'));
}

public function testParseBooleanFieldReturnBoolean(): void
{
$data = [
'alias' => 'boolean',
'group' => 'core',
'id' => 44,
'label' => 'boolean',
'type' => 'boolean',
'value' => false
];
$parsedData = (new FieldParser())->parseField($data);
self::assertIsBool($parsedData);
self::assertFalse($parsedData);
}

public function testParseEmailFieldReturnEmail(): void
{
$data = [
'alias' => 'email',
'group' => 'core',
'id' => 6,
'label' => 'Email',
'type' => 'email',
'value' => "john@doe.name"
];
$parsedData = (new FieldParser())->parseField($data);
self::assertInstanceOf(Email::class, $parsedData);
self::assertSame("john@doe.name", $parsedData->email);
}

public function testParseLocaleFieldReturnLocale(): void
{
$data = [
'alias' => 'preferred_locale',
'group' => 'core',
'id' => 6,
'label' => 'Preferred Locale',
'type' => 'locale',
'value' => "fr_FR"
];
$parsedData = (new FieldParser())->parseField($data);
self::assertInstanceOf(Locale::class, $parsedData);
self::assertSame("fr_FR", $parsedData->locale);
}

public function testParseFieldOfTypeMultiselectShouldReturnArray(): void
{
$data = [
'alias' => 'multiselect',
'group' => 'core',
'id' => 42,
'label' => 'Multiselect',
'type' => 'multiselect',
'value' => "php|js"
];
$parsedData = (new FieldParser())->parseField($data);
self::assertIsArray($parsedData);
self::assertEquals(['php', 'js'], $parsedData);
}

#[TestWith(['boolean'])]
#[TestWith(['country'])]
#[TestWith(['datetime'])]
#[TestWith(['email'])]
#[TestWith(['locale'])]
#[TestWith(['lookup'])]
#[TestWith(['multiselect'])]
#[TestWith(['number'])]
#[TestWith(['region'])]
#[TestWith(['select'])]
#[TestWith(['tel'])]
#[TestWith(['text'])]
#[TestWith(['timezone'])]
#[TestWith(['url'])]
#[TestDox('Parse field of type $type with null value should return null')]
public function testParseFieldWithNullValueReturnNull(string $type): void
{
$data = [
'alias' => 'address1',
'group' => 'core',
'id' => 10,
'label' => 'Address Line 1',
'type' => $type,
'value' => null
];
$parsedData = (new FieldParser())->parseField($data);
self::assertNull($parsedData);
}
}
38 changes: 38 additions & 0 deletions tests/ValueObject/LocaleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* @author: Julien Mercier-Rojas <julien@jeckel-lab.fr>
* Created at: 18/10/2023
*/

namespace JeckelLab\MauticWebhookParser\Tests\ValueObject;

use JeckelLab\MauticWebhookParser\Exception\InvalidArgumentException;
use JeckelLab\MauticWebhookParser\ValueObject\Locale;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;

class LocaleTest extends TestCase
{
#[TestWith(['fr_FR'])]
#[TestWith(['fr_CA'])]
#[TestWith(['en_US'])]
#[TestWith(['en_GB'])]
#[TestDox('Create locale $localeToTest with should return Locale instance')]
public function testValidLocaleShouldReturnLocaleInstance(string $localeToTest): void
{
$locale = new Locale($localeToTest);
self::assertSame($localeToTest, $locale->locale);
}

#[TestWith(['fr_DE'])]
#[TestWith(['en_UK'])]
#[TestWith(['foobar'])]
#[TestDox('Create locale $localeToTest with should fail')]
public function testWithInvalidLocaleShouldFail(string $localeToTest): void
{
$this->expectException(InvalidArgumentException::class);
new Locale($localeToTest);
}
}
Loading