Skip to content

Commit

Permalink
Merge pull request #59 from pwsdotru/issue-54
Browse files Browse the repository at this point in the history
Fix issue for call toJson on a non-object
  • Loading branch information
TheMY3 authored Apr 14, 2017
2 parents d6f8383 + 178dda6 commit 71e9c7e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,16 @@ public function toJson($inner = false)
foreach (static::$map as $key => $item) {
$property = lcfirst(self::toCamelCase($key));
if (!is_null($this->$property)) {
$output[$key] = $item === true ? $this->$property : $this->$property->toJson(true);
if (is_array($this->$property)) {
$output[$key] = array_map(
function ($v) {
return is_object($v) ? $v->toJson(true) : $v;
},
$this->$property
);
} else {
$output[$key] = $item === true ? $this->$property : $this->$property->toJson(true);
}
}
}

Expand Down
53 changes: 53 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -797,4 +797,57 @@ public function testGetMigrateFromChatId()

$this->assertEquals(3, $item->getMigrateFromChatId());
}

public function testToJson1()
{
$data = array(
'message_id' => 1,
'from' => array(
'first_name' => 'Ilya',
'last_name' => 'Gusev',
'id' => 123456,
'username' => 'iGusev'
),
'date' => 2,
'chat' => array(
'id' => 1,
'type' => 'group',
'title' => 'test chat'
),
'migrate_from_chat_id' => 3
);

$item = Message::fromResponse($data);
$this->assertJson(json_encode($data), $item->toJson());
}

public function testToJson2()
{
$data = array(
'message_id' => 1,
'from' => array(
'first_name' => 'Ilya',
'last_name' => 'Gusev',
'id' => 123456,
'username' => 'iGusev'
),
'date' => 2,
'chat' => array(
'id' => 1,
'type' => 'group',
'title' => 'test chat'
),
'entities' => array(
array(
"type" => "bot_command",
"offset" => 0,
"length" => 7,
)
),
'migrate_from_chat_id' => 3
);

$item = Message::fromResponse($data);
$this->assertJson(json_encode($data), $item->toJson());
}
}

0 comments on commit 71e9c7e

Please sign in to comment.