Skip to content

Commit

Permalink
Merge pull request #13 from freearhey/dev
Browse files Browse the repository at this point in the history
Upgrade to v3.2.0
  • Loading branch information
freearhey authored Sep 13, 2019
2 parents fe24263 + 770dcf0 commit ebcda5f
Show file tree
Hide file tree
Showing 14 changed files with 345 additions and 154 deletions.
53 changes: 32 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,44 +144,55 @@ $entity = $wikidata->get('Q19837');
lang: "en"
label: "Steve Jobs"
aliases: array:2 [
0 => "Steven Jobs"
0 => "Steven Jobs",
1 => "Steven Paul Jobs"
]
description: "American entrepreneur and co-founder of Apple Inc."
properties: Collection {
#items: array:98 [
"P18" => Property {
id: "P18"
label: "image"
value: "http://commons.wikimedia.org/wiki/Special:FilePath/Steve%20Jobs%20Headshot%202010-CROP2.jpg"
}
...
]
}
properties: Collection { ... }
}
*/


// List of all properties as array
// List of all properties as an array
$properties = $entity->properties->toArray();

/*
[
"P18" => Property {
id: "P18"
label: "image"
value: "http://commons.wikimedia.org/wiki/Special:FilePath/Steve%20Jobs%20Headshot%202010-CROP2.jpg"
},
"P19" => Property {
id: "P19"
label: "place of birth"
value: "San Francisco"
"P1006" => Property {
id: "P1006"
label: "NTA ID"
values: Collection {
#items: array:6 [
0 => Value {
id: "Q5593916"
label: "Grammy Trustees Award"
qualifiers: Collection {
#items: array:1 [
0 => Qualifier {
id: "P585"
label: "point in time"
value: "2012-01-01T00:00:00Z"
}
]
}
},
...
]
}
},
...
]
*/
```

### Upgrade Guide

#### Upgrade to 3.2.* from 3.1.*

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`.

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).

### Testing

```sh
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["wikidata", "client", "php"],
"homepage": "https://github.com/freearhey/wikidata",
"license": "MIT",
"version": "3.1.0",
"version": "3.2.0",
"authors": [
{
"name": "Aleksandr Statciuk",
Expand All @@ -24,7 +24,7 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
"Wikidata\\Tests\\": "tests"
},
"files": ["src/helpers.php"]
},
Expand Down
53 changes: 23 additions & 30 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Entity
public $id;

/**
* @var string Entity label
* @var string Entity language
*/
public $lang;

Expand All @@ -22,7 +22,7 @@ class Entity
public $label;

/**
* @var string[] Array of all entity aliases
* @var string[] List of entity aliases
*/
public $aliases = [];

Expand All @@ -32,44 +32,37 @@ class Entity
public $description;

/**
* @var string[] Array of all entity properties
* @var \Illuminate\Support\Collection Collection of entity properties
*/
public $properties = [];
public $properties;

/**
* @param \Illuminate\Support\Collection $data
* @param array $data
* @param string $lang
*/
public function __construct($data, $lang)
{
$data = $this->formatData($data);

$this->id = $data['id'];
$this->parseData($data);
$this->lang = $lang;
$this->label = $data['label'];
$this->aliases = $data['aliases'];
$this->description = $data['description'];
$this->properties = $data['properties'];
}

private function formatData($data)
{
$id = str_replace("http://www.wikidata.org/entity/", "", $data[0]['item']);
$label = $data[0]['itemLabel'];
$description = $data[0]['itemDescription'];
$aliases = explode(', ', $data[0]['itemAltLabel']);
/**
* Parse input data
*
* @param array $data
*/
private function parseData($data)
{
$this->id = get_id($data[0]['item']);
$this->label = $data[0]['itemLabel'];
$this->aliases = explode(', ', $data[0]['itemAltLabel']);
$this->description = $data[0]['itemDescription'];

$collection = collect($data)->groupBy('prop');
$this->properties = $collection->mapWithKeys(function($item) {
$property = new Property($item);

$collection = collect($data);
$properties = $collection->mapWithKeys(function($prop) {
$property = new Property($prop);
return [$property->id => $property];
});

return [
'id' => $id,
'label' => $label,
'description' => $description,
'aliases' => $aliases,
'properties' => $properties
];
}
}
}
42 changes: 22 additions & 20 deletions src/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Wikidata;

use Wikidata\Value;

class Property
{
/**
Expand All @@ -15,32 +17,32 @@ class Property
public $label;

/**
* @var string Property value
* @var \Illuminate\Support\Collection Collection of property values
*/
public $value;
public $values;

/**
* @param \Illuminate\Support\Collection $data
* @param array $data
*/
public function __construct($data)
public function __construct($data)
{
$data = $this->formatData($data);

$this->id = $data['id'];
$this->label = $data['label'];
$this->value = $data['value'];
$this->parseData($data);
}

private function formatData($data)
/**
* Parse input data
*
* @param array $data
*/
private function parseData($data)
{
$id = str_replace("http://www.wikidata.org/entity/", "", $data['prop']);
$label = $data['propLabel'];
$value = $data['propValue'];

return [
'id' => $id,
'label' => $label,
'value' => $value
];
$grouped = collect($data)->groupBy('statement');
$flatten = $grouped->flatten(1);

$this->id = get_id($flatten[0]['prop']);
$this->label = $flatten[0]['propertyLabel'];
$this->values = $grouped->values()->map(function($v) {
return new Value($v->toArray());
});
}
}
}
41 changes: 41 additions & 0 deletions src/Qualifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Wikidata;

class Qualifier
{
/**
* @var string Qualifier Id
*/
public $id;

/**
* @var string Qualifier label
*/
public $label;

/**
* @var string Qualifier value
*/
public $value;

/**
* @param array $data
*/
public function __construct($data)
{
$this->parseData($data);
}

/**
* Parse input data
*
* @param array $data
*/
private function parseData($data)
{
$this->id = get_id($data['qualifier']);
$this->label = $data['qualifierLabel'];
$this->value = $data['qualifierValueLabel'];
}
}
49 changes: 49 additions & 0 deletions src/Value.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Wikidata;

use Wikidata\Qualifier;

class Value
{
/**
* @var string Value Id
*/
public $id;

/**
* @var string Value label
*/
public $label;

/**
* @var \Illuminate\Support\Collection Collection of value qualifiers
*/
public $qualifiers;

/**
* @param array $data
*/
public function __construct($data)
{
$this->parseData($data);
}

/**
* Parse input data
*
* @param array $data
*/
private function parseData($data)
{
$this->id = get_id($data[0]['propertyValue']);
$this->label = $data[0]['propertyValueLabel'];
$this->qualifiers = collect($data)->map(function($item) {
if($item['qualifier']) {
return new Qualifier($item);
}

return null;
})->filter();
}
}
Loading

0 comments on commit ebcda5f

Please sign in to comment.