Skip to content

Commit

Permalink
Added new method CNet::queryTransactionTree (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxvx authored Jun 6, 2021
1 parent a2eeeb8 commit 0384d1b
Show file tree
Hide file tree
Showing 9 changed files with 562 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

1.15.0
-----

* Added new method `Net::queryTransactionTree`
* Added new integration and unit tests

1.14.0
-----

Expand Down
174 changes: 174 additions & 0 deletions src/Entity/AbstractData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

declare(strict_types=1);

namespace Extraton\TonClient\Entity;

use Extraton\TonClient\Exception\DataException;

class AbstractData
{
/** @var array<mixed> */
private array $data;

/**
* @param array<mixed> $data
*/
public function __construct(array $data)
{
$this->data = $data;
}

/**
* @param string ...$keys
* @return array|mixed|null
*/
protected function getOriginData(string ...$keys)
{
$result = $this->data;
while ($key = array_shift($keys)) {
if (!is_array($result) || !isset($result[$key])) {
return null;
}

$result = $result[$key];
}

return $result;
}

/**
* @param string ...$keys
* @return array<mixed>|null
*/
protected function getArray(string ...$keys): ?array
{
$result = $this->getOriginData(...$keys);

if (!is_array($result)) {
return null;
}

return $result;
}

/**
* @param string ...$keys
* @return string|null
*/
protected function getString(string ...$keys): ?string
{
$result = $this->getOriginData(...$keys);

if (!is_string($result)) {
return null;
}

return $result;
}

/**
* @param string ...$keys
* @return int|null
*/
protected function getInt(string ...$keys): ?int
{
$result = $this->getOriginData(...$keys);

if (!is_int($result)) {
return null;
}

return $result;
}

/**
* @param string ...$keys
* @return mixed
* @throws DataException
*/
protected function requireData(string ...$keys)
{
$result = $this->getOriginData(...$keys);

if ($result === null) {
$path = implode('.', $keys);

throw new DataException(sprintf('Data not found by key %s.', $path));
}

return $result;
}

/**
* @param string ...$keys
* @return array<mixed>
* @throws DataException
*/
protected function requireArray(string ...$keys): array
{
$result = $this->requireData(...$keys);

if (!is_array($result)) {
$path = implode('.', $keys);

throw new DataException(sprintf('Data is corrupted by key %s.', $path));
}

return $result;
}

/**
* @param string ...$keys
* @return string
* @throws DataException
*/
protected function requireString(string ...$keys): string
{
$result = $this->requireData(...$keys);

if (!is_string($result)) {
$path = implode('.', $keys);

throw new DataException(sprintf('Data is corrupted by key %s.', $path));
}

return $result;
}

/**
* @param string ...$keys
* @return int
* @throws DataException
*/
protected function requireInt(string ...$keys): int
{
$result = $this->requireData(...$keys);

if (!is_int($result)) {
$path = implode('.', $keys);

throw new DataException(sprintf('Data is corrupted by key %s.', $path));
}

return $result;
}

/**
* @param string ...$keys
* @return bool
* @throws DataException
*/
protected function requireBool(string ...$keys): bool
{
$result = $this->requireData(...$keys);

if (!is_bool($result)) {
$path = implode('.', $keys);

throw new DataException(sprintf('Data is corrupted by key %s.', $path));
}

return $result;
}
}
13 changes: 2 additions & 11 deletions src/Entity/Net/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,11 @@ public function getCollection(): string
*/
public function getResult(): string
{
$fields = array_merge(
...array_map(
static fn ($resultField): array => explode(' ', $resultField),
$this->resultFields
)
);

$fields = array_unique(array_filter(array_map('trim', $fields)));

if (empty($fields)) {
if (empty($this->resultFields)) {
throw new LogicException('Result fields cannot be empty');
}

return implode(' ', $fields);
return implode(' ', $this->resultFields);
}

/**
Expand Down
116 changes: 116 additions & 0 deletions src/Entity/Net/MessageNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

declare(strict_types=1);

namespace Extraton\TonClient\Entity\Net;

use Extraton\TonClient\Entity\Abi\DecodedMessageBody;
use Extraton\TonClient\Entity\AbstractData;
use Extraton\TonClient\Handler\Response;

/**
* Type MessageNode
*/
class MessageNode extends AbstractData
{
/**
* Create collection of MessageNode
*
* @param array<array<mixed>> $list
* @return array<MessageNode>
*/
public static function createCollection(array $list): array
{
return array_map(
fn ($data): self => new self($data),
$list
);
}

/**
* Get message id
*
* @return string
*/
public function getId(): string
{
return $this->requireString('id');
}

/**
* Source transaction id. This field is missing for an external inbound messages.
*
* @return string|null
*/
public function getSrcTransactionId(): ?string
{
return $this->getString('src_transaction_id');
}

/**
* Destination transaction id. This field is missing for an external outbound messages.
*
* @return string|null
*/
public function getDstTransactionId(): ?string
{
return $this->getString('dst_transaction_id');
}

/**
* Get source address
*
* @return string|null
*/
public function getSrcAddress(): ?string
{
return $this->getString('src');
}

/**
* Destination address
*
* @return string|null
*/
public function getDstAddress(): ?string
{
return $this->getString('dst');
}

/**
* Get transferred tokens value
*
* @return string|null
*/
public function getValue(): ?string
{
return $this->getString('value');
}

/**
* Get bounce flag
*
* @return bool|null
*/
public function getBounce(): ?bool
{
return $this->requireBool('bounce');
}

/**
* Get decoded body.
* Library tries to decode message body using provided params.abi_registry.
* This field will be missing if none of the provided abi can be used to decode.
*
* @return DecodedMessageBody|null
*/
public function getDecodedBody(): ?DecodedMessageBody
{
$data = $this->getArray('decoded_body');
if ($data === null) {
return null;
}

return new DecodedMessageBody(new Response($data));
}
}
33 changes: 33 additions & 0 deletions src/Entity/Net/ResultOfQueryTransactionTree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Extraton\TonClient\Entity\Net;

use Extraton\TonClient\Entity\AbstractResult;

/**
* Type ResultOfQueryTransactionTree
*/
class ResultOfQueryTransactionTree extends AbstractResult
{
/**
* Get Messages
*
* @return array<MessageNode>
*/
public function getMessages(): array
{
return MessageNode::createCollection($this->requireArray('messages'));
}

/**
* Get transactions
*
* @return array<TransactionNode>
*/
public function getTransactions(): array
{
return TransactionNode::createCollection($this->requireArray('transactions'));
}
}
Loading

0 comments on commit 0384d1b

Please sign in to comment.