1 | <?php |
||
23 | class Localization |
||
24 | { |
||
25 | use File, |
||
26 | LocalizedString; |
||
27 | /** |
||
28 | * \addtogroup Localization Localization |
||
29 | * \brief Create a localized bot. |
||
30 | * \details Using both a sql database and a redis database you can develop a localized bot with just a small impact on the performance. |
||
31 | * By default the sql database will store the language permanently in a table which name is defined in $user_table. |
||
32 | * The redis database will cache the language for the chat_id for a variable time. |
||
33 | * You can use only the sql database if a redis database is not accessible. |
||
34 | * These methods will treat groups as User so they will be stored in the table as normal user does. |
||
35 | * @{ |
||
36 | */ |
||
37 | |||
38 | /** \brief Reference to the bot. */ |
||
39 | protected $bot; |
||
40 | |||
41 | /** \brief Table containing bot users data into database. */ |
||
42 | public $user_table = 'User'; |
||
43 | |||
44 | /** \brief Name of the column that represents the user ID into database */ |
||
45 | public $id_column = 'chat_id'; |
||
46 | |||
47 | /** \brief Current user/group language. */ |
||
48 | public $language; |
||
49 | |||
50 | public function __construct(BasicBot &$bot) |
||
54 | |||
55 | /** |
||
56 | * \brief Get current user's language from the database, and set it in $language. |
||
57 | * @return string Language set for the current user, throw error if there is language is not set for the user. |
||
58 | */ |
||
59 | public function getLanguageDatabase() : string |
||
85 | |||
86 | /** |
||
87 | * \brief Set the current user language in database and internally. |
||
88 | * \details Save it into database first. |
||
89 | * @param string $language The language to set. |
||
90 | * @return bool On sucess, return true, throws exception otherwise. |
||
91 | */ |
||
92 | public function setLanguageDatabase(string $language) : bool |
||
114 | |||
115 | /** |
||
116 | * \brief Get current user language from Redis (as a cache) and set it in language. |
||
117 | * \details Using Redis as cache, check for the language. On failure, get the language |
||
118 | * from the database and store it (with default expiring time of one day) in Redis. |
||
119 | * |
||
120 | * It also change $language parameter of the bot to the language returned. |
||
121 | * @param int $expiring_time <i>Optional</i>. Set the expiring time for the language on redis each time it is took from the sql database. |
||
122 | * @return string Language for the current user, $default_language on errors. |
||
123 | */ |
||
124 | public function getLanguageRedis(int $expiring_time = 86400) : string |
||
143 | |||
144 | /** |
||
145 | * \brief Set the current user language in both Redis, database and internally. |
||
146 | * \details Save it into database first, then create the expiring key on Redis. |
||
147 | * @param string $language The language to set. |
||
148 | * @param int $expiring_time <i>Optional</i>. Time for the language key in redis to expire. |
||
149 | * @return bool On sucess, return true, throws exception otherwise. |
||
150 | */ |
||
151 | public function setLanguageRedis(string $language, int $expiring_time = 86400) : bool |
||
164 | |||
165 | /** @} */ |
||
166 | } |
||
167 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: