-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathEntity.php
More file actions
91 lines (77 loc) · 2.02 KB
/
Entity.php
File metadata and controls
91 lines (77 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Wikidata;
use Illuminate\Support\Collection;
use Wikidata\Property;
class Entity
{
/**
* @var string Entity Id
*/
public string $id;
/**
* @var string Entity label
*/
public string $label;
/**
* @var string A link to a Wikipedia article about this entity
*/
public ?string $wiki_url = null;
/**
* @var string[] List of entity aliases
*/
public array $aliases = [];
/**
* @var string|null Entity description
*/
public ?string $description=null;
public Collection $properties;
/**
* @param array $data
* @param string $lang
*/
public function __construct(array $data, public $lang)
{
$this->properties = new Collection();
$this->parseData($data);
}
/**
* Parse input data
*
* @param array $data
*/
private function parseData(array $data): void
{
$lang = $this->lang;
$site = $lang . 'wiki';
$this->id = $data['id'];
$this->label = isset($data['labels'][$lang]) ? $data['labels'][$lang]['value'] : null;
$this->description = isset($data['descriptions'][$lang]) ? $data['descriptions'][$lang]['value'] : null;
$this->wiki_url = isset($data['sitelinks'][$site]) ? $data['sitelinks'][$site]['url'] : null;
$this->aliases = isset($data['aliases'][$lang]) ? collect($data['aliases'][$lang])->pluck('value')->toArray() : [];
}
/**
* Parse entity properties from sparql result
*
* @param Collection $data
*/
public function parseProperties(array|Collection $data): void
{
$collection = new Collection($data)->groupBy('prop');
$this->properties = $collection->mapWithKeys(function ($item): array {
$property = new Property($item);
return [$property->id => $property];
});
}
public function toArray(): array
{
return [
'id' => $this->id,
'lang' => $this->lang,
'label' => $this->label,
'description' => $this->description,
'wiki_url' => $this->wiki_url,
'aliases' => $this->aliases,
'properties' => $this->properties,
];
}
}