|
37 | 37 | * @method int getMaxVotes()
|
38 | 38 | *
|
39 | 39 | * @psalm-import-type TalkPoll from ResponseDefinitions
|
| 40 | + * @psalm-import-type TalkPollDraft from ResponseDefinitions |
40 | 41 | */
|
41 | 42 | class Poll extends Entity {
|
42 | 43 | public const STATUS_OPEN = 0;
|
@@ -75,25 +76,32 @@ public function __construct() {
|
75 | 76 | /**
|
76 | 77 | * @return TalkPoll
|
77 | 78 | */
|
78 |
| - public function asArray(): array { |
| 79 | + public function renderAsPoll(): array { |
| 80 | + $data = $this->renderAsDraft(); |
79 | 81 | $votes = json_decode($this->getVotes(), true, 512, JSON_THROW_ON_ERROR);
|
80 | 82 |
|
81 | 83 | // Because PHP is turning arrays with sequent numeric keys "{"0":x,"1":y,"2":z}" into "[x,y,z]"
|
82 | 84 | // when json_encode() is used we have to prefix the keys with a string,
|
83 | 85 | // to prevent breaking in the mobile apps.
|
84 |
| - $prefixedVotes = []; |
| 86 | + $data['votes'] = []; |
85 | 87 | foreach ($votes as $option => $count) {
|
86 |
| - $prefixedVotes['option-' . $option] = $count; |
| 88 | + $data['votes']['option-' . $option] = $count; |
87 | 89 | }
|
| 90 | + $data['numVoters'] = $this->getNumVoters(); |
88 | 91 |
|
| 92 | + return $data; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @return TalkPollDraft |
| 97 | + */ |
| 98 | + public function renderAsDraft(): array { |
89 | 99 | return [
|
90 | 100 | 'id' => $this->getId(),
|
91 | 101 | // The room id is not needed on the API level but only internally for optimising database queries
|
92 | 102 | // 'roomId' => $this->getRoomId(),
|
93 | 103 | 'question' => $this->getQuestion(),
|
94 | 104 | 'options' => json_decode($this->getOptions(), true, 512, JSON_THROW_ON_ERROR),
|
95 |
| - 'votes' => $prefixedVotes, |
96 |
| - 'numVoters' => $this->getNumVoters(), |
97 | 105 | 'actorType' => $this->getActorType(),
|
98 | 106 | 'actorId' => $this->getActorId(),
|
99 | 107 | 'actorDisplayName' => $this->getDisplayName(),
|
|
0 commit comments