diff --git a/src/Concerns/Arrayable.php b/src/Concerns/Arrayable.php new file mode 100644 index 0000000..b4aef68 --- /dev/null +++ b/src/Concerns/Arrayable.php @@ -0,0 +1,21 @@ +getProperties(ReflectionProperty::IS_PUBLIC) as $property) { + $array[$property->getName()] = $property->isInitialized($this) ? $property->getValue($this) : null; + } + + return $array; + } +} diff --git a/src/Contracts/Arrayable.php b/src/Contracts/Arrayable.php new file mode 100644 index 0000000..602e67f --- /dev/null +++ b/src/Contracts/Arrayable.php @@ -0,0 +1,17 @@ + + */ + public function toArray(): array; +} diff --git a/src/InvoiceHeader.php b/src/InvoiceHeader.php index a99ee07..b013786 100644 --- a/src/InvoiceHeader.php +++ b/src/InvoiceHeader.php @@ -3,10 +3,13 @@ namespace Novaday\Moadian; use DateTime; +use Novaday\Moadian\Contracts\Arrayable; use Novaday\Moadian\Services\VerhoeffService; -class InvoiceHeader +class InvoiceHeader implements Arrayable { + use Concerns\Arrayable; + protected const CHARACTER_TO_NUMBER_CODING = [ 'A' => 65, 'B' => 66, 'C' => 67, 'D' => 68, 'E' => 69, 'F' => 70, 'G' => 71, 'H' => 72, 'I' => 73, 'J' => 74, 'K' => 75, 'L' => 76, 'M' => 77, 'N' => 78, 'O' => 79, 'P' => 80, 'Q' => 81, 'R' => 82, @@ -16,7 +19,17 @@ class InvoiceHeader /** * MOADIAN_USERNAME */ - public string $clientId; + protected string $clientId; + + public function getClientId(): string + { + return $this->clientId; + } + + public function setClientId(string $clientId): void + { + $this->clientId = $clientId; + } /** * unique tax ID (should be set by setTaxID ) @@ -118,16 +131,6 @@ class InvoiceHeader */ public ?string $crn; - /** - * customs declaration cottage number - */ - public ?string $cdcn; - - /** - * customs declaration cottage date - */ - public ?int $cdcd; - /** * billing ID */ @@ -163,21 +166,6 @@ class InvoiceHeader */ public float $tbill; - /** - * total net weight - */ - public ?float $tonw; - - /** - * total Rial value - */ - public ?float $torv; - - /** - * total currency value - */ - public ?float $tocv; - /** * settlement type */ @@ -203,17 +191,59 @@ class InvoiceHeader */ public ?float $tax17; - public function __construct(string $username = null) - { - $this->clientId = $username; - } + /** + * customs declaration cottage number + */ + public ?string $cdcn; - public function toArray(): array - { - $arr = get_object_vars($this); - unset($arr['clientId']); - return $arr; - } + /** + * customs declaration cottage date + */ + public ?int $cdcd; + + /** + * total net weight + */ + public ?float $tonw; + + /** + * total Rial value + */ + public ?float $torv; + + /** + * total currency value + */ + public ?float $tocv; + + public ?string $tinc; + + public ?string $lno; + + public ?string $lrno; + + public ?string $ocu; + + public ?string $oci; + + public ?string $dco; + + public ?string $dci; + + public ?string $tid; + + public ?string $rid; + + public ?int $lt; + + public ?string $cno; + + public ?string $did; + + public ?array $sg; + + public ?string $asn; + public ?int $asd; public function setTaxID(DateTime $date, int $internalInvoiceId) { diff --git a/src/InvoiceItem.php b/src/InvoiceItem.php index d56c6ca..6685f27 100644 --- a/src/InvoiceItem.php +++ b/src/InvoiceItem.php @@ -2,8 +2,12 @@ namespace Novaday\Moadian; -class InvoiceItem +use Novaday\Moadian\Contracts\Arrayable; + +class InvoiceItem implements Arrayable { + use Concerns\Arrayable; + /** * service stuff ID */ @@ -154,8 +158,7 @@ class InvoiceItem */ public float $tsstam; - public function toArray(): array - { - return get_object_vars($this); - } + public ?float $cui; + public ?float $cpr; + public ?int $sovat; } diff --git a/src/Services/SignatureService.php b/src/Services/SignatureService.php index 3f68ad9..35f8261 100644 --- a/src/Services/SignatureService.php +++ b/src/Services/SignatureService.php @@ -32,20 +32,19 @@ public static function normalizer(array $data, array $headers): string $normalizedData = []; - $flatted = self::flattener($data); + $flatted = self::dot($data); ksort($flatted); foreach ($flatted as $value) { - - if (is_bool($value)) { - $value = $value ? 'true' : 'false'; - } - - if ($value === '' || $value === null) { + if (blank($value)) { $value = '#'; } else { - $value = strtr($value, [ '#' => '##']); + if (blank($value = strval($value))) { + $value = '#'; + } else { + $value = str_replace('#', '##', $value); + } } $normalizedData[] = $value; @@ -54,16 +53,18 @@ public static function normalizer(array $data, array $headers): string return implode("#", $normalizedData); } - private static function flattener(array $array, string $prefix = ''): array + private static function dot(array $array, string $prepend = ''): array { - $flatted = []; + $results = []; + foreach ($array as $key => $value) { - if (is_array($value)) { - $flatted = array_merge($flatted, self::flattener($value, "$prefix.$key")); + if (is_array($value) && !empty($value)) { + $results = array_merge($results, self::dot($value, $prepend . $key . '.')); } else { - $flatted["$prefix.$key"] = $value; + $results[$prepend . $key] = $value; } } - return $flatted; + + return $results; } }