1
0
mirror of synced 2024-11-22 03:46:02 +03:00

Several important fixes

This commit is contained in:
Pavel 2019-06-20 18:06:21 +03:00
parent ec3bd1b293
commit 02a45d8272
9 changed files with 54 additions and 28 deletions

View File

@ -94,7 +94,17 @@ class Client
if ($arrayOfObjects) {
return new ListResponse($responseType, $data);
} else {
return new $responseType($data);
$obj = Serializer::deserialize($data, $responseType, Serializer::S_ARRAY);
if ($response->getStatusCode() >= 400
&& method_exists($obj, 'setErrors')
&& method_exists($obj, 'getErrors')
&& count(call_user_func([$obj, 'getErrors'])) == 0
) {
call_user_func_array([$obj, 'setErrors'], [['Status Code ' . $response->getStatusCode()]]);
}
return $obj;
}
} else {
throw new InvalidJsonException('Received invalid JSON', 1);
@ -251,7 +261,7 @@ class Client
HttpClient::METHOD_PUT,
$request,
static::getResponseClass(self::ERROR_ONLY_RESPONSE),
true
Serializer::S_JSON
);
}

View File

@ -203,7 +203,7 @@ class HttpClient
*/
private function validateRequest($class)
{
if (method_exists($class, 'validate')) {
if (!is_string($class) && method_exists($class, 'validate')) {
$errors = $class->validate();
} else {
$validator = Validation::createValidatorBuilder()
@ -213,7 +213,7 @@ class HttpClient
$errors = $validator->validate($class);
}
if ((is_object($errors) && $errors->count() > 0) || is_string($errors)) {
if ((is_object($errors) && call_user_func([$errors, 'count']) > 0) || is_string($errors)) {
$message = (string) $errors;
throw new InvalidArgumentException($message);
}

View File

@ -73,17 +73,17 @@ class ChatLastMessage
}
/**
* @return string
* @return \DateTime
*/
public function getTime(): string
public function getTime(): \DateTime
{
return $this->time;
}
/**
* @param string $time
* @param \DateTime $time
*/
public function setTime(string $time)
public function setTime(\DateTime $time)
{
$this->time = $time;
}

View File

@ -16,6 +16,8 @@ namespace RetailCrm\Mg\Bot\Model\Request;
use JMS\Serializer\Annotation\Accessor;
use JMS\Serializer\Annotation\SkipWhenEmpty;
use JMS\Serializer\Annotation\Type;
use RetailCrm\Mg\Bot\Model\Entity\Message\MessageOrder;
use RetailCrm\Mg\Bot\Model\Entity\Message\MessageProduct;
use Symfony\Component\Validator\Constraints as Assert;
use RetailCrm\Mg\Bot\Model\Entity\Order;
use RetailCrm\Mg\Bot\Model\Entity\Product;
@ -52,7 +54,7 @@ class MessageSendRequest
private $content;
/**
* @var Product $product
* @var MessageProduct $product
*
* @Type("Product")
* @Accessor(getter="getProduct",setter="setProduct")
@ -61,7 +63,7 @@ class MessageSendRequest
private $product;
/**
* @var Order $order
* @var MessageOrder $order
*
* @Type("Order")
* @Accessor(getter="getOrder",setter="setOrder")
@ -174,7 +176,7 @@ class MessageSendRequest
}
/**
* @return Product
* @return MessageProduct
*/
public function getProduct()
{
@ -182,15 +184,15 @@ class MessageSendRequest
}
/**
* @param Product $product
* @param MessageProduct $product
*/
public function setProduct(Product $product)
public function setProduct(MessageProduct $product)
{
$this->product = $product;
}
/**
* @return Order
* @return MessageOrder
*/
public function getOrder()
{
@ -198,9 +200,9 @@ class MessageSendRequest
}
/**
* @param Order $order
* @param MessageOrder $order
*/
public function setOrder(Order $order)
public function setOrder(MessageOrder $order)
{
$this->order = $order;
}

View File

@ -43,7 +43,7 @@ trait CommonFields
*/
public function getErrors(): array
{
return $this->errors;
return is_null($this->errors) ? [] : $this->errors;
}
/**
@ -59,6 +59,6 @@ trait CommonFields
*/
public function isSuccessful()
{
return (bool) empty($this->getErrors());
return empty($this->errors);
}
}

View File

@ -45,8 +45,8 @@ class ListResponse implements \Iterator, \ArrayAccess, \Countable {
/**
* ListResponse constructor.
*
* @param $responseType
* @param $data
* @param string $responseType
* @param array $data
*/
public function __construct($responseType, $data)
{
@ -149,7 +149,7 @@ class ListResponse implements \Iterator, \ArrayAccess, \Countable {
}
/**
* @param $name
* @param mixed $name
*
* @return mixed
* @internal

View File

@ -62,8 +62,8 @@ class Serializer
/**
* Deserialize given array or JSON to object
*
* @param $data
* @param $entityType
* @param mixed $data
* @param string self::normalizeNamespace($entityType)
* @param string $from
*
* @return array|object|null
@ -76,10 +76,10 @@ class Serializer
switch ($from) {
case self::S_ARRAY:
$deserialized = $serializer->fromArray(array_filter($data), $entityType, $context);
$deserialized = $serializer->fromArray(array_filter($data), self::normalizeNamespace($entityType), $context);
break;
case self::S_JSON:
$deserialized = $serializer->deserialize($data, $entityType, $from, $context);
$deserialized = $serializer->deserialize($data, self::normalizeNamespace($entityType), $from, $context);
break;
}
@ -103,4 +103,18 @@ class Serializer
return $context;
}
/**
* @param string $namespace
*
* @return bool|string
*/
private static function normalizeNamespace(string $namespace)
{
if (substr($namespace, 0, 1) == '\\') {
return substr($namespace, 1);
} else {
return $namespace;
}
}
}

View File

@ -89,7 +89,7 @@ class CommandsTest extends TestCase
$response = $client->commandEdit($request);
self::assertTrue($response instanceof ErrorOnlyResponse);
self::assertTrue(!$response->isError());
self::assertTrue($response->isSuccessful());
}
/**
@ -108,6 +108,6 @@ class CommandsTest extends TestCase
$response = $client->commandDelete("show_payment_types");
self::assertTrue($response->isError() == false);
self::assertTrue($response->isSuccessful() == true);
}
}

View File

@ -52,7 +52,7 @@ class MessagesTest extends TestCase
$response = $client->messageSend($request);
self::assertTrue($response->isError());
self::assertTrue(!$response->isSuccessful());
self::assertEquals(1, count($response->getErrors()));
}
}