Skip to content

Commit

Permalink
feat: cria funçoes para converter o objeto
Browse files Browse the repository at this point in the history
A conversao ocorrera de objeto para XML e
XML para objeto.
  • Loading branch information
valdeir2000 committed Aug 22, 2020
1 parent 3addbb5 commit 967d718
Show file tree
Hide file tree
Showing 28 changed files with 1,825 additions and 44 deletions.
130 changes: 109 additions & 21 deletions upload/system/library/PagSeguro/src/Domains/Address.php
Expand Up @@ -3,27 +3,32 @@

namespace ValdeirPsr\PagSeguro\Domains;

use DOMDocument;
use ValdeirPsr\PagSeguro\Interfaces\Serializer\Xml;
use ValdeirPsr\PagSeguro\Interfaces\Serializer\IArray;
use ValdeirPsr\PagSeguro\Parser\Xml as XmlParser;

/**
* Classe responsável pelo endereço de envio e entrega
*/
class Address
class Address implements Xml, IArray
{
private $street;
private $number;
private $complement;
private $district;
private $city;
private $state;
private $country = 'BRA';
private $postalcode;
private $complement;
private $postalCode;

/**
* @param string $street
* @param string $number
* @param string $district
* @param string $city
* @param string $state
* @param string $postalcode
* @param string $postalCode
* @param string|null $complement
*/
public function __construct(
Expand All @@ -32,7 +37,7 @@ public function __construct(
string $district = null,
string $city = null,
string $state = null,
string $postalcode = null,
string $postalCode = null,
string $complement = null
)
{
Expand All @@ -41,7 +46,7 @@ public function __construct(
if ($district) $this->setDistrict($district);
if ($city) $this->setCity($city);
if ($state) $this->setState($state);
if ($postalcode) $this->setPostalcode($postalcode);
if ($postalCode) $this->setPostalCode($postalCode);
if ($complement) $this->setComplement($complement);
}

Expand Down Expand Up @@ -91,6 +96,27 @@ public function getNumber(): string
return $this->number;
}

/**
* Define o complemento do endereço
*
* @param string|null $value
*/
public function setComplement(?string $value): self
{
$this->complement = $value;
return $this;
}

/**
* Retorna o complemento
*
* @return string|null
*/
public function getComplement(): ?string
{
return $this->complement;
}


/**
* Define o bairro
Expand Down Expand Up @@ -177,9 +203,9 @@ public function getCountry(): string
*
* @param stringi $value
*/
public function setPostalcode(string $value): self
public function setPostalCode(string $value): self
{
$this->postalcode = preg_replace('/\D/', '', $value);
$this->postalCode = preg_replace('/\D/', '', $value);
return $this;
}

Expand All @@ -188,29 +214,91 @@ public function setPostalcode(string $value): self
*
* @return string (Apenas o número)
*/
public function getPostalcode(): string
public function getPostalCode(): string
{
return $this->postalcode;
return $this->postalCode;
}

/**
* Define o complemento do endereço
*
* @param string|null $value
* {@inheritDoc}
*/
public function setComplement(?string $value): self
public static function fromXml(string $value)
{
$this->complement = $value;
return $this;
$dom = new DOMDocument();
$dom->loadXML($value);

$instance = new self();

$street = $dom->getElementsByTagName('street');

if ($street->count() > 0) {
$instance->street = $street->item(0)->textContent;
}

$number = $dom->getElementsByTagName('number');

if ($number->count() > 0) {
$instance->number = $number->item(0)->textContent;
}

$district = $dom->getElementsByTagName('district');

if ($district->count() > 0) {
$instance->district = $district->item(0)->textContent;
}

$city = $dom->getElementsByTagName('city');

if ($city->count() > 0) {
$instance->city = $city->item(0)->textContent;
}

$state = $dom->getElementsByTagName('state');

if ($state->count() > 0) {
$instance->state = $state->item(0)->textContent;
}

$country = $dom->getElementsByTagName('country');

if ($country->count() > 0) {
$instance->country = $country->item(0)->textContent;
}

$postalCode = $dom->getElementsByTagName('postalCode');

if ($postalCode->count() > 0) {
$instance->postalCode = $postalCode->item(0)->textContent;
}

$complement = $dom->getElementsByTagName('complement');

if ($complement->count() > 0) {
$instance->complement = $complement->item(0)->textContent;
}


return $instance;
}

/**
* Retorna o complemento
*
* @return string|null
* {@inheritDoc}
*/
public function getComplement(): ?string
public function toXml(): string
{
return $this->complement;
$parser = new XmlParser();
$result = $parser->parser([
'address' => $this->toArray()
]);

return $result->saveXML();
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return array_filter(get_object_vars($this));
}
}
70 changes: 67 additions & 3 deletions upload/system/library/PagSeguro/src/Domains/CartItem.php
Expand Up @@ -2,9 +2,13 @@

namespace ValdeirPsr\PagSeguro\Domains;

use DOMDocument;
use ValdeirPsr\PagSeguro\Interfaces\Serializer\Xml;
use ValdeirPsr\PagSeguro\Interfaces\Serializer\IArray;
use ValdeirPsr\PagSeguro\Parser\Xml as XmlParser;
use ValdeirPsr\PagSeguro\Validation\Validator as v;

class CartItem
class CartItem implements Xml, IArray
{
/** @var string Identificador do produto. Deve ser único */
private $id;
Expand Down Expand Up @@ -80,13 +84,15 @@ public function setAmount(float $value): self
throw new \InvalidArgumentException('Amount invalid. The value must have two decimal places. Was: ' . $value);
}

$this->amount = $value;
$this->amount = number_format($value, 2, '.', '');

return $this;
}

/**
* @return float Retorna o preço unitário do item
*/
public function getAmount(): float
public function getAmount(): string
{
return $this->amount;
}
Expand All @@ -113,4 +119,62 @@ public function getQuantity(): int
{
return $this->quantity;
}

/**
* {@inheritDoc}
*/
public static function fromXml(string $value)
{
$dom = new DOMDocument();
$dom->loadXML($value);

$instance = new self();

$id = $dom->getElementsByTagName('id');

if ($id->count() > 0) {
$instance->id = $id->item(0)->textContent;
}

$description = $dom->getElementsByTagName('description');

if ($description->count() > 0) {
$instance->description = $description->item(0)->textContent;
}

$quantity = $dom->getElementsByTagName('quantity');

if ($quantity->count() > 0) {
$instance->quantity = $quantity->item(0)->textContent;
}

$amount = $dom->getElementsByTagName('amount');

if ($amount->count() > 0) {
$instance->amount = $amount->item(0)->textContent;
}

return $instance;
}

/**
* {@inheritDoc}
*/
public function toXml(): string
{
$parser = new XmlParser();
$result = $parser->parser([
'item' => $this->toArray()
]);

return $result->saveXML();
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return array_filter(get_object_vars($this));
}
}
52 changes: 51 additions & 1 deletion upload/system/library/PagSeguro/src/Domains/Document.php
Expand Up @@ -2,9 +2,13 @@

namespace ValdeirPsr\PagSeguro\Domains;

use DOMDocument;
use ValdeirPsr\PagSeguro\Interfaces\Serializer\Xml;
use ValdeirPsr\PagSeguro\Interfaces\Serializer\IArray;
use ValdeirPsr\PagSeguro\Parser\Xml as XmlParser;
use \ValdeirPsr\PagSeguro\Validation\Validator as v;

class Document
class Document implements Xml, IArray
{
private $type;
private $value;
Expand Down Expand Up @@ -72,4 +76,50 @@ public function getValue(): string
{
return $this->value;
}

/**
* {@inheritDoc}
*/
public static function fromXml(string $value)
{
$dom = new DOMDocument();
$dom->loadXml($value);

$type = $dom->getElementsByTagName('type');
$value = $dom->getElementsByTagName('value');

if ($type->count() > 0) {
$type = strtolower(trim($type->item(0)->textContent));
$value = preg_replace('/\D/', '', (trim($value->item(0)->textContent)));

if ($type === 'cpf') {
return new self('cpf', $value);
} elseif ($type === 'cnpj') {
return new self('cnpj', $value);
}
}

return null;
}

/**
* {@inheritDoc}
*/
public function toXml(): string
{
$parser = new XmlParser();
$result = $parser->parser([
"document" => get_object_vars($this)
]);

return $result->saveXml();
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return array_filter(get_object_vars($this));
}
}

0 comments on commit 967d718

Please sign in to comment.