]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - plugins/wallabag/WallabagInstance.php
Apply PHP Code Beautifier on source code for linter automatic fixes
[github/shaarli/Shaarli.git] / plugins / wallabag / WallabagInstance.php
CommitLineData
c0a50f36 1<?php
53054b2b 2
95854417 3namespace Shaarli\Plugin\Wallabag;
c0a50f36
A
4
5/**
6 * Class WallabagInstance.
7 */
8class 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 */
53054b2b 15 private static $wallabagVersions = [
c0a50f36
A
16 1 => '1.x',
17 2 => '2.x',
53054b2b 18 ];
c0a50f36
A
19
20 /**
21 * @var array Static reference to WB endpoint according to the API version.
22 * - key: version name.
23 * - value: endpoint.
24 */
53054b2b 25 private static $wallabagEndpoints = [
c0a50f36
A
26 '1.x' => '?plainurl=',
27 '2.x' => 'bookmarklet?url=',
53054b2b 28 ];
c0a50f36
A
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
93b1fe54 40 public function __construct($instance, $version)
c0a50f36
A
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}