1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Igorsgm\TibiaDataApi\Response; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Igorsgm\TibiaDataApi\Exceptions\NotFoundException; |
7
|
|
|
use Igorsgm\TibiaDataApi\Models\Character; |
8
|
|
|
use Igorsgm\TibiaDataApi\Models\Character\AccountInformation; |
9
|
|
|
use Igorsgm\TibiaDataApi\Models\Character\Achievement; |
10
|
|
|
use Igorsgm\TibiaDataApi\Models\Character\Death; |
11
|
|
|
use Igorsgm\TibiaDataApi\Models\Character\Guild; |
12
|
|
|
use Igorsgm\TibiaDataApi\Models\Character\OtherCharacter; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @see https://tibiadata.com/doc-api-v2/characters/ |
16
|
|
|
*/ |
17
|
|
|
class CharacterResponse extends AbstractResponse |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Character |
21
|
|
|
*/ |
22
|
|
|
private $character; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* CharacterResponse constructor. |
26
|
|
|
* @param \stdClass $response |
27
|
|
|
* @throws NotFoundException |
28
|
|
|
* @throws \Igorsgm\TibiaDataApi\Exceptions\ImmutableException |
29
|
|
|
* @throws \Exception |
30
|
|
|
*/ |
31
|
|
|
public function __construct(\stdClass $response) |
32
|
|
|
{ |
33
|
|
|
if (isset($response->characters->error)) { |
34
|
|
|
throw new NotFoundException('Character does not exists.'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$achievements = collect(); |
38
|
|
|
foreach ($response->characters->achievements as $achievement) { |
39
|
|
|
$achievements->push(new Achievement($achievement->name, $achievement->stars)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$accountInformation = null; |
43
|
|
|
if (!empty($response->characters->account_information)) { |
44
|
|
|
$accountInformation = new AccountInformation( |
45
|
|
|
$response->characters->account_information->loyalty_title, |
46
|
|
|
new Carbon( |
47
|
|
|
$response->characters->account_information->created->date, |
48
|
|
|
$response->characters->account_information->created->timezone |
49
|
|
|
) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$deaths = collect(); |
54
|
|
|
if (!empty($response->characters->deaths)) { |
55
|
|
|
foreach ($response->characters->deaths as $death) { |
56
|
|
|
$involved = collect(); |
57
|
|
|
foreach ($death->involved as $deathInvolved) { |
58
|
|
|
$involved->push($deathInvolved->name); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$deaths->push(new Death( |
62
|
|
|
new Carbon($death->date->date, $death->date->timezone), |
63
|
|
|
$death->level, |
64
|
|
|
$death->reason, |
65
|
|
|
$involved |
66
|
|
|
)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$otherCharacters = collect(); |
71
|
|
|
if (!empty($response->characters->account_information)) { |
72
|
|
|
foreach ($response->characters->other_characters as $other_character) { |
73
|
|
|
$otherCharacters->push(new OtherCharacter( |
74
|
|
|
$other_character->name, |
75
|
|
|
$other_character->world, |
76
|
|
|
$other_character->status |
77
|
|
|
)); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$guild = null; |
82
|
|
|
if (isset($response->characters->data->guild)) { |
83
|
|
|
$guild = new Guild($response->characters->data->guild->name, $response->characters->data->guild->rank); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$lastLogin = null; |
87
|
|
|
if (isset($response->characters->data->last_login)) { |
88
|
|
|
$lastLogin = new Carbon( |
89
|
|
|
$response->characters->data->last_login[0]->date, |
90
|
|
|
$response->characters->data->last_login[0]->timezone |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->character = Character::createFromArray([ |
95
|
|
|
'name' => $response->characters->data->name, |
96
|
|
|
'former_names' => !empty($response->characters->data->former_names) |
97
|
|
|
? collect($response->characters->data->former_names) |
98
|
|
|
: collect(), |
99
|
|
|
'sex' => $response->characters->data->sex, |
100
|
|
|
'vocation' => $response->characters->data->vocation, |
101
|
|
|
'level' => $response->characters->data->level, |
102
|
|
|
'achievement_points' => $response->characters->data->achievement_points, |
103
|
|
|
'world' => $response->characters->data->world, |
104
|
|
|
'former_world' => $response->characters->data->former_world ?? null, |
105
|
|
|
'residence' => $response->characters->data->residence, |
106
|
|
|
'guild' => $guild, |
107
|
|
|
'last_login' => $lastLogin, |
108
|
|
|
'comment' => $response->characters->data->comment ?? '', |
109
|
|
|
'account_status' => $response->characters->data->account_status, |
110
|
|
|
'status' => $response->characters->data->status, |
111
|
|
|
'achievements' => $achievements, |
112
|
|
|
'account_information' => $accountInformation, |
113
|
|
|
'deaths' => $deaths, |
114
|
|
|
'other_characters' => $otherCharacters, |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
parent::__construct($response); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return Character |
122
|
|
|
*/ |
123
|
|
|
public function getCharacter(): Character |
124
|
|
|
{ |
125
|
|
|
return $this->character; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|