Symfony Serializer

This commit is contained in:
David Bomba 2024-09-07 17:27:00 +10:00
parent 78367c1e2d
commit c0483d727f
4 changed files with 249 additions and 1 deletions

View File

@ -13,6 +13,7 @@
namespace App\PaymentDrivers\CBAPowerBoard; namespace App\PaymentDrivers\CBAPowerBoard;
use App\Models\ClientGatewayToken; use App\Models\ClientGatewayToken;
use App\PaymentDrivers\CBAPowerBoard\Models\Customer as ModelsCustomer;
use App\PaymentDrivers\CBAPowerBoardPaymentDriver; use App\PaymentDrivers\CBAPowerBoardPaymentDriver;
class Customer class Customer
@ -31,7 +32,7 @@ class Customer
->first(); ->first();
if($token && $customer = $this->getCustomer($token->gateway_customer_reference)){ if($token && $customer = $this->getCustomer($token->gateway_customer_reference)){
return $customer->resource->data; return (new \App\PaymentDrivers\CBAPowerBoard\Models\Parse())->decode(ModelsCustomer::class, $customer->resource->data);
} }
if($customer = $this->findCustomer()) if($customer = $this->findCustomer())
@ -113,6 +114,7 @@ class Customer
// 'address_postcode' => $this->powerboard->client->postal_code ?? '', // 'address_postcode' => $this->powerboard->client->postal_code ?? '',
$payload = [ $payload = [
'company_name' => $this->powerboard->client->present()->name(),
'first_name' => $this->powerboard->client->present()->first_name(), 'first_name' => $this->powerboard->client->present()->first_name(),
'last_name' => $this->powerboard->client->present()->first_name(), 'last_name' => $this->powerboard->client->present()->first_name(),
'email' => $this->powerboard->client->present()->email(), 'email' => $this->powerboard->client->present()->email(),

View File

@ -0,0 +1,84 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\CBAPowerBoard\Models;
class Customer
{
/** @var string */
public string $_id;
/** @var string */
public string $_source_ip_address;
/** @var string */
public string $first_name;
/** @var string */
public string $last_name;
/** @var string */
public string $email;
/** @var string */
public string $reference;
/** @var string */
public string $default_source;
/** @var string */
public string $status;
/** @var bool */
public bool $archived;
/** @var string */
public string $created_at;
/** @var string */
public string $updated_at;
/** @var bool */
public bool $_check_expire_date;
/** @var PaymentSources[] */
public array $payment_sources;
/** @var array */
public array $payment_destinations;
/** @var string */
public string $company_id;
/**
* @param PaymentSources[] $payment_sources
*/
public function __construct(
string $_id,
string $_source_ip_address,
string $first_name,
string $last_name,
string $email,
string $reference,
string $default_source,
string $status,
bool $archived,
string $created_at,
string $updated_at,
bool $_check_expire_date,
array $payment_sources,
array $payment_destinations,
string $company_id
) {
$this->_id = $_id;
$this->_source_ip_address = $_source_ip_address;
$this->first_name = $first_name;
$this->last_name = $last_name;
$this->email = $email;
$this->reference = $reference;
$this->default_source = $default_source;
$this->status = $status;
$this->archived = $archived;
$this->created_at = $created_at;
$this->updated_at = $updated_at;
$this->_check_expire_date = $_check_expire_date;
$this->payment_sources = $payment_sources;
$this->payment_destinations = $payment_destinations;
$this->company_id = $company_id;
}
}

View File

@ -0,0 +1,70 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\CBAPowerBoard\Models;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
class Parse
{
public function __construct()
{
}
public function decode($object_type, $document)
{
$phpDocExtractor = new PhpDocExtractor();
$reflectionExtractor = new ReflectionExtractor();
// list of PropertyTypeExtractorInterface (any iterable)
$typeExtractors = [$reflectionExtractor,$phpDocExtractor];
// list of PropertyDescriptionExtractorInterface (any iterable)
$descriptionExtractors = [$phpDocExtractor];
// list of PropertyInitializableExtractorInterface (any iterable)
$propertyInitializableExtractors = [$reflectionExtractor];
$propertyInfo = new PropertyInfoExtractor(
$propertyInitializableExtractors,
$descriptionExtractors,
$typeExtractors,
);
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
$metadataAwareNameConverter = new MetadataAwareNameConverter($classMetadataFactory);
$normalizer = new ObjectNormalizer($classMetadataFactory, $metadataAwareNameConverter, null, $propertyInfo);
$normalizers = [new DateTimeNormalizer(), $normalizer, new ArrayDenormalizer()];
$encoders = [new JsonEncoder()];
$serializer = new Serializer($normalizers, $encoders);
nlog($document);
$data = $serializer->deserialize(json_encode($document), $object_type, 'json', [\Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);
return $data;
}
}

View File

@ -0,0 +1,92 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\CBAPowerBoard\Models;
class PaymentSources
{
/** @var string */
public string $_id;
/** @var string */
public string $type;
/** @var string */
public string $vault_token;
/** @var string */
public string $card_name;
/** @var string */
public string $card_number_bin;
/** @var string */
public string $card_number_last4;
/** @var string */
public string $card_scheme;
/** @var string|null */
public ?string $address_line1;
/** @var string|null */
public ?string $address_line2;
/** @var string|null */
public ?string $address_city;
/** @var string|null */
public ?string $address_country;
/** @var string|null */
public ?string $address_state;
/** @var int */
public int $expire_month;
/** @var int */
public int $expire_year;
/** @var string */
public string $status;
/** @var string */
public string $created_at;
/** @var string */
public string $updated_at;
/** @var string */
public string $vault_type;
public function __construct(
string $_id,
string $type,
string $vault_token,
string $card_name,
string $card_number_bin,
string $card_number_last4,
string $card_scheme,
?string $address_line1,
?string $address_line2,
?string $address_city,
?string $address_country,
?string $address_state,
int $expire_month,
int $expire_year,
string $status,
string $created_at,
string $updated_at,
string $vault_type
) {
$this->_id = $_id;
$this->type = $type;
$this->vault_token = $vault_token;
$this->card_name = $card_name;
$this->card_number_bin = $card_number_bin;
$this->card_number_last4 = $card_number_last4;
$this->card_scheme = $card_scheme;
$this->address_line1 = $address_line1;
$this->address_line2 = $address_line2;
$this->address_city = $address_city;
$this->address_country = $address_country;
$this->address_state = $address_state;
$this->expire_month = $expire_month;
$this->expire_year = $expire_year;
$this->status = $status;
$this->created_at = $created_at;
$this->updated_at = $updated_at;
$this->vault_type = $vault_type;
}
}