Completed
Push — develop ( f65bd6...158ffc )
by David
08:55
created

Wordlift_Sparql_Tuple_Rendition::get()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Renditions: Sparql Tuple Rendition.
4
 *
5
 * Renders a property (accessed using a {@link Wordlift_Storage} instance) to
6
 * a tuple for use in SPARQL statements.
7
 *
8
 * @since      3.15.0
9
 * @package    Wordlift
10
 * @subpackage Wordlift/includes
11
 */
12
13
/**
14
 * Define the {@link Wordlift_Sparql_Tuple_Rendition} class.
15
 *
16
 * @since      3.15.0
17
 * @package    Wordlift
18
 * @subpackage Wordlift/includes
19
 */
20
class Wordlift_Sparql_Tuple_Rendition {
21
22
	/**
23
	 * A {@link Wordlift_Storage} instance to read a property.
24
	 *
25
	 * @since  3.15.0
26
	 * @access private
27
	 * @var \Wordlift_Storage $storage A {@link Wordlift_Storage} instance to
28
	 *                                 read a property.
29
	 */
30
	private $storage;
31
32
	/**
33
	 * The predicate URI.
34
	 *
35
	 * @since  3.15.0
36
	 * @access private
37
	 * @var string $predicate The predicate URI.
38
	 */
39
	private $predicate;
40
41
	/**
42
	 * The data type (or null if not set).
43
	 *
44
	 * @since  3.15.0
45
	 * @access private
46
	 * @var string|null $data_type The data type (or null if not set).
47
	 */
48
	private $data_type;
49
50
	/**
51
	 * The language (or null if not set).
52
	 *
53
	 * @since  3.15.0
54
	 * @access private
55
	 * @var string|null $language The language (or null if not set).
56
	 */
57
	private $language;
58
59
	/**
60
	 * The {@link Wordlift_Entity_Service} instance.
61
	 *
62
	 * @since  3.15.0
63
	 * @access private
64
	 * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance.
65
	 */
66
	private $entity_service;
67
68
	/**
69
	 * Create a {@link Wordlift_Sparql_Tuple_Rendition} instance.
70
	 *
71
	 * @since 3.15.0
72
	 *
73
	 * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service}
74
	 *                                                 instance.
75
	 * @param \Wordlift_Storage        $storage        The {@link Wordlift_Storage}
76
	 *                                                 instance.
77
	 * @param string                   $predicate      The predicate URI.
78
	 * @param string|null              $data_type      The data type or null.
79
	 * @param string|null              $language       The language code or null.
80
	 */
81
	public function __construct( $entity_service, $storage, $predicate, $data_type = null, $language = null ) {
82
83
		$this->entity_service = $entity_service;
84
		$this->storage        = $storage;
85
		$this->predicate      = $predicate;
86
		$this->data_type      = $data_type;
87
		$this->language       = $language;
88
89
	}
90
91
	/**
92
	 * Get tuple representations for the specified {@link WP_Post}.
93
	 *
94
	 * @since 3.15.0
95
	 *
96
	 * @param int $post_id The {@link WP_Post}'s id.
97
	 *
98
	 * @return array An array of tuples.
99
	 */
100
	public function get( $post_id ) {
101
102
		// Get the entity URI.
103
		$uri = $this->entity_service->get_uri( $post_id );
104
105
		// Get the predicate, data type and language.
106
		$predicate = $this->predicate;
107
		$data_type = $this->data_type;
108
		$language  = $this->language;
109
110
		// Filter out empty values.
111
		$values = array_filter( (array) $this->storage->get( $post_id ), function ( $item ) {
112
			return ! empty( $item );
113
		} );
114
115
		// Map the values to tuples.
116
		return array_map( function ( $item ) use ( $uri, $predicate, $data_type, $language ) {
117
118
			return sprintf( '<%s> <%s> %s . ',
119
				Wordlift_Sparql_Service::escape_uri( $uri ),
120
				Wordlift_Sparql_Service::escape_uri( $predicate ),
121
				Wordlift_Sparql_Service::format( $item, $data_type, $language )
122
			);
123
		}, $values );
124
	}
125
126
	/**
127
	 * Get the predicate for this {@link Wordlift_Sparql_Tuple_Rendition}.
128
	 *
129
	 * @since 3.15.0
130
	 *
131
	 * @return string The predicate.
132
	 */
133
	public function get_predicate() {
134
135
		return $this->predicate;
136
	}
137
138
}
139