Skip to content

Commit

Permalink
Merge pull request #75 from jirik1h/master
Browse files Browse the repository at this point in the history
Add AddressesForServices to export package method
  • Loading branch information
Salamek committed Jan 18, 2023
2 parents ac9e470 + ae1f015 commit 3f433f8
Show file tree
Hide file tree
Showing 5 changed files with 427 additions and 18 deletions.
41 changes: 38 additions & 3 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,15 @@ public function createPackages(array $packages, $customerUniqueImportId = null)

$packagesExtNums = [];
foreach ($package->getExternalNumbers() AS $externalNumber) {
$packagesExtNums[]['MyApiPackageExtNum'] = [
$packagesExtNums['MyApiPackageExtNum'][] = [
'Code' => $externalNumber->getCode(),
'ExtNumber' => $externalNumber->getExternalNumber()
];
}

$packageServices = [];
foreach ($package->getPackageServices() AS $service) {
$packageServices[]['MyApiPackageInServices'] = [
$packageServices['MyApiPackageInServices'][] = [
'SvcCode' => $service->getSvcCode()
];
}
Expand Down Expand Up @@ -412,6 +412,35 @@ public function createPackages(array $packages, $customerUniqueImportId = null)
'SpecTakeTimeTo' => $specialDelivery->getTakeTimeTo() ? $specialDelivery->getTakeTimeTo()->format('H:i:s') : null
] : null;

$addressesForServices = [];
foreach ($package->getAddressesForServices() as $addressForService) {
$addressFlagsList = [];
foreach ($addressForService->getFlags() AS $flag) {
$addressFlagsList[] = [
'Code' => $flag->getCode(),
'Value' => $flag->isValue()
];
}

$addressFlags = empty($addressFlagsList) ? false : ['MyApiFlag' => $addressFlagsList];

$addressesForServices['AddressForService'][] = [
'ServiceAddressType' => $addressForService->getServiceAddressType(),
'Recipient' => [
'City' => $addressForService->getCity(),
'Contact' => $addressForService->getContact(),
'Country' => $addressForService->getCountry(),
'Email' => $addressForService->getEmail(),
'Name' => $addressForService->getName(),
'Name2' => $addressForService->getName2(),
'Phone' => $addressForService->getPhone(),
'Street' => $addressForService->getStreet(),
'ZipCode' => $addressForService->getZipCode()
],
'Flags' => $addressFlags,
];
}

$packagesProcessed[] = [
'PackNumber' => $package->getPackageNumber(),
'PackProductType' => $package->getPackageProductType(),
Expand Down Expand Up @@ -460,7 +489,8 @@ public function createPackages(array $packages, $customerUniqueImportId = null)
] : null),
'Flags' => $flags,
'PalletInfo' => $palletInfo,
'WeightedPackageInfo' => $weightedPackageInfo
'WeightedPackageInfo' => $weightedPackageInfo,
'AddressesForServices' => $addressesForServices,
];

if ($package->getDepoCode() !== null) {
Expand Down Expand Up @@ -637,4 +667,9 @@ public function getLastResponseHeaders() {
return $this->soap->__getLastResponseHeaders();
}

public function setTokenLifespan(string $tokenLifespan): Api
{
$this->tokenLifespan = $tokenLifespan;
return $this;
}
}
19 changes: 19 additions & 0 deletions src/Enum/AddressForService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Salamek\PplMyApi\Enum;

class AddressForService
{
public const RETURN_ADDRESS_NORMAL_PACKAGE = 'BP';
public const RETURN_ADDRESS_RETURN_PACKAGE = 'RETD';
public const RETURN_ADDRESS_CONNECT_PACKAGE = 'RETC';

/** @var array */
public static $list = [
self::RETURN_ADDRESS_NORMAL_PACKAGE,
self::RETURN_ADDRESS_RETURN_PACKAGE,
self::RETURN_ADDRESS_CONNECT_PACKAGE,
];
}
247 changes: 247 additions & 0 deletions src/Model/AddressForService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
<?php

namespace Salamek\PplMyApi\Model;

use Salamek\PplMyApi\Enum\Country;
use Salamek\PplMyApi\Exception\WrongDataException;
use Salamek\PplMyApi\Validators\MaxLengthValidator;
use Salamek\PplMyApi\Enum\AddressForService as AddressForServiceEnum;

class AddressForService implements IAddressForService
{
/** @var string */
private $city;

/** @var null|string */
private $contact = null;

/** @var null|string */
private $country = null;

/** @var null|string */
private $email = null;

/** @var string */
private $name;

/** @var null|string */
private $name2 = null;

/** @var null|string */
private $phone = null;

/** @var string */
private $street;

/** @var string */
private $zipCode;

/** @var IFlag[] */
private $flags;

/** @var string */
private $serviceAddressType;

/**
* @param iFlag[] $flags
* @throws WrongDataException
*/
public function __construct(
string $serviceAddressType,
string $city,
string $name,
string $street,
string $zipCode,
string $email = null,
string $phone = null,
string $contact = null,
string $country = Country::CZ,
string $name2 = null,
array $flags = []
) {
$this->setServiceAddressType($serviceAddressType);
$this->setCity($city);
$this->setName($name);
$this->setStreet($street);
$this->setZipCode($zipCode);
$this->setEmail($email);
$this->setPhone($phone);
$this->setContact($contact);
$this->setCountry($country);
$this->setName2($name2);
$this->setFlags($flags);
}

/**
* @inheritdoc
*/
public function setServiceAddressType(string $serviceAddressType)
{
if (!in_array($serviceAddressType, AddressForServiceEnum::$list)) {
throw new WrongDataException(
sprintf('$packageProductType has wrong value, only %s are allowed', implode(', ', AddressForServiceEnum::$list))
);
}
$this->serviceAddressType = $serviceAddressType;
}

public function setCity(string $city)
{
MaxLengthValidator::validate($city, 50);
$this->city = $city;
}

/**
* @inheritdoc
*/
public function setContact(string $contact)
{
if (!is_null($contact)) {
MaxLengthValidator::validate($contact, 30);
}

$this->contact = $contact;
}

/**
* @inheritdoc
*/
public function setCountry(string $country)
{
if (!in_array($country, Country::$list)) {
throw new WrongDataException(sprintf('Country Code %s is not supported, use one of %s', $country, implode(', ', Country::$list)));
}
$this->country = $country;
}

/**
* @inheritdoc
*/
public function setEmail(string $email)
{
if (!is_null($email)) {
MaxLengthValidator::validate($email, 100);

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new WrongDataException('$email have invalid value');
}
}


$this->email = $email;
}

/**
* @inheritdoc
*/
public function setName(string $name)
{
MaxLengthValidator::validate($name, 250);
$this->name = $name;
}

/**
* @inheritdoc
*/
public function setName2(string $name2)
{
if (!is_null($name2)) {
MaxLengthValidator::validate($name2, 250);
}

$this->name2 = $name2;
}

/**
* @inheritdoc
*/
public function setPhone(string $phone)
{
if (!is_null($phone)) {
MaxLengthValidator::validate($phone, 30);
}

$this->phone = $phone;
}

/**
* @inheritdoc
*/
public function setStreet(string $street)
{
MaxLengthValidator::validate($street, 30);
$this->street = $street;
}

/**
* @inheritdoc
*/
public function setZipCode(string $zipCode)
{
MaxLengthValidator::validate($zipCode, 10);
$this->zipCode = $zipCode;
}

/**
* @param IFlag[] $flags
*/
public function setFlags(array $flags)
{
$this->flags = $flags;
}

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

public function getContact(): ?string
{
return $this->contact;
}

public function getCountry(): ?string
{
return $this->country;
}

public function getEmail(): ?string
{
return $this->email;
}

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

public function getName2(): ?string
{
return $this->name2;
}

public function getPhone(): ?string
{
return $this->phone;
}

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

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

public function getFlags(): array
{
return $this->flags;
}

public function getServiceAddressType(): string
{
return $this->serviceAddressType;
}
}
Loading

0 comments on commit 3f433f8

Please sign in to comment.