Completed
Push — develop ( 8af5a6...eb4440 )
by David
03:01
created
src/wordlift/analysis/response/class-analysis-response-ops.php 2 patches
Indentation   +259 added lines, -259 removed lines patch added patch discarded remove patch
@@ -5,264 +5,264 @@
 block discarded – undo
5 5
 
6 6
 class Analysis_Response_Ops {
7 7
 
8
-	/**
9
-	 * The analysis response json.
10
-	 *
11
-	 * @since 3.21.5
12
-	 * @access private
13
-	 * @var mixed $json Holds the analysis response json.
14
-	 */
15
-	private $json;
16
-
17
-	/**
18
-	 * Holds the {@link Wordlift_Entity_Uri_Service}.
19
-	 *
20
-	 * @since 3.21.5
21
-	 * @access private
22
-	 * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
23
-	 */
24
-	private $entity_uri_service;
25
-
26
-	private $entity_service;
27
-
28
-	/**
29
-	 * @var \Wordlift_Entity_Type_Service
30
-	 */
31
-	private $entity_type_service;
32
-	/**
33
-	 * @var \Wordlift_Post_Image_Storage
34
-	 */
35
-	private $post_image_storage;
36
-
37
-	/**
38
-	 * Analysis_Response_Ops constructor.
39
-	 *
40
-	 * @param \Wordlift_Entity_Uri_Service  $entity_uri_service The {@link Wordlift_Entity_Uri_Service}.
41
-	 * @param \Wordlift_Entity_Service      $entity_service The {@link Wordlift_Entity_Service}.
42
-	 * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service}.
43
-	 * @param \Wordlift_Post_Image_Storage  $post_image_storage A {@link Wordlift_Post_Image_Storage} instance.
44
-	 * @param mixed                         $json The analysis response json.
45
-	 *
46
-	 * @since 3.21.5
47
-	 */
48
-	public function __construct( $entity_uri_service, $entity_service, $entity_type_service, $post_image_storage, $json ) {
49
-
50
-		$this->json                = $json;
51
-		$this->entity_uri_service  = $entity_uri_service;
52
-		$this->entity_service      = $entity_service;
53
-		$this->entity_type_service = $entity_type_service;
54
-		$this->post_image_storage  = $post_image_storage;
55
-
56
-	}
57
-
58
-	/**
59
-	 * Switches remote entities, i.e. entities with id outside the local dataset, to local entities.
60
-	 *
61
-	 * The function takes all the entities that have an id which is not local. For each remote entity, a list of URIs
62
-	 * is built comprising the entity id and the sameAs. Then a query is issued in the local database to find potential
63
-	 * matches from the local vocabulary.
64
-	 *
65
-	 * If found, the entity id is swapped with the local id and the remote id is added to the sameAs.
66
-	 *
67
-	 * @return Analysis_Response_Ops The current Analysis_Response_Ops instance.
68
-	 */
69
-	public function make_entities_local() {
70
-
71
-		if ( ! isset( $this->json->entities ) ) {
72
-			return $this;
73
-		}
74
-
75
-		// Get the URIs.
76
-		$uris = array_keys( get_object_vars( $this->json->entities ) );
77
-
78
-		// Filter only the external URIs.
79
-		$entity_uri_service = $this->entity_uri_service;
80
-		$external_uris      = array_filter( $uris, function ( $item ) use ( $entity_uri_service ) {
81
-			return ! $entity_uri_service->is_internal( $item );
82
-		} );
83
-
84
-		// Preload the URIs.
85
-		$entity_uri_service->preload_uris( $external_uris );
86
-
87
-		$mappings = array();
88
-		foreach ( $external_uris as $external_uri ) {
89
-			$entity = $entity_uri_service->get_entity( $external_uri );
90
-			if ( null !== $entity ) {
91
-
92
-				// Get the internal URI.
93
-				$internal_uri              = $this->entity_service->get_uri( $entity->ID );
94
-				$mappings[ $external_uri ] = $internal_uri;
95
-			}
96
-		}
97
-
98
-		foreach ( $mappings as $external_uri => $internal_uri ) {
99
-
100
-			// Move the data from the external URI to the internal URI.
101
-			if ( ! isset( $this->json->entities->{$internal_uri} ) ) {
102
-				$this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri};
103
-			}
104
-
105
-			// Ensure sameAs is an array.
106
-			if ( ! isset( $this->json->entities->{$internal_uri}->sameAs )
107
-			     || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) {
108
-				$this->json->entities->{$internal_uri}->sameAs = array();
109
-			}
110
-
111
-			// Add the external URI as sameAs.
112
-			$this->json->entities->{$internal_uri}->sameAs[] = $external_uri;
113
-
114
-			// Finally remove the external URI.
115
-			unset( $this->json->entities->{$external_uri} );
116
-		}
117
-
118
-		if ( isset( $this->json->annotations ) ) {
119
-			foreach ( $this->json->annotations as $key => $annotation ) {
120
-				if ( isset( $annotation->entityMatches ) ) {
121
-					foreach ( $annotation->entityMatches as $match ) {
122
-						if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) {
123
-							$match->entityId = $mappings[ $match->entityId ];
124
-						}
125
-					}
126
-				}
127
-			}
128
-		}
129
-
130
-		return $this;
131
-	}
132
-
133
-	public function add_occurrences( $content ) {
134
-
135
-		// Try to get all the disambiguated annotations and bail out if an error occurs.
136
-		if ( false === preg_match_all(
137
-				'|<span\s+id="([^"]+)"\s+class="textannotation\s+disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">|',
138
-				$content,
139
-				$matches,
140
-				PREG_SET_ORDER
141
-			) ) {
142
-			return $this;
143
-		}
144
-
145
-		// Get the annotations' ids indexed by entity ids.
146
-		$occurrences = array_reduce( $matches, function ( $carry, $item ) {
147
-			$annotation_id = $item[1];
148
-			$item_id       = $item[2];
149
-			if ( ! isset( $carry[ $item_id ] ) ) {
150
-				$carry[ $item_id ] = array();
151
-			}
152
-
153
-			$carry[ $item_id ][] = $annotation_id;
154
-
155
-			return $carry;
156
-		}, array() );
157
-
158
-		foreach ( array_keys( $occurrences ) as $id ) {
159
-
160
-			// If the entity isn't there, add it.
161
-			if ( ! isset( $this->json->entities->{$id} ) ) {
162
-				$entity = $this->get_local_entity( $id );
163
-
164
-				// Entity not found in the local vocabulary, continue to the next one.
165
-				if ( false === $entity ) {
166
-					continue;
167
-				}
168
-
169
-				$this->json->entities->{$id} = $entity;
170
-			}
171
-		}
172
-
173
-		// Here we're adding back some data structures required by the client-side code.
174
-		//
175
-		// We're adding:
176
-		//  1. the .entities[entity_id].occurrences array with the annotations' ids.
177
-		//  2. the .entities[entity_id].annotations[annotation_id] = { id: annotation_id } map.
178
-		//
179
-		// Before 3.23.0 this was done by the client-side code located in src/coffee/editpost-widget/app.services.AnalysisService.coffee
180
-		// function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in
181
-		// `embedAnalysis`.
182
-		foreach ( $this->json->entities as $id => $entity ) {
183
-			$this->json->entities->{$id}->occurrences = isset( $occurrences[ $id ] ) ? $occurrences[ $id ] : array();;
184
-
185
-			foreach ( $this->json->entities->{$id}->occurrences as $annotation_id ) {
186
-				$this->json->entities->{$id}->annotations[ $annotation_id ] = array(
187
-					'id' => $annotation_id,
188
-				);
189
-			}
190
-		}
191
-
192
-		return $this;
193
-	}
194
-
195
-	private function get_local_entity( $uri ) {
196
-
197
-		$entity = $this->entity_uri_service->get_entity( $uri );
198
-
199
-		if ( null === $entity ) {
200
-			return false;
201
-		}
202
-
203
-		$type   = $this->entity_type_service->get( $entity->ID );
204
-		$images = $this->post_image_storage->get( $entity->ID );
205
-
206
-		return (object) array(
207
-			'id'          => $uri,
208
-			'label'       => $entity->post_title,
209
-			'description' => $entity->post_content,
210
-			'sameAs'      => wl_schema_get_value( $entity->ID, 'sameAs' ),
211
-			'mainType'    => str_replace( 'wl-', '', $type['css_class'] ),
212
-			'types'       => wl_get_entity_rdf_types( $entity->ID ),
213
-			'images'      => $images,
214
-		);
215
-	}
216
-
217
-	/**
218
-	 * Get the string representation of the JSON.
219
-	 *
220
-	 * @return false|string The string representation or false in case of error.
221
-	 */
222
-	public function to_string() {
223
-
224
-		// Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+.
225
-		$options = ( version_compare( PHP_VERSION, '5.4', '>=' )
226
-			? 256 : 0 );
227
-
228
-		return wp_json_encode( $this->json, $options );
229
-	}
230
-
231
-	/**
232
-	 * Create an Analysis_Response_Ops instance given the provided JSON structure.
233
-	 *
234
-	 * @param mixed $json The JSON structure.
235
-	 *
236
-	 * @return Analysis_Response_Ops A new Analysis_Response_Ops instance.
237
-	 */
238
-	public static function create( $json ) {
239
-
240
-		return new static(
241
-			\Wordlift_Entity_Uri_Service::get_instance(),
242
-			\Wordlift_Entity_Service::get_instance(),
243
-			\Wordlift_Entity_Type_Service::get_instance(),
244
-			\Wordlift_Storage_Factory::get_instance()->post_images(),
245
-			$json );
246
-	}
247
-
248
-	/**
249
-	 * Create an Analysis_Response_Ops instance given the provided http response.
250
-	 *
251
-	 * @param array $response {
252
-	 *
253
-	 * @type string $body The response body.
254
-	 * }
255
-	 *
256
-	 * @return Analysis_Response_Ops A new Analysis_Response_Ops instance.
257
-	 * @throws \Exception if the provided response doesn't contain a `body` element.
258
-	 */
259
-	public static function create_with_response( $response ) {
260
-
261
-		if ( ! isset( $response['body'] ) ) {
262
-			throw new \Exception( "`body` is required in response." );
263
-		}
264
-
265
-		return static::create( json_decode( $response['body'] ) );
266
-	}
8
+    /**
9
+     * The analysis response json.
10
+     *
11
+     * @since 3.21.5
12
+     * @access private
13
+     * @var mixed $json Holds the analysis response json.
14
+     */
15
+    private $json;
16
+
17
+    /**
18
+     * Holds the {@link Wordlift_Entity_Uri_Service}.
19
+     *
20
+     * @since 3.21.5
21
+     * @access private
22
+     * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance.
23
+     */
24
+    private $entity_uri_service;
25
+
26
+    private $entity_service;
27
+
28
+    /**
29
+     * @var \Wordlift_Entity_Type_Service
30
+     */
31
+    private $entity_type_service;
32
+    /**
33
+     * @var \Wordlift_Post_Image_Storage
34
+     */
35
+    private $post_image_storage;
36
+
37
+    /**
38
+     * Analysis_Response_Ops constructor.
39
+     *
40
+     * @param \Wordlift_Entity_Uri_Service  $entity_uri_service The {@link Wordlift_Entity_Uri_Service}.
41
+     * @param \Wordlift_Entity_Service      $entity_service The {@link Wordlift_Entity_Service}.
42
+     * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service}.
43
+     * @param \Wordlift_Post_Image_Storage  $post_image_storage A {@link Wordlift_Post_Image_Storage} instance.
44
+     * @param mixed                         $json The analysis response json.
45
+     *
46
+     * @since 3.21.5
47
+     */
48
+    public function __construct( $entity_uri_service, $entity_service, $entity_type_service, $post_image_storage, $json ) {
49
+
50
+        $this->json                = $json;
51
+        $this->entity_uri_service  = $entity_uri_service;
52
+        $this->entity_service      = $entity_service;
53
+        $this->entity_type_service = $entity_type_service;
54
+        $this->post_image_storage  = $post_image_storage;
55
+
56
+    }
57
+
58
+    /**
59
+     * Switches remote entities, i.e. entities with id outside the local dataset, to local entities.
60
+     *
61
+     * The function takes all the entities that have an id which is not local. For each remote entity, a list of URIs
62
+     * is built comprising the entity id and the sameAs. Then a query is issued in the local database to find potential
63
+     * matches from the local vocabulary.
64
+     *
65
+     * If found, the entity id is swapped with the local id and the remote id is added to the sameAs.
66
+     *
67
+     * @return Analysis_Response_Ops The current Analysis_Response_Ops instance.
68
+     */
69
+    public function make_entities_local() {
70
+
71
+        if ( ! isset( $this->json->entities ) ) {
72
+            return $this;
73
+        }
74
+
75
+        // Get the URIs.
76
+        $uris = array_keys( get_object_vars( $this->json->entities ) );
77
+
78
+        // Filter only the external URIs.
79
+        $entity_uri_service = $this->entity_uri_service;
80
+        $external_uris      = array_filter( $uris, function ( $item ) use ( $entity_uri_service ) {
81
+            return ! $entity_uri_service->is_internal( $item );
82
+        } );
83
+
84
+        // Preload the URIs.
85
+        $entity_uri_service->preload_uris( $external_uris );
86
+
87
+        $mappings = array();
88
+        foreach ( $external_uris as $external_uri ) {
89
+            $entity = $entity_uri_service->get_entity( $external_uri );
90
+            if ( null !== $entity ) {
91
+
92
+                // Get the internal URI.
93
+                $internal_uri              = $this->entity_service->get_uri( $entity->ID );
94
+                $mappings[ $external_uri ] = $internal_uri;
95
+            }
96
+        }
97
+
98
+        foreach ( $mappings as $external_uri => $internal_uri ) {
99
+
100
+            // Move the data from the external URI to the internal URI.
101
+            if ( ! isset( $this->json->entities->{$internal_uri} ) ) {
102
+                $this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri};
103
+            }
104
+
105
+            // Ensure sameAs is an array.
106
+            if ( ! isset( $this->json->entities->{$internal_uri}->sameAs )
107
+                 || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) {
108
+                $this->json->entities->{$internal_uri}->sameAs = array();
109
+            }
110
+
111
+            // Add the external URI as sameAs.
112
+            $this->json->entities->{$internal_uri}->sameAs[] = $external_uri;
113
+
114
+            // Finally remove the external URI.
115
+            unset( $this->json->entities->{$external_uri} );
116
+        }
117
+
118
+        if ( isset( $this->json->annotations ) ) {
119
+            foreach ( $this->json->annotations as $key => $annotation ) {
120
+                if ( isset( $annotation->entityMatches ) ) {
121
+                    foreach ( $annotation->entityMatches as $match ) {
122
+                        if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) {
123
+                            $match->entityId = $mappings[ $match->entityId ];
124
+                        }
125
+                    }
126
+                }
127
+            }
128
+        }
129
+
130
+        return $this;
131
+    }
132
+
133
+    public function add_occurrences( $content ) {
134
+
135
+        // Try to get all the disambiguated annotations and bail out if an error occurs.
136
+        if ( false === preg_match_all(
137
+                '|<span\s+id="([^"]+)"\s+class="textannotation\s+disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">|',
138
+                $content,
139
+                $matches,
140
+                PREG_SET_ORDER
141
+            ) ) {
142
+            return $this;
143
+        }
144
+
145
+        // Get the annotations' ids indexed by entity ids.
146
+        $occurrences = array_reduce( $matches, function ( $carry, $item ) {
147
+            $annotation_id = $item[1];
148
+            $item_id       = $item[2];
149
+            if ( ! isset( $carry[ $item_id ] ) ) {
150
+                $carry[ $item_id ] = array();
151
+            }
152
+
153
+            $carry[ $item_id ][] = $annotation_id;
154
+
155
+            return $carry;
156
+        }, array() );
157
+
158
+        foreach ( array_keys( $occurrences ) as $id ) {
159
+
160
+            // If the entity isn't there, add it.
161
+            if ( ! isset( $this->json->entities->{$id} ) ) {
162
+                $entity = $this->get_local_entity( $id );
163
+
164
+                // Entity not found in the local vocabulary, continue to the next one.
165
+                if ( false === $entity ) {
166
+                    continue;
167
+                }
168
+
169
+                $this->json->entities->{$id} = $entity;
170
+            }
171
+        }
172
+
173
+        // Here we're adding back some data structures required by the client-side code.
174
+        //
175
+        // We're adding:
176
+        //  1. the .entities[entity_id].occurrences array with the annotations' ids.
177
+        //  2. the .entities[entity_id].annotations[annotation_id] = { id: annotation_id } map.
178
+        //
179
+        // Before 3.23.0 this was done by the client-side code located in src/coffee/editpost-widget/app.services.AnalysisService.coffee
180
+        // function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in
181
+        // `embedAnalysis`.
182
+        foreach ( $this->json->entities as $id => $entity ) {
183
+            $this->json->entities->{$id}->occurrences = isset( $occurrences[ $id ] ) ? $occurrences[ $id ] : array();;
184
+
185
+            foreach ( $this->json->entities->{$id}->occurrences as $annotation_id ) {
186
+                $this->json->entities->{$id}->annotations[ $annotation_id ] = array(
187
+                    'id' => $annotation_id,
188
+                );
189
+            }
190
+        }
191
+
192
+        return $this;
193
+    }
194
+
195
+    private function get_local_entity( $uri ) {
196
+
197
+        $entity = $this->entity_uri_service->get_entity( $uri );
198
+
199
+        if ( null === $entity ) {
200
+            return false;
201
+        }
202
+
203
+        $type   = $this->entity_type_service->get( $entity->ID );
204
+        $images = $this->post_image_storage->get( $entity->ID );
205
+
206
+        return (object) array(
207
+            'id'          => $uri,
208
+            'label'       => $entity->post_title,
209
+            'description' => $entity->post_content,
210
+            'sameAs'      => wl_schema_get_value( $entity->ID, 'sameAs' ),
211
+            'mainType'    => str_replace( 'wl-', '', $type['css_class'] ),
212
+            'types'       => wl_get_entity_rdf_types( $entity->ID ),
213
+            'images'      => $images,
214
+        );
215
+    }
216
+
217
+    /**
218
+     * Get the string representation of the JSON.
219
+     *
220
+     * @return false|string The string representation or false in case of error.
221
+     */
222
+    public function to_string() {
223
+
224
+        // Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+.
225
+        $options = ( version_compare( PHP_VERSION, '5.4', '>=' )
226
+            ? 256 : 0 );
227
+
228
+        return wp_json_encode( $this->json, $options );
229
+    }
230
+
231
+    /**
232
+     * Create an Analysis_Response_Ops instance given the provided JSON structure.
233
+     *
234
+     * @param mixed $json The JSON structure.
235
+     *
236
+     * @return Analysis_Response_Ops A new Analysis_Response_Ops instance.
237
+     */
238
+    public static function create( $json ) {
239
+
240
+        return new static(
241
+            \Wordlift_Entity_Uri_Service::get_instance(),
242
+            \Wordlift_Entity_Service::get_instance(),
243
+            \Wordlift_Entity_Type_Service::get_instance(),
244
+            \Wordlift_Storage_Factory::get_instance()->post_images(),
245
+            $json );
246
+    }
247
+
248
+    /**
249
+     * Create an Analysis_Response_Ops instance given the provided http response.
250
+     *
251
+     * @param array $response {
252
+     *
253
+     * @type string $body The response body.
254
+     * }
255
+     *
256
+     * @return Analysis_Response_Ops A new Analysis_Response_Ops instance.
257
+     * @throws \Exception if the provided response doesn't contain a `body` element.
258
+     */
259
+    public static function create_with_response( $response ) {
260
+
261
+        if ( ! isset( $response['body'] ) ) {
262
+            throw new \Exception( "`body` is required in response." );
263
+        }
264
+
265
+        return static::create( json_decode( $response['body'] ) );
266
+    }
267 267
 
268 268
 }
Please login to merge, or discard this patch.
Spacing   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
 	 *
46 46
 	 * @since 3.21.5
47 47
 	 */
48
-	public function __construct( $entity_uri_service, $entity_service, $entity_type_service, $post_image_storage, $json ) {
48
+	public function __construct($entity_uri_service, $entity_service, $entity_type_service, $post_image_storage, $json) {
49 49
 
50 50
 		$this->json                = $json;
51 51
 		$this->entity_uri_service  = $entity_uri_service;
@@ -68,43 +68,43 @@  discard block
 block discarded – undo
68 68
 	 */
69 69
 	public function make_entities_local() {
70 70
 
71
-		if ( ! isset( $this->json->entities ) ) {
71
+		if ( ! isset($this->json->entities)) {
72 72
 			return $this;
73 73
 		}
74 74
 
75 75
 		// Get the URIs.
76
-		$uris = array_keys( get_object_vars( $this->json->entities ) );
76
+		$uris = array_keys(get_object_vars($this->json->entities));
77 77
 
78 78
 		// Filter only the external URIs.
79 79
 		$entity_uri_service = $this->entity_uri_service;
80
-		$external_uris      = array_filter( $uris, function ( $item ) use ( $entity_uri_service ) {
81
-			return ! $entity_uri_service->is_internal( $item );
80
+		$external_uris      = array_filter($uris, function($item) use ($entity_uri_service) {
81
+			return ! $entity_uri_service->is_internal($item);
82 82
 		} );
83 83
 
84 84
 		// Preload the URIs.
85
-		$entity_uri_service->preload_uris( $external_uris );
85
+		$entity_uri_service->preload_uris($external_uris);
86 86
 
87 87
 		$mappings = array();
88
-		foreach ( $external_uris as $external_uri ) {
89
-			$entity = $entity_uri_service->get_entity( $external_uri );
90
-			if ( null !== $entity ) {
88
+		foreach ($external_uris as $external_uri) {
89
+			$entity = $entity_uri_service->get_entity($external_uri);
90
+			if (null !== $entity) {
91 91
 
92 92
 				// Get the internal URI.
93
-				$internal_uri              = $this->entity_service->get_uri( $entity->ID );
94
-				$mappings[ $external_uri ] = $internal_uri;
93
+				$internal_uri              = $this->entity_service->get_uri($entity->ID);
94
+				$mappings[$external_uri] = $internal_uri;
95 95
 			}
96 96
 		}
97 97
 
98
-		foreach ( $mappings as $external_uri => $internal_uri ) {
98
+		foreach ($mappings as $external_uri => $internal_uri) {
99 99
 
100 100
 			// Move the data from the external URI to the internal URI.
101
-			if ( ! isset( $this->json->entities->{$internal_uri} ) ) {
101
+			if ( ! isset($this->json->entities->{$internal_uri} )) {
102 102
 				$this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri};
103 103
 			}
104 104
 
105 105
 			// Ensure sameAs is an array.
106
-			if ( ! isset( $this->json->entities->{$internal_uri}->sameAs )
107
-			     || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) {
106
+			if ( ! isset($this->json->entities->{$internal_uri}->sameAs)
107
+			     || ! is_array($this->json->entities->{$internal_uri}->sameAs)) {
108 108
 				$this->json->entities->{$internal_uri}->sameAs = array();
109 109
 			}
110 110
 
@@ -112,15 +112,15 @@  discard block
 block discarded – undo
112 112
 			$this->json->entities->{$internal_uri}->sameAs[] = $external_uri;
113 113
 
114 114
 			// Finally remove the external URI.
115
-			unset( $this->json->entities->{$external_uri} );
115
+			unset($this->json->entities->{$external_uri} );
116 116
 		}
117 117
 
118
-		if ( isset( $this->json->annotations ) ) {
119
-			foreach ( $this->json->annotations as $key => $annotation ) {
120
-				if ( isset( $annotation->entityMatches ) ) {
121
-					foreach ( $annotation->entityMatches as $match ) {
122
-						if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) {
123
-							$match->entityId = $mappings[ $match->entityId ];
118
+		if (isset($this->json->annotations)) {
119
+			foreach ($this->json->annotations as $key => $annotation) {
120
+				if (isset($annotation->entityMatches)) {
121
+					foreach ($annotation->entityMatches as $match) {
122
+						if (isset($match->entityId) && isset($mappings[$match->entityId])) {
123
+							$match->entityId = $mappings[$match->entityId];
124 124
 						}
125 125
 					}
126 126
 				}
@@ -130,39 +130,39 @@  discard block
 block discarded – undo
130 130
 		return $this;
131 131
 	}
132 132
 
133
-	public function add_occurrences( $content ) {
133
+	public function add_occurrences($content) {
134 134
 
135 135
 		// Try to get all the disambiguated annotations and bail out if an error occurs.
136
-		if ( false === preg_match_all(
136
+		if (false === preg_match_all(
137 137
 				'|<span\s+id="([^"]+)"\s+class="textannotation\s+disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">|',
138 138
 				$content,
139 139
 				$matches,
140 140
 				PREG_SET_ORDER
141
-			) ) {
141
+			)) {
142 142
 			return $this;
143 143
 		}
144 144
 
145 145
 		// Get the annotations' ids indexed by entity ids.
146
-		$occurrences = array_reduce( $matches, function ( $carry, $item ) {
146
+		$occurrences = array_reduce($matches, function($carry, $item) {
147 147
 			$annotation_id = $item[1];
148 148
 			$item_id       = $item[2];
149
-			if ( ! isset( $carry[ $item_id ] ) ) {
150
-				$carry[ $item_id ] = array();
149
+			if ( ! isset($carry[$item_id])) {
150
+				$carry[$item_id] = array();
151 151
 			}
152 152
 
153
-			$carry[ $item_id ][] = $annotation_id;
153
+			$carry[$item_id][] = $annotation_id;
154 154
 
155 155
 			return $carry;
156
-		}, array() );
156
+		}, array());
157 157
 
158
-		foreach ( array_keys( $occurrences ) as $id ) {
158
+		foreach (array_keys($occurrences) as $id) {
159 159
 
160 160
 			// If the entity isn't there, add it.
161
-			if ( ! isset( $this->json->entities->{$id} ) ) {
162
-				$entity = $this->get_local_entity( $id );
161
+			if ( ! isset($this->json->entities->{$id} )) {
162
+				$entity = $this->get_local_entity($id);
163 163
 
164 164
 				// Entity not found in the local vocabulary, continue to the next one.
165
-				if ( false === $entity ) {
165
+				if (false === $entity) {
166 166
 					continue;
167 167
 				}
168 168
 
@@ -179,11 +179,11 @@  discard block
 block discarded – undo
179 179
 		// Before 3.23.0 this was done by the client-side code located in src/coffee/editpost-widget/app.services.AnalysisService.coffee
180 180
 		// function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in
181 181
 		// `embedAnalysis`.
182
-		foreach ( $this->json->entities as $id => $entity ) {
183
-			$this->json->entities->{$id}->occurrences = isset( $occurrences[ $id ] ) ? $occurrences[ $id ] : array();;
182
+		foreach ($this->json->entities as $id => $entity) {
183
+			$this->json->entities->{$id}->occurrences = isset($occurrences[$id]) ? $occurrences[$id] : array(); ;
184 184
 
185
-			foreach ( $this->json->entities->{$id}->occurrences as $annotation_id ) {
186
-				$this->json->entities->{$id}->annotations[ $annotation_id ] = array(
185
+			foreach ($this->json->entities->{$id}->occurrences as $annotation_id) {
186
+				$this->json->entities->{$id}->annotations[$annotation_id] = array(
187 187
 					'id' => $annotation_id,
188 188
 				);
189 189
 			}
@@ -192,24 +192,24 @@  discard block
 block discarded – undo
192 192
 		return $this;
193 193
 	}
194 194
 
195
-	private function get_local_entity( $uri ) {
195
+	private function get_local_entity($uri) {
196 196
 
197
-		$entity = $this->entity_uri_service->get_entity( $uri );
197
+		$entity = $this->entity_uri_service->get_entity($uri);
198 198
 
199
-		if ( null === $entity ) {
199
+		if (null === $entity) {
200 200
 			return false;
201 201
 		}
202 202
 
203
-		$type   = $this->entity_type_service->get( $entity->ID );
204
-		$images = $this->post_image_storage->get( $entity->ID );
203
+		$type   = $this->entity_type_service->get($entity->ID);
204
+		$images = $this->post_image_storage->get($entity->ID);
205 205
 
206 206
 		return (object) array(
207 207
 			'id'          => $uri,
208 208
 			'label'       => $entity->post_title,
209 209
 			'description' => $entity->post_content,
210
-			'sameAs'      => wl_schema_get_value( $entity->ID, 'sameAs' ),
211
-			'mainType'    => str_replace( 'wl-', '', $type['css_class'] ),
212
-			'types'       => wl_get_entity_rdf_types( $entity->ID ),
210
+			'sameAs'      => wl_schema_get_value($entity->ID, 'sameAs'),
211
+			'mainType'    => str_replace('wl-', '', $type['css_class']),
212
+			'types'       => wl_get_entity_rdf_types($entity->ID),
213 213
 			'images'      => $images,
214 214
 		);
215 215
 	}
@@ -222,10 +222,10 @@  discard block
 block discarded – undo
222 222
 	public function to_string() {
223 223
 
224 224
 		// Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+.
225
-		$options = ( version_compare( PHP_VERSION, '5.4', '>=' )
226
-			? 256 : 0 );
225
+		$options = (version_compare(PHP_VERSION, '5.4', '>=')
226
+			? 256 : 0);
227 227
 
228
-		return wp_json_encode( $this->json, $options );
228
+		return wp_json_encode($this->json, $options);
229 229
 	}
230 230
 
231 231
 	/**
@@ -235,7 +235,7 @@  discard block
 block discarded – undo
235 235
 	 *
236 236
 	 * @return Analysis_Response_Ops A new Analysis_Response_Ops instance.
237 237
 	 */
238
-	public static function create( $json ) {
238
+	public static function create($json) {
239 239
 
240 240
 		return new static(
241 241
 			\Wordlift_Entity_Uri_Service::get_instance(),
@@ -256,13 +256,13 @@  discard block
 block discarded – undo
256 256
 	 * @return Analysis_Response_Ops A new Analysis_Response_Ops instance.
257 257
 	 * @throws \Exception if the provided response doesn't contain a `body` element.
258 258
 	 */
259
-	public static function create_with_response( $response ) {
259
+	public static function create_with_response($response) {
260 260
 
261
-		if ( ! isset( $response['body'] ) ) {
262
-			throw new \Exception( "`body` is required in response." );
261
+		if ( ! isset($response['body'])) {
262
+			throw new \Exception("`body` is required in response.");
263 263
 		}
264 264
 
265
-		return static::create( json_decode( $response['body'] ) );
265
+		return static::create(json_decode($response['body']));
266 266
 	}
267 267
 
268 268
 }
Please login to merge, or discard this patch.