Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix one-to-many relations #964

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/Objects/BaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function getPropertyValue($property, $default = null)

$relations = $this->relations();
if (isset($relations[$property])) {
return $relations[$property]::make($value);
return $this->getRelationValue($property, $value);
}

/** @var BaseObject $class */
Expand All @@ -77,6 +77,34 @@ protected function getPropertyValue($property, $default = null)
return $value;
}

/**
* @param string $relationName
* @param array $relationRawData
* @return array|\Illuminate\Support\Enumerable|\Illuminate\Support\Traits\EnumeratesValues|\Telegram\Bot\Objects\BaseObject
*/
protected function getRelationValue(string $relationName, iterable $relationRawData)
{
/** @var class-string<\Telegram\Bot\Objects\BaseObject>|list<class-string<\Telegram\Bot\Objects\BaseObject>> $relation */
$relation = $this->relations()[$relationName];

if (is_string($relation) && class_exists($relation)) {
return $relation::make($relationRawData);
}

$isOneToManyRelation = is_array($relation);
if ($isOneToManyRelation) {
/** @var class-string<\Telegram\Bot\Objects\BaseObject> $clasString */
$clasString = $relation[0];
$relatedObjects = Collection::make(); // @todo array type can be used in v4
foreach ($relationRawData as $singleObjectRawData) {
$relatedObjects[] = $clasString::make($singleObjectRawData);
}
return $relatedObjects;
}

throw new \InvalidArgumentException("Unknown type of the relationship data for the $relationName relation.");
}

/**
* Get an item from the collection by key.
*
Expand Down
6 changes: 3 additions & 3 deletions src/Objects/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class Game extends BaseObject
public function relations()
{
return [
'photo' => PhotoSize::class,
'text_entities' => MessageEntity::class,
'animation' => Animation::class,
'photo' => [PhotoSize::class],
'text_entities' => [MessageEntity::class],
'animation' => Animation::class,
];
}
}
10 changes: 5 additions & 5 deletions src/Objects/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ public function relations()
'forward_from' => User::class,
'forward_from_chat' => Chat::class,
'reply_to_message' => self::class,
'entities' => MessageEntity::class,
'caption_entities' => MessageEntity::class,
'entities' => [MessageEntity::class],
'caption_entities' => [MessageEntity::class],
'audio' => Audio::class,
'dice' => Dice::class,
'animation' => Animation::class,
'document' => Document::class,
'game' => Game::class,
'photo' => PhotoSize::class,
'photo' => [PhotoSize::class],
'sticker' => Sticker::class,
'video' => Video::class,
'voice' => Voice::class,
Expand All @@ -88,9 +88,9 @@ public function relations()
'venue' => Venue::class,
'poll' => Poll::class,
'new_chat_member' => ChatMember::class,
'new_chat_members' => User::class,
'new_chat_members' => [User::class],
'left_chat_member' => User::class,
'new_chat_photo' => PhotoSize::class,
'new_chat_photo' => [PhotoSize::class],
'delete_chat_photo' => ChatPhoto::class,
'pinned_message' => self::class,
'invoice' => Invoice::class,
Expand Down
4 changes: 2 additions & 2 deletions src/Objects/Passport/EncryptedPassportElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class EncryptedPassportElement extends BaseObject
public function relations()
{
return [
'files' => PassportFile::class,
'files' => [PassportFile::class],
'front_side' => PassportFile::class,
'reverse_side' => PassportFile::class,
'selfie' => PassportFile::class,
'translation' => PassportFile::class,
'translation' => [PassportFile::class],
];
}
}
4 changes: 2 additions & 2 deletions src/Objects/Passport/SecureValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function relations()
'front_side' => FileCredentials::class,
'reverse_side' => FileCredentials::class,
'selfie' => FileCredentials::class,
'translation' => FileCredentials::class,
'files' => FileCredentials::class,
'translation' => [FileCredentials::class],
'files' => [FileCredentials::class],
];
}
}
2 changes: 1 addition & 1 deletion src/Objects/Payments/ShippingOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ShippingOption extends BaseObject
public function relations()
{
return [
'prices' => LabeledPrice::class,
'prices' => [LabeledPrice::class],
];
}
}
2 changes: 1 addition & 1 deletion src/Objects/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Poll extends BaseObject
public function relations()
{
return [
'options' => PollOption::class,
'options' => [PollOption::class],
];
}
}
2 changes: 1 addition & 1 deletion src/Objects/StickerSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class StickerSet extends BaseObject
public function relations()
{
return [
'stickers' => Sticker::class,
'stickers' => [Sticker::class],
'thumb' => PhotoSize::class,
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Objects/UserProfilePhotos.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class UserProfilePhotos extends BaseObject
public function relations()
{
return [
'photos' => PhotoSize::class,
'photos' => [PhotoSize::class],
];
}
}
31 changes: 31 additions & 0 deletions tests/Unit/Objects/MessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Telegram\Bot\Tests\Unit\Objects;

use PHPUnit\Framework\TestCase;
use Telegram\Bot\Objects\Message;
use Telegram\Bot\Objects\PhotoSize;

/** @covers \Telegram\Bot\Objects\Message */
class MessageTest extends TestCase
{
/** @test */
public function it_inits_one_to_many_relation(): void
{
$message = Message::make([
'photo' => [
['file_id' => ''],
['file_id' => ''],
['file_id' => ''],
['file_id' => ''],
],
]);

$this->assertIsIterable($message->photo);
$this->assertCount(4, $message->photo);
$this->assertInstanceOf(PhotoSize::class, $message->photo[0]);
$this->assertInstanceOf(PhotoSize::class, $message->photo[1]);
$this->assertInstanceOf(PhotoSize::class, $message->photo[2]);
$this->assertInstanceOf(PhotoSize::class, $message->photo[3]);
}
}