Skip to content

Commit e7aff2f

Browse files
committed
Finalize bringing over the discovery library
1 parent 5feb10a commit e7aff2f

2 files changed

Lines changed: 130 additions & 1 deletion

File tree

lib/discovery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace WDS_WP_REST_API;
33

4-
use WordPress\Discovery\Site;
4+
use WDS_WP_REST_API\Discover\Site;
55
use Exception;
66
use Requests;
77
use Requests_Exception_HTTP;

lib/site.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
namespace WDS_WP_REST_API\Discover;
3+
4+
/**
5+
* Site data from an API index.
6+
*/
7+
class Site {
8+
/**
9+
* Data from the API index.
10+
*
11+
* @var stdClass
12+
*/
13+
protected $data;
14+
15+
/**
16+
* API index URL.
17+
*
18+
* @var string
19+
*/
20+
protected $index;
21+
22+
/**
23+
* Constructor.
24+
*
25+
* @param stdClass $data Data from the API index.
26+
* @param string $index API index URL.
27+
*/
28+
public function __construct( $data, $index ) {
29+
$this->data = $data;
30+
$this->index = $index;
31+
}
32+
33+
/**
34+
* Get the name of a site.
35+
*
36+
* @return string
37+
*/
38+
public function getName() {
39+
return $this->data->name;
40+
}
41+
42+
/**
43+
* Get the description for a site.
44+
*
45+
* @return string
46+
*/
47+
public function getDescription() {
48+
return $this->data->description;
49+
}
50+
51+
/**
52+
* Get the URL for a site.
53+
*
54+
* @return string
55+
*/
56+
public function getURL() {
57+
return $this->data->url;
58+
}
59+
60+
/**
61+
* Get the index URL for the API.
62+
*
63+
* @return string
64+
*/
65+
public function getIndexURL() {
66+
return $this->index;
67+
}
68+
69+
/**
70+
* Get namespaces supported by the site.
71+
*
72+
* @return string[] List of namespaces supported by the site.
73+
*/
74+
public function getSupportedNamespaces() {
75+
if ( empty( $this->data->namespaces ) || ! is_array( $this->data->namespaces ) ) {
76+
return array();
77+
}
78+
79+
return $this->data->namespaces;
80+
}
81+
82+
/**
83+
* Does the site support a namespace?
84+
*
85+
* @param string $namespace Namespace to check.
86+
* @return bool True if supported by the site, false otherwise.
87+
*/
88+
public function supportsNamespace( $namespace ) {
89+
return in_array( $namespace, $this->getSupportedNamespaces() );
90+
}
91+
92+
/**
93+
* Get features supported by the site.
94+
*
95+
* @return array Map of authentication method => method-specific data.
96+
*/
97+
public function getSupportedAuthentication() {
98+
if ( empty( $this->data->authentication ) || empty( $this->data->authentication ) ) {
99+
return array();
100+
}
101+
102+
return (array) $this->data->authentication;
103+
}
104+
105+
/**
106+
* Does the site support an authentication method?
107+
*
108+
* @param string $method Authentication method to check.
109+
* @return bool True if supported by the site, false otherwise.
110+
*/
111+
public function supportsAuthentication( $method ) {
112+
return array_key_exists( $method, $this->getSupportedAuthentication() );
113+
}
114+
115+
/**
116+
* Get method-specific data for the given authentication method.
117+
*
118+
* @param string $method Authentication method to get data for.
119+
* @return mixed Method-specific data if available, null if not supported.
120+
*/
121+
public function getAuthenticationData( $method ) {
122+
if ( ! $this->supportsAuthentication( $method ) ) {
123+
return null;
124+
}
125+
126+
$authentication = $this->getSupportedAuthentication();
127+
return $authentication[ $method ];
128+
}
129+
}

0 commit comments

Comments
 (0)