Skip to content

Commit ebcda5f

Browse files
authored
Merge pull request #13 from freearhey/dev
Upgrade to v3.2.0
2 parents fe24263 + 770dcf0 commit ebcda5f

14 files changed

Lines changed: 345 additions & 154 deletions

README.md

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -144,44 +144,55 @@ $entity = $wikidata->get('Q19837');
144144
lang: "en"
145145
label: "Steve Jobs"
146146
aliases: array:2 [
147-
0 => "Steven Jobs"
147+
0 => "Steven Jobs",
148148
1 => "Steven Paul Jobs"
149149
]
150150
description: "American entrepreneur and co-founder of Apple Inc."
151-
properties: Collection {
152-
#items: array:98 [
153-
"P18" => Property {
154-
id: "P18"
155-
label: "image"
156-
value: "http://commons.wikimedia.org/wiki/Special:FilePath/Steve%20Jobs%20Headshot%202010-CROP2.jpg"
157-
}
158-
...
159-
]
160-
}
151+
properties: Collection { ... }
161152
}
162153
*/
163154

164155

165-
// List of all properties as array
156+
// List of all properties as an array
166157
$properties = $entity->properties->toArray();
167158

168159
/*
169160
[
170-
"P18" => Property {
171-
id: "P18"
172-
label: "image"
173-
value: "http://commons.wikimedia.org/wiki/Special:FilePath/Steve%20Jobs%20Headshot%202010-CROP2.jpg"
174-
},
175-
"P19" => Property {
176-
id: "P19"
177-
label: "place of birth"
178-
value: "San Francisco"
161+
"P1006" => Property {
162+
id: "P1006"
163+
label: "NTA ID"
164+
values: Collection {
165+
#items: array:6 [
166+
0 => Value {
167+
id: "Q5593916"
168+
label: "Grammy Trustees Award"
169+
qualifiers: Collection {
170+
#items: array:1 [
171+
0 => Qualifier {
172+
id: "P585"
173+
label: "point in time"
174+
value: "2012-01-01T00:00:00Z"
175+
}
176+
]
177+
}
178+
},
179+
...
180+
]
181+
}
179182
},
180183
...
181184
]
182185
*/
183186
```
184187

188+
### Upgrade Guide
189+
190+
#### Upgrade to 3.2.* from 3.1.*
191+
192+
The main changes in the new version have occurred in the way property values are stored. Now each property can have more than one value. In this regard, the attribute `value` in the `Property` object was replaced with the attribute `values`.
193+
194+
Also, values are now presented not as a string, but as an `Value` object. This allowed us to save along with each value not only its string representation but also a list of its `qualifiers`. Detailed information on what `qualifiers` is can be found [here](https://www.wikidata.org/wiki/Help:Qualifiers).
195+
185196
### Testing
186197

187198
```sh

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"keywords": ["wikidata", "client", "php"],
55
"homepage": "https://github.com/freearhey/wikidata",
66
"license": "MIT",
7-
"version": "3.1.0",
7+
"version": "3.2.0",
88
"authors": [
99
{
1010
"name": "Aleksandr Statciuk",
@@ -24,7 +24,7 @@
2424
},
2525
"autoload-dev": {
2626
"psr-4": {
27-
"Tests\\": "tests"
27+
"Wikidata\\Tests\\": "tests"
2828
},
2929
"files": ["src/helpers.php"]
3030
},

src/Entity.php

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Entity
1212
public $id;
1313

1414
/**
15-
* @var string Entity label
15+
* @var string Entity language
1616
*/
1717
public $lang;
1818

@@ -22,7 +22,7 @@ class Entity
2222
public $label;
2323

2424
/**
25-
* @var string[] Array of all entity aliases
25+
* @var string[] List of entity aliases
2626
*/
2727
public $aliases = [];
2828

@@ -32,44 +32,37 @@ class Entity
3232
public $description;
3333

3434
/**
35-
* @var string[] Array of all entity properties
35+
* @var \Illuminate\Support\Collection Collection of entity properties
3636
*/
37-
public $properties = [];
37+
public $properties;
3838

3939
/**
40-
* @param \Illuminate\Support\Collection $data
40+
* @param array $data
41+
* @param string $lang
4142
*/
4243
public function __construct($data, $lang)
4344
{
44-
$data = $this->formatData($data);
45-
46-
$this->id = $data['id'];
45+
$this->parseData($data);
4746
$this->lang = $lang;
48-
$this->label = $data['label'];
49-
$this->aliases = $data['aliases'];
50-
$this->description = $data['description'];
51-
$this->properties = $data['properties'];
5247
}
5348

54-
private function formatData($data)
55-
{
56-
$id = str_replace("http://www.wikidata.org/entity/", "", $data[0]['item']);
57-
$label = $data[0]['itemLabel'];
58-
$description = $data[0]['itemDescription'];
59-
$aliases = explode(', ', $data[0]['itemAltLabel']);
49+
/**
50+
* Parse input data
51+
*
52+
* @param array $data
53+
*/
54+
private function parseData($data)
55+
{
56+
$this->id = get_id($data[0]['item']);
57+
$this->label = $data[0]['itemLabel'];
58+
$this->aliases = explode(', ', $data[0]['itemAltLabel']);
59+
$this->description = $data[0]['itemDescription'];
60+
61+
$collection = collect($data)->groupBy('prop');
62+
$this->properties = $collection->mapWithKeys(function($item) {
63+
$property = new Property($item);
6064

61-
$collection = collect($data);
62-
$properties = $collection->mapWithKeys(function($prop) {
63-
$property = new Property($prop);
6465
return [$property->id => $property];
6566
});
66-
67-
return [
68-
'id' => $id,
69-
'label' => $label,
70-
'description' => $description,
71-
'aliases' => $aliases,
72-
'properties' => $properties
73-
];
7467
}
75-
}
68+
}

src/Property.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Wikidata;
44

5+
use Wikidata\Value;
6+
57
class Property
68
{
79
/**
@@ -15,32 +17,32 @@ class Property
1517
public $label;
1618

1719
/**
18-
* @var string Property value
20+
* @var \Illuminate\Support\Collection Collection of property values
1921
*/
20-
public $value;
22+
public $values;
2123

2224
/**
23-
* @param \Illuminate\Support\Collection $data
25+
* @param array $data
2426
*/
25-
public function __construct($data)
27+
public function __construct($data)
2628
{
27-
$data = $this->formatData($data);
28-
29-
$this->id = $data['id'];
30-
$this->label = $data['label'];
31-
$this->value = $data['value'];
29+
$this->parseData($data);
3230
}
3331

34-
private function formatData($data)
32+
/**
33+
* Parse input data
34+
*
35+
* @param array $data
36+
*/
37+
private function parseData($data)
3538
{
36-
$id = str_replace("http://www.wikidata.org/entity/", "", $data['prop']);
37-
$label = $data['propLabel'];
38-
$value = $data['propValue'];
39-
40-
return [
41-
'id' => $id,
42-
'label' => $label,
43-
'value' => $value
44-
];
39+
$grouped = collect($data)->groupBy('statement');
40+
$flatten = $grouped->flatten(1);
41+
42+
$this->id = get_id($flatten[0]['prop']);
43+
$this->label = $flatten[0]['propertyLabel'];
44+
$this->values = $grouped->values()->map(function($v) {
45+
return new Value($v->toArray());
46+
});
4547
}
46-
}
48+
}

src/Qualifier.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Wikidata;
4+
5+
class Qualifier
6+
{
7+
/**
8+
* @var string Qualifier Id
9+
*/
10+
public $id;
11+
12+
/**
13+
* @var string Qualifier label
14+
*/
15+
public $label;
16+
17+
/**
18+
* @var string Qualifier value
19+
*/
20+
public $value;
21+
22+
/**
23+
* @param array $data
24+
*/
25+
public function __construct($data)
26+
{
27+
$this->parseData($data);
28+
}
29+
30+
/**
31+
* Parse input data
32+
*
33+
* @param array $data
34+
*/
35+
private function parseData($data)
36+
{
37+
$this->id = get_id($data['qualifier']);
38+
$this->label = $data['qualifierLabel'];
39+
$this->value = $data['qualifierValueLabel'];
40+
}
41+
}

src/Value.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Wikidata;
4+
5+
use Wikidata\Qualifier;
6+
7+
class Value
8+
{
9+
/**
10+
* @var string Value Id
11+
*/
12+
public $id;
13+
14+
/**
15+
* @var string Value label
16+
*/
17+
public $label;
18+
19+
/**
20+
* @var \Illuminate\Support\Collection Collection of value qualifiers
21+
*/
22+
public $qualifiers;
23+
24+
/**
25+
* @param array $data
26+
*/
27+
public function __construct($data)
28+
{
29+
$this->parseData($data);
30+
}
31+
32+
/**
33+
* Parse input data
34+
*
35+
* @param array $data
36+
*/
37+
private function parseData($data)
38+
{
39+
$this->id = get_id($data[0]['propertyValue']);
40+
$this->label = $data[0]['propertyValueLabel'];
41+
$this->qualifiers = collect($data)->map(function($item) {
42+
if($item['qualifier']) {
43+
return new Qualifier($item);
44+
}
45+
46+
return null;
47+
})->filter();
48+
}
49+
}

0 commit comments

Comments
 (0)