diff --git "a/class-wb\\DataValueQuantity.php" "b/class-wb\\DataValueQuantity.php" new file mode 100644 index 0000000..058da89 --- /dev/null +++ "b/class-wb\\DataValueQuantity.php" @@ -0,0 +1,55 @@ +. + +# Wikibase +namespace wb; + +/** + * A DataValue for a quantity. + */ +class DataValueQuantity extends DataValue { + + /** + * @param $value mixed amount Any number e.g. 0 + * @param $wikidata_unit_id string Wikidata ID of a unit e.g. 'Q11573' for metre + * @param $upper_bound mixed The $value at its maximum error e.g. +1 + * @param $lower_bound mixed The $value at its minimum error e.g. -1 + */ + public function __construct( $amount, $wikidata_unit_id, $upper_bound = null, $lower_bound = null ) { + $value = [ + 'amount' => self::int2string( $amount ), + 'unit' => "http://www.wikidata.org/entity/$wikidata_unit_id" + ]; + if( null !== $upper_bound ) { + $value['upperBound'] = self::int2string( $upper_bound ); + } + if( null !== $lower_bound ) { + $value['lowerBound'] = self::int2string( $lower_bound ); + } + parent::__construct( DataType::QUANTITY, $value ); + } + + /** + * Convert a number to a prefixed string e.g. 1 is converted to '+1' + * + * @param $n mixed + * @return string + */ + private static function int2string( $n ) { + return $n > 0 ? '+' . $n : '-' . $n; + } +} diff --git "a/class-wb\\StatementString.php" "b/class-wb\\SnakQuantity.php" similarity index 63% copy from "class-wb\\StatementString.php" copy to "class-wb\\SnakQuantity.php" index 1855940..3a82577 100644 --- "a/class-wb\\StatementString.php" +++ "b/class-wb\\SnakQuantity.php" @@ -1,33 +1,38 @@ . # Wikibase namespace wb; /** - * A StatementString is a Statement that contains a SnakString. + * A Snak for a string. */ -class StatementString extends Statement { +class SnakQuantity extends Snak { /** - * @param $property string Property as 'P123' - * @param $filename string File name as 'File:Example.png' + * @param $property string Wikidata property as 'P123' + * @param $value string */ - public function __construct( $property, $filename ) { - parent::__construct( new SnakString( $property, $filename ) ); + public function __construct( $property, $amount, $unit, $upper_bound = null, $lower_bound = null ) { + return parent::__construct( + 'value', + $property, + DataType::QUANTITY, + new DataValueQuantity( $amount, $unit, $upper_bound, $lower_bound ) + ); } } diff --git "a/class-wb\\Snaks.php" "b/class-wb\\Snaks.php" index e0f4437..b509068 100644 --- "a/class-wb\\Snaks.php" +++ "b/class-wb\\Snaks.php" @@ -1,50 +1,54 @@ . # Wikibase namespace wb; /** * A Snak collector. */ class Snaks { var $snaks = []; public function __construct( $snaks = [] ) { foreach( $snaks as $snak ) { $this->add( $snak ); } } + public static function factory( $snaks = [] ) { + return new self( $snaks ); + } + public function add( Snak $snak ) { $this->snaks[] = $snak; return $this; } 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 7335087..08a5134 100644 --- "a/class-wb\\Statement.php" +++ "b/class-wb\\Statement.php" @@ -1,87 +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 6f163fa..fd4e9d7 100644 --- "a/class-wb\\StatementCommonsMedia.php" +++ "b/class-wb\\StatementCommonsMedia.php" @@ -1,33 +1,41 @@ . # 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( new SnakCommonsMedia( $property, $filename ) ); } + + /** + * @param $property string Property as 'P123' + * @param $filename string File name as 'File:Example.png' + */ + public static function factory( $property, $filename ) { + return new self( $property, $filename ); + } } diff --git "a/class-wb\\StatementItem.php" "b/class-wb\\StatementItem.php" index 4dc7aba..8a5fd3d 100644 --- "a/class-wb\\StatementItem.php" +++ "b/class-wb\\StatementItem.php" @@ -1,33 +1,41 @@ . # 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( new SnakItem( $property, $item ) ); } + + /** + * @param $property Wikidata property as 'P123' + * @param $item Wikidata item as 'Q1' + */ + public static function factory( $property, $item ) { + return new self( $property, $item ); + } } diff --git "a/class-wb\\StatementQuantity.php" "b/class-wb\\StatementQuantity.php" new file mode 100644 index 0000000..4737ca0 --- /dev/null +++ "b/class-wb\\StatementQuantity.php" @@ -0,0 +1,47 @@ +. + +# Wikibase +namespace wb; + +/** + * A StatementString is a Statement that contains a SnakString. + */ +class StatementQuantity extends Statement { + + /** + * @param $property string Property as 'P123' + * @param $value mixed amount Any number e.g. 0 + * @param $wikidata_unit_id string Wikidata ID of a unit e.g. 'Q11573' for metre + * @param $upper_bound mixed The $value at its maximum error e.g. +1 + * @param $lower_bound mixed The $value at its minimum error e.g. -1 + */ + public function __construct( $property, $amount, $unit, $upper_bound = null, $lower_bound = null ) { + parent::__construct( new SnakQuantity( $property, $amount, $unit, $upper_bound = null, $lower_bound = null ) ); + } + + /** + * @param $property string Property as 'P123' + * @param $value mixed amount Any number e.g. 0 + * @param $wikidata_unit_id string Wikidata ID of a unit e.g. 'Q11573' for metre + * @param $upper_bound mixed The $value at its maximum error e.g. +1 + * @param $lower_bound mixed The $value at its minimum error e.g. -1 + */ + public static function factory( $property, $amount, $unit, $upper_bound = null, $lower_bound = null ) { + return new self( $property, $amount, $unit, $upper_bound = null, $lower_bound = null ); + } +} diff --git "a/class-wb\\StatementString.php" "b/class-wb\\StatementString.php" index 1855940..07158d3 100644 --- "a/class-wb\\StatementString.php" +++ "b/class-wb\\StatementString.php" @@ -1,33 +1,41 @@ . # 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( new SnakString( $property, $filename ) ); } + + /** + * @param $property string Property as 'P123' + * @param $filename string File name as 'File:Example.png' + */ + public static function factory( $property, $filename ) { + return new self( $property, $filename ); + } }