aboutsummaryrefslogtreecommitdiffhomepage
path: root/plugins/wallabag/WallabagInstance.php
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/wallabag/WallabagInstance.php')
-rw-r--r--plugins/wallabag/WallabagInstance.php71
1 files changed, 71 insertions, 0 deletions
diff --git a/plugins/wallabag/WallabagInstance.php b/plugins/wallabag/WallabagInstance.php
new file mode 100644
index 00000000..87352e66
--- /dev/null
+++ b/plugins/wallabag/WallabagInstance.php
@@ -0,0 +1,71 @@
1<?php
2
3/**
4 * Class WallabagInstance.
5 */
6class WallabagInstance
7{
8 /**
9 * @var array Static reference to differrent WB API versions.
10 * - key: version ID, must match plugin settings.
11 * - value: version name.
12 */
13 private static $wallabagVersions = array(
14 1 => '1.x',
15 2 => '2.x',
16 );
17
18 /**
19 * @var array Static reference to WB endpoint according to the API version.
20 * - key: version name.
21 * - value: endpoint.
22 */
23 private static $wallabagEndpoints = array(
24 '1.x' => '?plainurl=',
25 '2.x' => 'bookmarklet?url=',
26 );
27
28 /**
29 * @var string Wallabag user instance URL.
30 */
31 private $instanceUrl;
32
33 /**
34 * @var string Wallabag user instance API version.
35 */
36 private $apiVersion;
37
38 function __construct($instance, $version)
39 {
40 if ($this->isVersionAllowed($version)) {
41 $this->apiVersion = self::$wallabagVersions[$version];
42 } else {
43 // Default API version: 1.x.
44 $this->apiVersion = self::$wallabagVersions[1];
45 }
46
47 $this->instanceUrl = add_trailing_slash($instance);
48 }
49
50 /**
51 * Build the Wallabag URL to reach from instance URL and API version endpoint.
52 *
53 * @return string wallabag url.
54 */
55 public function getWallabagUrl()
56 {
57 return $this->instanceUrl . self::$wallabagEndpoints[$this->apiVersion];
58 }
59
60 /**
61 * Checks version configuration.
62 *
63 * @param mixed $version given version ID.
64 *
65 * @return bool true if it's valid, false otherwise.
66 */
67 private function isVersionAllowed($version)
68 {
69 return isset(self::$wallabagVersions[$version]);
70 }
71}