Skip to content

Commit

Permalink
project copy
Browse files Browse the repository at this point in the history
  • Loading branch information
t3ran13 committed Jan 28, 2018
1 parent b89ac18 commit f7319e9
Show file tree
Hide file tree
Showing 57 changed files with 3,202 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Commands/Broadcast/BroadcastTransactionCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php


namespace GolosPHP\Commands\Broadcast;

/**
* Class BroadcastTransactionCommand
*
* This call will return before the transaction is included in a block.
*
* @package GolosPHP\Commands\Broadcast
*/
class BroadcastTransactionCommand extends CommandAbstract
{
protected $method = 'broadcast_transaction';
protected $queryDataMap = [
'0:ref_block_num' => ['integer'],
'0:ref_block_prefix' => ['integer'],
'0:expiration' => ['string'],
'0:operations:*:0' => ['string'],
'0:operations:*:1' => ['array'],
'0:extensions' => ['array'],
'0:signatures' => ['array']
];
}
25 changes: 25 additions & 0 deletions Commands/Broadcast/BroadcastTransactionSynchronousCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php


namespace GolosPHP\Commands\Broadcast;

/**
* Class BroadcastTransactionSynchronousCommand
*
* This call will not return until the transaction is included in a block.
*
* @package GolosPHP\Commands\Broadcast
*/
class BroadcastTransactionSynchronousCommand extends CommandAbstract
{
protected $method = 'broadcast_transaction_synchronous';
protected $queryDataMap = [
'0:ref_block_num' => ['integer'],
'0:ref_block_prefix' => ['integer'],
'0:expiration' => ['string'],
'0:operations:*:0' => ['string'],
'0:operations:*:1' => ['array'],
'0:extensions' => ['array'],
'0:signatures' => ['array']
];
}
108 changes: 108 additions & 0 deletions Commands/Broadcast/CommandAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php


namespace GolosPHP\Commands\Broadcast;


use GolosPHP\Commands\CommandQueryData;
use GolosPHP\Commands\CommandQueryDataInterface;
use GolosPHP\Connectors\ConnectorInterface;

abstract class CommandAbstract implements CommandInterface
{
/** @var string */
protected $method = '';
/** @var array */
protected $queryDataMap = [];
/** @var ConnectorInterface */
protected $connector;
/** @var string */
protected $apiName = 'network_broadcast_api';


public function __construct(ConnectorInterface $connector)
{
$this->connector = $connector;
}

/**
* @param CommandQueryDataInterface $commandQueryData
* @param string $answerFormat
* @param string $getElementWithKey If you want to get only certain element from answer.
* Example: 'key:123:qwe' => $array['key']['123']['qwe'] or $object->key->123->qwe
* @return array|object
*/
public function execute(CommandQueryDataInterface $commandQueryData, $getElementWithKey = null, $answerFormat = ConnectorInterface::ANSWER_FORMAT_ARRAY)
{
/** @var CommandQueryData $commandQueryData */
$params = $commandQueryData->prepareData($this->getQueryDataMap());

$answer = $this->doRequest($params, $answerFormat);

$defaultValue = $answerFormat === ConnectorInterface::ANSWER_FORMAT_ARRAY ? [] : ((object)[]);

return $this->getElementByKey($answer, $getElementWithKey, $defaultValue);
}


/**
* @return array|mixed
*/
public function getQueryDataMap()
{
return isset($this->queryDataMap[$this->connector->getPlatform()])
? $this->queryDataMap[$this->connector->getPlatform()]
: $this->queryDataMap;
}

/**
* @param array $params
* @param string $answerFormat
* @return array|object
*/
protected function doRequest($params, $answerFormat = ConnectorInterface::ANSWER_FORMAT_ARRAY)
{
$data = [
'method' => $this->method,
'params' => $params
];

return $this->connector->doRequest($this->apiName, $data, $answerFormat);
}


/**
* get all values or vulue by key
*
* Example: 'key:123:qwe' => $array['key']['123']['qwe'] or $object->key->123->qwe
*
* @param null|string $getKey
* @param null|mixed $default
* @param array|object $array
*
* @return mixed
*/
protected function getElementByKey($array, $getKey = null, $default = null)
{
$data = $array;
if ($getKey) {
$keyParts = explode(':', $getKey);
foreach ($keyParts as $key) {
if (is_array($data) && isset($data[$key])) {
$data = $data[$key];
} elseif (is_object($data) && isset($data->$key)) {
$data = $data->$key;
} else {
$data = null;
break;
}
}
}

if ($data === null) {
$data = $default;
}

return $data;
}
}
20 changes: 20 additions & 0 deletions Commands/Broadcast/CommandInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php


namespace GolosPHP\Commands\Broadcast;

use GolosPHP\Commands\CommandQueryDataInterface;

interface CommandInterface
{
/**
* @param CommandQueryDataInterface $commandQueryData
* @return mixed
*/
public function execute(CommandQueryDataInterface $commandQueryData);

/**
* @return array
*/
public function getQueryDataMap();
}
194 changes: 194 additions & 0 deletions Commands/CommandQueryData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<?php


namespace GolosPHP\Commands;


class CommandQueryData implements CommandQueryDataInterface
{
/** @var array */
protected $params = [];

/**
* @return array
*/
public function getParams()
{
return $this->params;
}

/**
* @param $params
*/
public function setParams($params)
{
$this->params = $params;
}

/**
* set value in params by key
*
* $setKey example: 'key:1:one_more_key' => $params['key'][1]['one_more_key']
* $setKey example: 'key:a1' => $params['key']['a1']
*
* @param string $setKey
* @param mixed $setVal
*/
public function setParamByKey($setKey, $setVal)
{
$this->params = $this->setArrayElementByKey($this->params, $setKey, $setVal);
}

/**
* @param array $map
* @return array
*/
public function prepareData($map)
{
$queryData = [];
foreach ($map as $route => $rules) {
$queryData = $this->prepareQueryDataForRoute($queryData, $route, $rules);
}

return $queryData;
}


/**
* @param array $data
* @param string $fullRoute
* @param array $rules
*
* @return mixed
*
* @throws CommandQueryDataException
*/
protected function prepareQueryDataForRoute($data, $fullRoute, $rules = [])
{
$errors = [];
$routeParts = explode(':', $fullRoute);
$values = $this->getParamsListByKey($this->params, $routeParts);

foreach ($values as $route => $value) {
foreach ($rules as $rule) {
if (!$this->validate($value, $rule)) {
$errors[] = 'Validation rule \'' . $rule . '\' was failed for route \'' . $route . '\' with value ' . $value . ';';
}
}
if ($value !== null) {
$data = $this->setArrayElementByKey($data, $route, $value);
}
}

if (!empty($errors)) {
throw new CommandQueryDataException(implode(PHP_EOL, $errors));
}

return $data;
}


/**
* @param mixed $value
* @param string $rule
* @return bool
*/
protected function validate($value, $rule)
{
if ($rule === 'required') {
return $value === null ? false : true;
} elseif ($rule === 'array') {
return $value !== null && is_array($value);
} elseif ($rule === 'string') {
return $value !== null && is_string($value);
} elseif ($rule === 'integer') {
return $value !== null && is_int($value);
} elseif ($rule === 'nullOrArray') {
return $value === null || is_array($value);
} elseif ($rule === 'nullOrString') {
return $value === null || is_string($value);
} elseif ($rule === 'nullOrInteger') {
return $value === null || is_int($value);
}
}


/**
* @param array $params
* @param array $routeParts
* @return array
*/
protected function getParamsListByKey($params, $routeParts = [])
{
$values = [];
if (empty($routeParts)) {
$values = $params;
} else {
$currentKeyPart = array_shift($routeParts);
if (
is_numeric($currentKeyPart)
&& (string)((integer)$currentKeyPart) === $currentKeyPart
) {
$currentKeyPart = (integer)$currentKeyPart;
}
if (isset($params[$currentKeyPart])) {
$tmp = $this->getParamsListByKey($params[$currentKeyPart], $routeParts);
if (is_array($tmp) && !empty($routeParts)) {
foreach ($tmp as $valueKey => $value) {
$values[$currentKeyPart . ':' . $valueKey] = $value;
}
} else {
$values[$currentKeyPart] = $tmp;
}
} elseif (is_array($params) && $currentKeyPart === '*') {
foreach ($params as $paramKey => $param) {
$tmp = $this->getParamsListByKey($param, $routeParts);
if (is_array($tmp)) {
foreach ($this->getParamsListByKey($param, $routeParts) as $valueKey => $value) {
$values[$paramKey . ':' . $valueKey] = $value;
}
} else {
$values[$paramKey] = $tmp;
}
}
} else {
$values[implode(':', array_merge([$currentKeyPart], $routeParts))] = null;
}
}

return $values;
}


/**
* set value in array by key
*
* $setKey example: 'key:123:array' => $_SESSION['key'][123]['array']
*
* @param array $array
* @param string $setKey
* @param mixed $setVal
*
* @return array
*/
protected function setArrayElementByKey($array, $setKey, $setVal)
{
$link = &$array;
$keyParts = explode(':', $setKey);
foreach ($keyParts as $key) {
if (
is_numeric($key)
&& (string)((integer)$key) === $key
) {
$key = (integer)$key;
}
if (!isset($link[$key])) {
$link[$key] = [];
}
$link = &$link[$key];
}
$link = $setVal;

return $array;
}
}
11 changes: 11 additions & 0 deletions Commands/CommandQueryDataException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php


namespace GolosPHP\Commands;


use \Exception;

class CommandQueryDataException extends Exception
{
}
Loading

0 comments on commit f7319e9

Please sign in to comment.