diff --git "a/class-wb\\Claim.php" "b/class-wb\\Claim.php" index bd5326a..4264f36 100644 --- "a/class-wb\\Claim.php" +++ "b/class-wb\\Claim.php" @@ -1,62 +1,73 @@ . # Wikibase namespace wb; /** * A Claim consists of a Snak and Qualifiers. * * Optionally, it can have qualifiers. * * @see https://www.wikidata.org/wiki/Wikidata:Glossary#Claim */ class Claim { var $mainsnak; //var $qualifiers; /** * @param $property string Property as 'P123' * @param $mainsnak Snak Main snak */ public function __construct( $mainsnak ) { $this->setMainsnak( $mainsnak ); } public function getMainsnak() { return $this->mainsnak; } public function hasQualifiers() { return ! empty( $this->qualifiers ); } public function getQualifiers() { return $this->qualifiers; } public function setMainsnak( Snak $mainsnak ) { $this->mainsnak = $mainsnak; return $this; } public function setQualifiers( $qualifiers ) { $this->qualifiers = $qualifiers; return $this; } + + public static function createFromData( $data ) { + if( ! isset( $data['mainsnak'] ) ) { + throw new WrongDataException( __CLASS__ ); + } + $claim = new static( Snak::createFromData( $data['mainsnak'] ) ); + if( isset( $data['qualifiers'] ) ) { + $claim->setQualifiers( $qualifiers ); + } + return $claim; + } } diff --git "a/class-wb\\Claims.php" "b/class-wb\\Claims.php" index dccad7e..72a5e90 100644 --- "a/class-wb\\Claims.php" +++ "b/class-wb\\Claims.php" @@ -1,59 +1,63 @@ . # Wikibase namespace wb; /** * A Claim collector. */ class Claims { var $claims = []; public function __construct( $claims = [] ) { - $this->set( $claims ); + foreach( $claims as $claim ) { + $this->add( $claim ); + } } public function add( Claim $claim ) { $this->claims[] = $claim; return $this; } public function count() { return count( $this->claims ); } - public function set( $claims ) { - foreach( $claims as $claim ) { - $this->add( $claim ); + public function haveProperty( $property ) { + foreach( $this->claims as $claim ) { + if( $claim->getMainSnak()->getProperty() === $property ) { + return true; + } } - return $this; + return false; } - public function get() { + public function getAll() { $properties = []; foreach( $this->claims as $claim ) { - $property = $claim->getProperty(); + $property = $claim->getMainSnak()->getProperty(); if( ! isset( $properties[ $property ] ) ) { $properties[ $property ] = []; } $properties[ $property ][] = $claim; } return $properties; } } diff --git "a/class-wb\\DataModel.php" "b/class-wb\\DataModel.php" index 99033e2..03098ea 100644 --- "a/class-wb\\DataModel.php" +++ "b/class-wb\\DataModel.php" @@ -1,112 +1,112 @@ . # Wikibase namespace wb; class DataModel { private $labels; private $descriptions; private $claims; public function __construct() { - $this->labels = []; - $this->descriptions = []; - $this->claims = new Claims(); + $this->labels = new Labels(); + $this->descriptions = new Descriptions(); + $this->claims = new Claims(); } public function getLabels() { - return $this->labels; + return $this->labels->getAll(); } public function getDescriptions() { - return $this->descriptions; + return $this->descriptions->getAll(); } public function getClaims() { - return $this->claims->get(); + return $this->claims->getAll(); } public function addClaim( $claim ) { $this->claims->add( $claim ); return $this; } public function setClaims( $claims ) { $this->claims->set( $claims ); return $this; } + public function hasClaimsInProperty( $property ) { + return $this->claims->haveProperty( $property ); + } + + public function countClaims() { + return $this->claims->count(); + } + /** * Set, delete, preserve if it exists, a label. */ - public function setLabel( $language, $value, $action = 'only-if-missing' ) { - $label = new LabelAction( $language, $value ); - switch( $action ) { - case 'only-if-missing': - $label->pleasePreserve(); - break; - case 'delete': - $label->pleaseDelete(); - break; - case 'overwrite': - break; - default: - throw new Exception(); - } - $this->labels[] = $label; + public function setLabel( $label ) { + $this->labels->set( $label ); return $this; } /** * Set, delete, preserve if it exists, a description. */ - public function setDescription( $language, $value, $action = 'only-if-missing' ) { - $description = new DescriptionAction( $language, $value ); - switch( $action ) { - case 'only-if-missing': - $description->pleasePreserve(); - break; - case 'delete': - $description->pleaseDelete(); - break; - case 'overwrite': - break; - default: - throw new Exception(); - } - $this->descriptions[] = $description; + public function setDescription( $description ) { + $this->descriptions->set( $description ); return $this; } - public function countClaims() { - return $this->claims->count(); - } - public function get() { return [ 'labels' => $this->getLabels(), 'descriptions' => $this->getDescriptions(), 'claims' => $this->getClaims() ]; } public function getJSON( $args = null ) { return json_encode( $this->get(), $args ); } + + public static function createFromData( $data ) { + $dataModel = new self(); + if( isset( $data['labels'] ) ) { + foreach( $data['labels'] as $label ) { + $dataModel->setLabel( Label::createFromData( $label ) ); + } + } + if( isset( $data['descriptions'] ) ) { + foreach( $data['descriptions'] as $description ) { + $dataModel->setDescription( Description::createFromData( $description ) ); + } + } + if( isset( $data['claims'] ) ) { + foreach( $data['claims'] as $claims ) { + foreach( $claims as $claim ) { + $dataModel->addClaim( Claim::createFromData( $claim ) ); + } + } + } + return $dataModel; + } } diff --git "a/class-wb\\DataValue.php" "b/class-wb\\DataValue.php" index 74af58b..01340cb 100644 --- "a/class-wb\\DataValue.php" +++ "b/class-wb\\DataValue.php" @@ -1,55 +1,62 @@ . # Wikibase namespace wb; /** * A generic DataValue is part of a Snak. */ class DataValue { var $type; var $value; /** * @param $type string Type * @param $value mixed Value */ public function __construct( $type, $value ) { $this->setType( $type ) ->setValue( $value ); } public function getType() { return $this->type; } public function getValue() { return $this->value(); } public function setType( $type ) { $this->type = $type; return $this; } public function setValue( $value ) { $this->value = $value; return $this; } + + public static function createFromData( $data ) { + if( ! isset( $data['type'], $data['value'] ) ) { + throw new WrongDataException( __CLASS__ ); + } + return new self( $data['type'], $data['value'] ); + } } diff --git "a/class-wb\\StatementItem.php" "b/class-wb\\Descriptions.php" similarity index 69% copy from "class-wb\\StatementItem.php" copy to "class-wb\\Descriptions.php" index 679fa7c..a8e950a 100644 --- "a/class-wb\\StatementItem.php" +++ "b/class-wb\\Descriptions.php" @@ -1,35 +1,24 @@ . # Wikibase namespace wb; /** - * A StatementItem is a Statement that contains a SnakItem. + * A Label collector. */ -class StatementItem extends Statement { - - /** - * @param $property Wikidata property as 'P123' - * @param $item Wikidata item as 'Q1' - */ - public function __construct( $property, $item ) { - parent::__construct( $property, - new SnakItem( $property, $item ) - ); - } -} +class Descriptions extends Labels {} diff --git "a/class-wb\\Label.php" "b/class-wb\\Label.php" index df8499a..a799c08 100644 --- "a/class-wb\\Label.php" +++ "b/class-wb\\Label.php" @@ -1,53 +1,60 @@ . # Wikibase namespace wb; /** * An entity Label. * * @see https://www.wikidata.org/wiki/Wikidata:Glossary#Label */ class Label { var $language; var $value; public function __construct( $language, $value ) { $this->setLanguage( $language ) ->setValue( $value ); } public function getLanguage() { return $this->language; } public function getValue() { return $this->value; } public function setLanguage( $language ) { $this->language = $language; return $this; } public function setValue( $value ) { $this->value = $value; return $this; } + + public static function createFromData( $data ) { + if( ! isset( $data['language'], $data['value'] ) ) { + throw new WrongDataException( __CLASS__ ); + } + return new static( $data['language'], $data['value'] ); + } } diff --git "a/class-wb\\LabelAction.php" "b/class-wb\\LabelAction.php" index 9568907..fb04b15 100644 --- "a/class-wb\\LabelAction.php" +++ "b/class-wb\\LabelAction.php" @@ -1,44 +1,59 @@ . # Wikibase namespace wb; /** * @see https://www.wikidata.org/w/api.php?action=help&modules=wbeditentity */ class LabelAction extends Label { + const ADD = 'add'; + const OVERWRITE = 'overwrite'; + const REMOVE = 'remove'; + + public function __construct( $language, $value, $action = self::ADD ) { + parent::__construct( $language, $value ); + if( self::ADD === $action ) { + $this->pleasePreserve(); + } elseif( self::REMOVE === $action ) { + $this->pleaseRemove(); + } elseif( self::OVERWRITE !== $action ) { + throw new \Exception('unknown action'); + } + } + /** - * Add a Label without overwriting if it already exists. + * Without overwriting if it already exists. */ public function pleasePreserve() { $this->add = ''; unset( $this->remove ); return $this; } /** - * Remove the label. + * Removing if it already exists. */ - public function pleaseDelete() { + public function pleaseRemove() { $this->remove = ''; unset( $this->add ); return $this; } } diff --git "a/class-wb\\Claims.php" "b/class-wb\\Labels.php" similarity index 56% copy from "class-wb\\Claims.php" copy to "class-wb\\Labels.php" index dccad7e..3661a96 100644 --- "a/class-wb\\Claims.php" +++ "b/class-wb\\Labels.php" @@ -1,59 +1,60 @@ . # Wikibase namespace wb; /** - * A Claim collector. + * A Label collector. */ -class Claims { +class Labels { - var $claims = []; + var $labels = []; - public function __construct( $claims = [] ) { - $this->set( $claims ); + public function __construct( $labels = [] ) { + foreach( $labels as $label ) { + $this->add( $label ); + } } - public function add( Claim $claim ) { - $this->claims[] = $claim; - return $this; + public function get( $language ) { + foreach( $this->labels as $label ) { + if( $label->getLanguage() === $language ) { + return $label; + } + } + return false; } - public function count() { - return count( $this->claims ); + public function have( $language ) { + return false !== $this->get( $language ); } - public function set( $claims ) { - foreach( $claims as $claim ) { - $this->add( $claim ); + public function set( Label $label ) { + $existing = $this->get( $label->getLanguage() ); + if( $existing ) { + $existing = $language; + } else { + $this->labels[] = $label; } return $this; } - public function get() { - $properties = []; - foreach( $this->claims as $claim ) { - $property = $claim->getProperty(); - if( ! isset( $properties[ $property ] ) ) { - $properties[ $property ] = []; - } - $properties[ $property ][] = $claim; - } - return $properties; + public function getAll() { + return $this->labels; } } diff --git "a/class-wb\\Snak.php" "b/class-wb\\Snak.php" index 875c90b..14ad67f 100644 --- "a/class-wb\\Snak.php" +++ "b/class-wb\\Snak.php" @@ -1,82 +1,110 @@ . # Wikibase namespace wb; /** * A generic Snak is a combination of a property and a datatype + datavalue. * * A Snak is part of a Claim or a Reference. It's based on a DataValue. */ class Snak { //var $hash; var $snaktype; var $property; var $datatype; var $datavalue; /** * @param $snaktype string * @param $property string * @param $datatype string * @param $datavalue mixed */ public function __construct( $snaktype, $property, $datatype, $datavalue ) { $this->setSnakType( $snaktype ) ->setProperty( $property ) ->setDataType( $datatype ) ->setDataValue( $datavalue ); } public function getProperty() { return $this->property; } public function getSnakType() { return $this->snaktype; } public function getDataType() { return $this->datatype; } public function getDataValue() { return $this->datavalue; } public function setSnakType( $snaktype ) { $this->snaktype = $snaktype; return $this; } public function setProperty( $property ) { $this->property = $property; return $this; } public function setDataType( $datatype ) { $this->datatype = $datatype; return $this; } public function setDataValue( DataValue $datavalue ) { $this->datavalue = $datavalue; return $this; } + + public function hasHash() { + return isset( $this->hash ); + } + + public function getHash() { + return $this->hash; + } + + public function setHash( $hash ) { + $this->hash = $hash; + } + + public static function createFromData( $data ) { + if( ! isset( $data['snaktype'], $data['property'], $data['datatype'], $data['datavalue'] ) ) { + throw new WrongDataException( __CLASS__ ); + } + $snak = new self( + $data['snaktype'], + $data['property'], + $data['datatype'], + DataValue::createFromData( $data['datavalue'] ) + ); + if( isset( $data['hash'] ) ) { + $snak->setHash( $data['hash'] ); + } + return $snak; + } } diff --git "a/class-wb\\Snaks.php" "b/class-wb\\Snaks.php" index 6d009af..e0f4437 100644 --- "a/class-wb\\Snaks.php" +++ "b/class-wb\\Snaks.php" @@ -1,55 +1,50 @@ . # Wikibase namespace wb; /** * A Snak collector. */ class Snaks { var $snaks = []; public function __construct( $snaks = [] ) { - $this->set( $snaks ); + foreach( $snaks as $snak ) { + $this->add( $snak ); + } } public function add( Snak $snak ) { $this->snaks[] = $snak; return $this; } - public function set( $snaks ) { - foreach( $snaks as $snak ) { - $this->add( $snak ); - } - return $this; - } - - public function get() { + public function getAll() { $properties = []; foreach( $this->snaks as $snak ) { $property = $snak->getProperty(); if( ! isset( $properties[ $property ] ) ) { $properties[ $property ] = []; } $properties[ $property ][] = $snak; } return $properties; } } diff --git "a/class-wb\\Statement.php" "b/class-wb\\Statement.php" index bfb6791..7335087 100644 --- "a/class-wb\\Statement.php" +++ "b/class-wb\\Statement.php" @@ -1,59 +1,87 @@ . # Wikibase namespace wb; /** * A Statement is a type of Claim with references and a rank. * * @see https://www.wikidata.org/wiki/Wikidata:Glossary#Statement */ class Statement extends Claim { //var $id; var $type = 'statement'; var $rank = 'normal'; var $references = []; public function getType() { return $this->type; } public function getRank() { return $this->rank; } public function getReferences() { return $this->references; } + public function hasID() { + return isset( $this->id ); + } + + public function getID() { + return $this->id; + } + public function setType( $type ) { $this->type = $type; return $this; } public function setRank( $rank ) { $this->rank = $rank; return $this; } public function setReferences( $references ) { $this->references = $references; return $this; } + + public function setID( $id ) { + $this->id = $id; + } + + public static function createFromData( $data ) { + if( ! isset( $data['type'], $data['rank'] ) ) { + throw new WrongDataException( __CLASS__ ); + } + $statement = parent::createFromData( $data ); + $statement->setType( $data['type'] ); + $statement->setRank( $data['rank'] ); + if( $data['id'] ) { + $statement->setID( $data['id'] ); + } + if( $data['references'] ) { + $statement->setReferences( $data['references'] ); + } + return $statement; + } } diff --git "a/class-wb\\StatementCommonsMedia.php" "b/class-wb\\StatementCommonsMedia.php" index 9287679..6f163fa 100644 --- "a/class-wb\\StatementCommonsMedia.php" +++ "b/class-wb\\StatementCommonsMedia.php" @@ -1,35 +1,33 @@ . # Wikibase namespace wb; /** * A StatementCommonMedia is a Statement that contains a SnakCommonMedia. */ class StatementCommonsMedia extends Statement { /** * @param $property string Property as 'P123' * @param $filename string File name as 'File:Example.png' */ public function __construct( $property, $filename ) { - parent::__construct( $property, - new SnakCommonsMedia( $property, $filename ) - ); + parent::__construct( new SnakCommonsMedia( $property, $filename ) ); } } diff --git "a/class-wb\\StatementItem.php" "b/class-wb\\StatementItem.php" index 679fa7c..4dc7aba 100644 --- "a/class-wb\\StatementItem.php" +++ "b/class-wb\\StatementItem.php" @@ -1,35 +1,33 @@ . # Wikibase namespace wb; /** * A StatementItem is a Statement that contains a SnakItem. */ class StatementItem extends Statement { /** * @param $property Wikidata property as 'P123' * @param $item Wikidata item as 'Q1' */ public function __construct( $property, $item ) { - parent::__construct( $property, - new SnakItem( $property, $item ) - ); + parent::__construct( new SnakItem( $property, $item ) ); } } diff --git "a/class-wb\\StatementString.php" "b/class-wb\\StatementString.php" index 12d5541..1855940 100644 --- "a/class-wb\\StatementString.php" +++ "b/class-wb\\StatementString.php" @@ -1,35 +1,33 @@ . # Wikibase namespace wb; /** * A StatementString is a Statement that contains a SnakString. */ class StatementString extends Statement { /** * @param $property string Property as 'P123' * @param $filename string File name as 'File:Example.png' */ public function __construct( $property, $filename ) { - parent::__construct( $property, - new SnakString( $property, $filename ) - ); + parent::__construct( new SnakString( $property, $filename ) ); } } diff --git "a/class-wb\\StatementItem.php" "b/class-wb\\WrongDataException.php" similarity index 69% copy from "class-wb\\StatementItem.php" copy to "class-wb\\WrongDataException.php" index 679fa7c..f8b3d3b 100644 --- "a/class-wb\\StatementItem.php" +++ "b/class-wb\\WrongDataException.php" @@ -1,35 +1,28 @@ . # Wikibase namespace wb; -/** - * A StatementItem is a Statement that contains a SnakItem. - */ -class StatementItem extends Statement { - - /** - * @param $property Wikidata property as 'P123' - * @param $item Wikidata item as 'Q1' - */ - public function __construct( $property, $item ) { - parent::__construct( $property, - new SnakItem( $property, $item ) - ); +class WrongDataException extends \Exception { + public function __construct( $subject, $code = 0, Throwable $previous = null ) { + parent::__construct( sprintf( + 'invalid %s data', + $subject + ), $code, $previous ); } }