|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wordlift\Jsonld; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Wordlift\Assertions; |
|
7
|
|
|
use Wordlift_Jsonld_Service; |
|
8
|
|
|
use Wordlift_Term_JsonLd_Adapter; |
|
9
|
|
|
|
|
10
|
|
|
class Jsonld_Service { |
|
11
|
|
|
|
|
12
|
|
|
const TYPE_HOMEPAGE = 'HOMEPAGE'; |
|
13
|
|
|
const TYPE_POST = 'POST'; |
|
14
|
|
|
const TYPE_TERM = 'TERM'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Wordlift_Jsonld_Service |
|
18
|
|
|
*/ |
|
19
|
|
|
private $legacy_jsonld_service; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Wordlift_Term_JsonLd_Adapter |
|
23
|
|
|
*/ |
|
24
|
|
|
private $term_jsonld_service; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Jsonld_Service constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param Wordlift_Jsonld_Service $legacy_jsonld_service |
|
30
|
|
|
* @param Wordlift_Term_JsonLd_Adapter $term_jsonld_adapter |
|
31
|
|
|
* |
|
32
|
|
|
* @throws Exception |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct( $legacy_jsonld_service, $term_jsonld_adapter ) { |
|
35
|
|
|
|
|
36
|
|
|
Assertions::assert_of_type( $legacy_jsonld_service, 'Wordlift_Jsonld_Service' ); |
|
37
|
|
|
Assertions::assert_of_type( $term_jsonld_adapter, 'Wordlift_Term_JsonLd_Adapter' ); |
|
38
|
|
|
|
|
39
|
|
|
$this->legacy_jsonld_service = $legacy_jsonld_service; |
|
40
|
|
|
$this->term_jsonld_service = $term_jsonld_adapter; |
|
41
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get the JSON-LD structure for the specified type and id. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $type The requested type, one of 'HOMEPAGE', 'POST' or 'TERM'. Default 'POST'. |
|
48
|
|
|
* @param int|null $id The id. Default `null`. |
|
49
|
|
|
* |
|
50
|
|
|
* @return array The JSON-LD structure. |
|
51
|
|
|
* @throws Exception Throws an exception if the type isn't recognized. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function get( $type = self::TYPE_POST, $id = null ) { |
|
54
|
|
|
|
|
55
|
|
|
switch ( $type ) { |
|
56
|
|
|
case self::TYPE_HOMEPAGE: |
|
57
|
|
|
return $this->legacy_jsonld_service->get_jsonld( true, $id ); |
|
58
|
|
|
case self::TYPE_POST: |
|
59
|
|
|
return $this->legacy_jsonld_service->get_jsonld( false, $id ); |
|
60
|
|
|
case self::TYPE_TERM: |
|
61
|
|
|
return $this->term_jsonld_service->get( $id ); |
|
62
|
|
|
default: |
|
63
|
|
|
throw new Exception( "Unknown type $type. Allowed types: 'HOMEPAGE', 'POST', 'TERM'." ); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|