Skip to content
Open
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
21 changes: 21 additions & 0 deletions src/Concerns/Arrayable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Novaday\Moadian\Concerns;

use ReflectionClass;
use ReflectionProperty;

trait Arrayable
{
public function toArray(): array
{
$array = [];
$reflection = new ReflectionClass($this);

foreach ($reflection->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
$array[$property->getName()] = $property->isInitialized($this) ? $property->getValue($this) : null;
}

return $array;
}
}
17 changes: 17 additions & 0 deletions src/Contracts/Arrayable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Novaday\Moadian\Contracts;

/**
* @template TKey of array-key
* @template TValue
*/
interface Arrayable
{
/**
* Get the instance as an array.
*
* @return array<TKey, TValue>
*/
public function toArray(): array;
}
104 changes: 67 additions & 37 deletions src/InvoiceHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 )
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand All @@ -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)
{
Expand Down
13 changes: 8 additions & 5 deletions src/InvoiceItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace Novaday\Moadian;

class InvoiceItem
use Novaday\Moadian\Contracts\Arrayable;

class InvoiceItem implements Arrayable
{
use Concerns\Arrayable;

/**
* service stuff ID
*/
Expand Down Expand Up @@ -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;
}
29 changes: 15 additions & 14 deletions src/Services/SignatureService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}