]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - plugins/wallabag/WallabagInstance.php
Apply PHP Code Beautifier on source code for linter automatic fixes
[github/shaarli/Shaarli.git] / plugins / wallabag / WallabagInstance.php
1 <?php
2
3 namespace Shaarli\Plugin\Wallabag;
4
5 /**
6 * Class WallabagInstance.
7 */
8 class WallabagInstance
9 {
10 /**
11 * @var array Static reference to differrent WB API versions.
12 * - key: version ID, must match plugin settings.
13 * - value: version name.
14 */
15 private static $wallabagVersions = [
16 1 => '1.x',
17 2 => '2.x',
18 ];
19
20 /**
21 * @var array Static reference to WB endpoint according to the API version.
22 * - key: version name.
23 * - value: endpoint.
24 */
25 private static $wallabagEndpoints = [
26 '1.x' => '?plainurl=',
27 '2.x' => 'bookmarklet?url=',
28 ];
29
30 /**
31 * @var string Wallabag user instance URL.
32 */
33 private $instanceUrl;
34
35 /**
36 * @var string Wallabag user instance API version.
37 */
38 private $apiVersion;
39
40 public function __construct($instance, $version)
41 {
42 if ($this->isVersionAllowed($version)) {
43 $this->apiVersion = self::$wallabagVersions[$version];
44 } else {
45 // Default API version: 1.x.
46 $this->apiVersion = self::$wallabagVersions[1];
47 }
48
49 $this->instanceUrl = add_trailing_slash($instance);
50 }
51
52 /**
53 * Build the Wallabag URL to reach from instance URL and API version endpoint.
54 *
55 * @return string wallabag url.
56 */
57 public function getWallabagUrl()
58 {
59 return $this->instanceUrl . self::$wallabagEndpoints[$this->apiVersion];
60 }
61
62 /**
63 * Checks version configuration.
64 *
65 * @param mixed $version given version ID.
66 *
67 * @return bool true if it's valid, false otherwise.
68 */
69 private function isVersionAllowed($version)
70 {
71 return isset(self::$wallabagVersions[$version]);
72 }
73 }