2020class SearchEngines
2121{
2222 /**
23+ * Associative array with search engine names as key and their base URLs as
24+ * value.
25+ *
2326 * @var array<string,string>
2427 */
2528 protected array $ engines = [
@@ -31,8 +34,18 @@ class SearchEngines
3134 'yahoo ' => 'https://search.yahoo.com/search?p= ' ,
3235 'yandex ' => 'https://yandex.com/search/?text= ' ,
3336 ];
37+ /**
38+ * Name of currently selected search engine.
39+ *
40+ * @var string
41+ */
3442 protected string $ current = 'google ' ;
3543
44+ /**
45+ * Instantiate the class and allows you to define the current search engine.
46+ *
47+ * @param string|null $current
48+ */
3649 public function __construct (?string $ current = null )
3750 {
3851 if (isset ($ current )) {
@@ -61,6 +74,8 @@ public function getAll() : array
6174 }
6275
6376 /**
77+ * Returns the array of search engines.
78+ *
6479 * @since 4.5
6580 *
6681 * @return array<string,string>
@@ -90,6 +105,8 @@ public function add(string $name, string $url) : static
90105 }
91106
92107 /**
108+ * Sets a search engine.
109+ *
93110 * @since 4.5
94111 *
95112 * @param string $name
@@ -106,6 +123,13 @@ public function setEngine(string $name, string $url) : static
106123 return $ this ;
107124 }
108125
126+ /**
127+ * Returns the base URL of an engine; throws exception if it does not exist.
128+ *
129+ * @param string $name
130+ *
131+ * @return string
132+ */
109133 public function getUrl (string $ name ) : string
110134 {
111135 if (!isset ($ this ->engines [$ name ])) {
@@ -114,6 +138,13 @@ public function getUrl(string $name) : string
114138 return $ this ->engines [$ name ];
115139 }
116140
141+ /**
142+ * Sets the current search engine; validates existence.
143+ *
144+ * @param string $name
145+ *
146+ * @return static
147+ */
117148 public function setCurrent (string $ name ) : static
118149 {
119150 if (!isset ($ this ->engines [$ name ])) {
@@ -123,16 +154,34 @@ public function setCurrent(string $name) : static
123154 return $ this ;
124155 }
125156
157+ /**
158+ * Returns the name of the current engine.
159+ *
160+ * @return string
161+ */
126162 public function getCurrent () : string
127163 {
128164 return $ this ->current ;
129165 }
130166
167+ /**
168+ * Returns the URL of the current engine.
169+ *
170+ * @return string
171+ */
131172 public function getCurrentUrl () : string
132173 {
133174 return $ this ->getUrl ($ this ->getCurrent ());
134175 }
135176
177+ /**
178+ * Generates a search link with the given query, using the current engine or a specific name.
179+ *
180+ * @param string $query
181+ * @param string|null $name
182+ *
183+ * @return string
184+ */
136185 public function makeLink (string $ query , ?string $ name = null ) : string
137186 {
138187 $ link = isset ($ name ) ? $ this ->getUrl ($ name ) : $ this ->getCurrentUrl ();
0 commit comments