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

[Feat] WIP: Custom field implementation #53

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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
16 changes: 10 additions & 6 deletions src/Director/BodyTo/BodyToOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@ class BodyToOrder
*/
public static function build(array $data): Order
{
$order = (new OrderInternal(false))
$order = new OrderInternal(
allowEmptyRelations: false
);

$order
->setCreatedAt(self::date($data, 'created_at'))
->setDeletedAt($data['deleted_at'] ? self::date($data, 'deleted_at') : null)
->setFirst($data['is_first'])
->setHidden($data['is_hidden'])
->setId($data['id'])
->setInvoiceNumber($data['invoice_number'])
->setInvoiceStatus(InvoiceStatus::from($data['invoice_status']))
->setMode(Mode::from($data['mode']))
->setReference($data['reference'])
->setSource(Source::tryFrom($data['source'] ?? '') ?? Source::UNKNOWN)
->setUpdatedAt(self::date($data, 'updated_at'))
->setHidden($data['is_hidden'])
->setMode(Mode::from($data['mode']))
->setAmount((float) $data['amount'])
->setAmountWithTax((float) $data['amount_with_tax'])
->setUpdatedAt(self::date($data, 'updated_at'));
->setAmountWithTax((float) $data['amount_with_tax']);
flavio-schoute marked this conversation as resolved.
Show resolved Hide resolved

if (isset($data['billing'])) {
$order->setBilling(BodyToOrderBilling::build($data['billing']));
Expand Down Expand Up @@ -85,7 +89,7 @@ public static function buildMulti(array $data): array
* @throws DecodeResponseException
* @codeCoverageIgnore
*/
private static function date(array $data, string $field): DateTimeImmutable
private function date(array $data, string $field): DateTimeImmutable
{
try {
return new DateTimeImmutable($data[$field]);
Expand Down
62 changes: 62 additions & 0 deletions src/Entity/CustomField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace PlugAndPay\Sdk\Entity;

class CustomField
{
private int $id;
private int $customFieldId;

private string $input;
private string $label;

public function id(): int
{
return $this->id;
}

public function setId(int $id): self
{
$this->id = $id;

return $this;
}

public function customFieldId(): int
{
return $this->customFieldId;
}

public function setCustomFieldId(int $customFieldId): self
{
$this->customFieldId = $customFieldId;

return $this;
}

public function input(): string
{
return $this->input;
}

public function setInput(string $input): self
{
$this->input = $input;

return $this;
}

public function label(): string
{
return $this->input;
}

public function setLabel(string $label): self
{
$this->label = $label;

return $this;
}
}
23 changes: 23 additions & 0 deletions src/Entity/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use BadFunctionCallException;
use PlugAndPay\Sdk\Enum\ItemType;
use PlugAndPay\Sdk\Exception\RelationNotLoadedException;

class Item
{
Expand All @@ -20,6 +21,9 @@ class Item
protected Tax $tax;
protected ItemType $type;

/** @var CustomField[] */
protected array $customFields;

public function id(): int
{
return $this->id;
Expand Down Expand Up @@ -114,6 +118,25 @@ public function type(): ItemType
return $this->type;
}

/**
* @throws RelationNotLoadedException
*/
public function customFields(): array
{
if (!isset($this->customFields)) {
throw new RelationNotLoadedException('customFields');
}

return $this->customFields;
}

public function setCustomFields(array $customFields): self
{
$this->customFields = $customFields;

return $this;
}

public function isset(string $field): bool
{
if (!method_exists($this, $field)) {
Expand Down
50 changes: 40 additions & 10 deletions src/Entity/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,47 @@

class Order
{
protected int $id;

protected bool $allowEmptyRelations;

protected float $amount;
protected float $amountWithTax;

protected OrderBilling $billing;

/** @var Comment[] */
protected array $comments;

protected DateTimeImmutable $createdAt;
protected ?DateTimeImmutable $deletedAt;
protected DateTimeImmutable | null $deletedAt;

/** @var Discount[] */
protected array $totalDiscounts;

protected bool $first;
protected bool $hidden;
protected int $id;
protected ?string $invoiceNumber;

protected string | null $invoiceNumber;

protected InvoiceStatus $invoiceStatus;

/** @var Item[] */
protected array $items;

protected Mode $mode;

protected Payment $payment;
protected string $reference;

protected Source $source;

/** @var string[] */
protected array $tags;

/** @var Tax[] */
protected array $taxes;

protected DateTimeImmutable $updatedAt;

public function __construct(bool $allowEmptyRelations = true)
Expand Down Expand Up @@ -121,7 +137,7 @@ public function createdAt(): DateTimeImmutable
return $this->createdAt;
}

public function deletedAt(): ?DateTimeImmutable
public function deletedAt(): DateTimeImmutable | null
{
return $this->deletedAt;
}
Expand All @@ -138,14 +154,11 @@ public function totalDiscounts(): array
return $this->totalDiscounts;
}

public function invoiceNumber(): ?string
public function setTotalDiscounts(array $totalDiscounts): self
{
return $this->invoiceNumber;
}
$this->totalDiscounts = $totalDiscounts;

public function invoiceStatus(): InvoiceStatus
{
return $this->invoiceStatus;
return $this;
}

public function isFirst(): bool
Expand All @@ -165,6 +178,16 @@ public function setHidden(bool $hidden): self
return $this;
}

public function invoiceNumber(): string | null
{
return $this->invoiceNumber;
}

public function invoiceStatus(): InvoiceStatus
{
return $this->invoiceStatus;
}

/**
* @throws RelationNotLoadedException
*/
Expand Down Expand Up @@ -260,6 +283,13 @@ public function taxes(): array
return $this->taxes;
}

public function setTaxes(array $taxes): self
{
$this->taxes = $taxes;

return $this;
}

public function updatedAt(): DateTimeImmutable
{
return $this->updatedAt;
Expand Down
24 changes: 2 additions & 22 deletions src/Entity/OrderInternal.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function setCreatedAt(DateTimeImmutable $createdAt): self
/**
* @internal
*/
public function setDeletedAt(?DateTimeImmutable $deletedAt): self
public function setDeletedAt(DateTimeImmutable | null $deletedAt): self
{
$this->deletedAt = $deletedAt;

Expand Down Expand Up @@ -56,7 +56,7 @@ public function setId(int $id): self
/**
* @internal
*/
public function setInvoiceNumber(?string $invoiceNumber): self
public function setInvoiceNumber(string | null $invoiceNumber): self
{
$this->invoiceNumber = $invoiceNumber;

Expand Down Expand Up @@ -93,26 +93,6 @@ public function setSource(Source $source): self
return $this;
}

/**
* @internal
*/
public function setTaxes(array $taxes): self
{
$this->taxes = $taxes;

return $this;
}

/**
* @internal
*/
public function setTotalDiscounts(array $totalDiscounts): self
{
$this->totalDiscounts = $totalDiscounts;

return $this;
}

/**
* @internal
*/
Expand Down
15 changes: 8 additions & 7 deletions src/Enum/OrderIncludes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

enum OrderIncludes: string
{
case BILLING = 'billing';
case COMMENTS = 'comments';
case DISCOUNTS = 'discounts';
case ITEMS = 'items';
case PAYMENT = 'payment';
case TAGS = 'tags';
case TAXES = 'taxes';
case BILLING = 'billing';
case COMMENTS = 'comments';
case DISCOUNTS = 'discounts';
case ITEMS = 'items';
case PAYMENT = 'payment';
case TAGS = 'tags';
case TAXES = 'taxes';
case CUSTOM_FIELDS = 'custom_fields';
flavio-schoute marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 2 additions & 2 deletions src/Service/OrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace PlugAndPay\Sdk\Service;

use PlugAndPay\Sdk\Contract\ClientInterface;
use PlugAndPay\Sdk\Contract\ClientPatchInterface;
use PlugAndPay\Sdk\Director\BodyTo\BodyToOrder;
use PlugAndPay\Sdk\Director\ToBody\OrderToBody;
use PlugAndPay\Sdk\Entity\Order;
Expand All @@ -17,7 +16,8 @@

class OrderService
{
private ClientPatchInterface $client;
private ClientInterface $client;

/** @var string[] */
private array $includes = [];

Expand Down