Completed
Pull Request — master (#51)
by
unknown
03:04
created

InlineQueryResults::newArticle()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 23
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 20
nc 2
nop 6
crap 6
1
<?php
2
3
/*
4
 * This file is part of the PhpBotFramework.
5
 *
6
 * PhpBotFramework is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, version 3.
9
 *
10
 * PhpBotFramework is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace PhpBotFramework\Entities;
20
21
use PhpBotFramework\Exceptions\BotException;
22
23
/**
24
 * \addtogroup Entities Entities
25
 * @{
26
 */
27
28
/**
29
 * \class InlineQueryResults InlineQueryResults
30
 * \brief Handle and store results before sending them to an <code>answerInlineQuery</code> API call.
31
 */
32
class InlineQueryResults
33
{
34
    /**
35
     * \addtogroup InlineQueryResults InlineQueryResults
36
     * \brief Handles and stores results before sending them to an answerInlineQuery API call.
37
     * \details In order to answer <i>inline queries</i>: create an object of type
38
     * PhpBotFramework\Entities\InlineQueryResults and add the wanted results
39
     * using InlineQueryResults::addResult() method or
40
     * type-based result method (InlineQueryResults::newArticle()).
41
     *
42
     *     use PhpBotFramework\Entities\InlineQueryResults;
43
     *     use PhpBotFramework\Entities\InlineQuery;
44
     *     ~
45
     *     ~
46
     *     ~
47
     *         processInlineQuery(InlineQuery $inline_query) {
48
     *             $handler = new InlineQueryResults();
49
     *             $handler->newArticle('First article', 'This is the first result');
50
     *
51
     *             $this->answerInlineQuery($handler->get());
52
     *         }
53
     *
54
     * @{
55
     */
56
57
    /** \brief Array of the results stored. */
58
    private $results;
59
60
    /** \brief Counts the result so we can assign them an unique id. */
61
    private $id_result;
62
63
    /** \brief Accepted types for results. */
64
    private $accepted_type = [
65
        'audio',
66
        'article',
67
        'photo',
68
        'gif',
69
        'mpeg4_gif',
70
        'video',
71
        'voice',
72
        'document',
73
        'location',
74
        'venue',
75
        'contact',
76
        'game'
77
    ];
78
79
    /**
80
     * \constructor Create an InlineQueryResult object. */
81
    public function __construct()
82
    {
83
        // Initialize the array to empty
84
        $this->results = [];
85
        $this->id_result = 0;
86
    }
87
88
    /**
89
     * \brief Add a result passing an array containing data.
90
     * \details Create a result of one of these types:
91
     * - InlineQueryResultCachedAudio
92
     * - InlineQueryResultCachedDocument
93
     * - InlineQueryResultCachedGif
94
     * - InlineQueryResultCachedMpeg4Gif
95
     * - InlineQueryResultCachedPhoto
96
     * - InlineQueryResultCachedSticker
97
     * - InlineQueryResultCachedVideo
98
     * - InlineQueryResultCachedVoice
99
     * - InlineQueryResultArticle
100
     * - InlineQueryResultAudio
101
     * - InlineQueryResultContact
102
     * - InlineQueryResultGame
103
     * - InlineQueryResultDocument
104
     * - InlineQueryResultGif
105
     * - InlineQueryResultLocation
106
     * - InlineQueryResultMpeg4Gif
107
     * - InlineQueryResultPhoto
108
     * - InlineQueryResultVenue
109
     * - InlineQueryResultVideo
110
     * - InlineQueryResultVoice
111
     *
112
     * To add a result, create an array containing data as showed on API Reference,
113
     *'id' parameter will be automatically genereted so there is no need to add it.
114
     *
115
     * Example of adding an article result:
116
     *
117
     *     $data = [
118
     *         'type' => 'result',
119
     *         'title' => 'Example title',
120
     *         'message_text' => 'Text of the message'
121
     *     ];
122
     *
123
     *     $handler->addResult($data);
124
     *
125
     * @param array $result Array containing data result to add.
126
     * @return int Id of the result added.
127
     */
128
    public function addResult(array $result) : int
129
    {
130
        if (array_key_exists('type', $result) && ! in_array($result['type'], $this->accepted_type)) {
131
            throw new BotException("Result has wrong or no type at all. Check that the result has a value of key 'type' that correspond to a type in the API Reference");
132
        }
133
134
        // Set the id of the result to add
135
        $result['id'] = (string)$this->id_result;
136
137
        $this->results[] = $result;
138
        return $this->id_result++;
139
    }
140
141
    /**
142
     * \brief Add a result of type Article.
143
     * \details Add a result that will be show to the user.
144
     * @param string $title Title of the result.
145
     * @param string $message_text Text of the message to be sent.
146
     * @param string $description <i>Optional</i>. Short description of the result
147
     * @param array $reply_markup Inline keyboard object.
148
     * Not JSON serialized, use getArray from InlineKeyboard class.
149
     * @param string $parse_mode <i>Optional</i>. Formattation style for the message.
150
     * @param string $disable_web_preview <i>Optional</i>. Disables link previews for
151
     * links in the sent message.
152
     * @return int Id the the article added.
153
     */
154
    public function newArticle(
155
        string $title,
156
        string $message_text,
157
        string $description = '',
158
        array $reply_markup = null,
159
        $parse_mode = 'HTML',
160
        $disable_web_preview = false
161
    ) : int {
162
    
163
        array_push($this->results, [
164
            'type' => 'article',
165
            'id' => (string)$this->id_result,
166
            'title' => $title,
167
            'message_text' => $message_text,
168
            'description' => $description,
169
            'parse_mode' => $parse_mode,
170
            'reply_markup' => $reply_markup,
171
            'disable_web_page_preview' => $disable_web_preview
172
        ]);
173
        
174
        if ( is_null($reply_markup) ) {
175
            unset( $this->results[ $this->id_result ]['reply_markup'] );
176
        }
177
178
        return $this->id_result++;
179
    }
180
181
    /**
182
     * \brief Get all results created.
183
     * @return string JSON string containing the results.
184
     */
185
    public function get()
186
    {
187
        $encoded_results = json_encode($this->results);
188
189
        // Clear results by resetting ID counter and results' container
190
        $this->results = [];
191
        $this->id_result = 0;
192
193
        return $encoded_results;
194
    }
195
196
    /** @} */
197
198
    /** @} */
199
}
200