diff --git a/src/Bot/Client.php b/src/Bot/Client.php index 56a8369..579894e 100644 --- a/src/Bot/Client.php +++ b/src/Bot/Client.php @@ -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 ); } diff --git a/src/Bot/HttpClient.php b/src/Bot/HttpClient.php index 616fd4e..8b3deac 100644 --- a/src/Bot/HttpClient.php +++ b/src/Bot/HttpClient.php @@ -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); } diff --git a/src/Bot/Model/Entity/Chat/ChatLastMessage.php b/src/Bot/Model/Entity/Chat/ChatLastMessage.php index 08b366d..0026569 100644 --- a/src/Bot/Model/Entity/Chat/ChatLastMessage.php +++ b/src/Bot/Model/Entity/Chat/ChatLastMessage.php @@ -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; } diff --git a/src/Bot/Model/Request/MessageSendRequest.php b/src/Bot/Model/Request/MessageSendRequest.php index 254134f..f89550f 100644 --- a/src/Bot/Model/Request/MessageSendRequest.php +++ b/src/Bot/Model/Request/MessageSendRequest.php @@ -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; } diff --git a/src/Bot/Model/Response/CommonFields.php b/src/Bot/Model/Response/CommonFields.php index 72891d6..18b121b 100644 --- a/src/Bot/Model/Response/CommonFields.php +++ b/src/Bot/Model/Response/CommonFields.php @@ -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); } } diff --git a/src/Bot/Model/Response/ListResponse.php b/src/Bot/Model/Response/ListResponse.php index 721cb3d..7032fd2 100644 --- a/src/Bot/Model/Response/ListResponse.php +++ b/src/Bot/Model/Response/ListResponse.php @@ -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 diff --git a/src/Serializer.php b/src/Serializer.php index 25244c7..77e5cc7 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -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; + } + } } diff --git a/tests/Bot/Tests/CommandsTest.php b/tests/Bot/Tests/CommandsTest.php index acafd7c..15a4311 100644 --- a/tests/Bot/Tests/CommandsTest.php +++ b/tests/Bot/Tests/CommandsTest.php @@ -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); } } diff --git a/tests/Bot/Tests/MessagesTest.php b/tests/Bot/Tests/MessagesTest.php index 72c33c9..c2ad240 100644 --- a/tests/Bot/Tests/MessagesTest.php +++ b/tests/Bot/Tests/MessagesTest.php @@ -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())); } }