Completed
Push — master ( 228e92...6a576a )
by David
02:49
created

Wordlift_Entity_Property_Service::get()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 1
nop 2
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Process references to other entities, local or remote, by returning a
5
 * {@link Wordlift_Property_Entity_Reference} with the URL of the referenced entity.
6
 *
7
 * @since 3.8.0
8
 */
9
class Wordlift_Entity_Property_Service extends Wordlift_Simple_Property_Service {
10
	/**
11
	 * @var \Wordlift_Entity_Service $entity_service
12
	 */
13
	private $entity_service;
14
15
	/**
16
	 * Wordlift_Entity_Property_Service constructor.
17
	 *
18
	 * @param \Wordlift_Entity_Service $entity_service
19
	 */
20
	public function __construct( $entity_service ) {
21
22
		$this->entity_service = $entity_service;
23
24
	}
25
26
	/**
27
	 * {@inheritdoc}
28
	 */
29
	public function get( $post_id, $meta_key ) {
30
31
		$entity_service = $this->entity_service;
32
33
		// Map each returned value to a Wordlift_Property_Entity_Reference.
34
		return array_map( function ( $item ) use ( $entity_service ) {
35
36
			// If the $item is a number and it's an existing post, return the
37
			// URI of the referenced entity. Otherwise return the value.
38
			return is_numeric( $item ) && NULL !== get_post( $item )
39
				? new Wordlift_Property_Entity_Reference( $entity_service->get_uri( $item ) )
40
				: $item;
41
		}, get_post_meta( $post_id, $meta_key ) );
42
	}
43
44
}
45