diff --git "a/include/class-wb\\DataValue.php" "b/include/class-wb\\DataValue.php" index 8d03f08..e976f89 100644 --- "a/include/class-wb\\DataValue.php" +++ "b/include/class-wb\\DataValue.php" @@ -1,98 +1,121 @@ . # Wikibase namespace wb; /** - * A generic DataValue is part of a Snak. + * A generic DataValue is part of a Snak */ class DataValue { - var $type; - var $value; + /** + * Type of the DataValue + * + * @var string + */ + private $type; + + /** + * Value of the DataValue + * + * @var array + */ + private $value; /** * Constructor * * @param $type string Type * @param $value mixed Value */ public function __construct( $type, $value ) { $this->setType( $type ) ->setValue( $value ); } /** * Get the type * * @return string */ public function getType() { return $this->type; } /** * Get the value * * @return mixed */ public function getValue() { return $this->value; } /** * Set the type */ public function setType( $type ) { $this->type = $type; return $this; } /** * Set the value * * @param mixed $value * @return self */ public function setValue( $value ) { $this->value = $value; return $this; } /** * Static constructor from a standard object * * @param object $data * @return self */ public static function createFromData( $data ) { if( ! isset( $data['type'], $data['value'] ) ) { throw new WrongDataException( __CLASS__ ); } return new self( $data['type'], $data['value'] ); } + /** + * Export this object to an associative array suitable for JSON-encoding + * + * @return array + */ + public function toData() { + return [ + 'type' => $this->getType(), + 'value' => $this->getValue(), + ]; + } + /** * Get this object in form of a string * * @return string */ public function __toString() { return json_encode( $this->getValue() ); } } diff --git "a/include/class-wb\\Snak.php" "b/include/class-wb\\Snak.php" index 9f76f6f..825e481 100644 --- "a/include/class-wb\\Snak.php" +++ "b/include/class-wb\\Snak.php" @@ -1,245 +1,249 @@ . # 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 { private $hash; private $snaktype; private $property; private $datatype; private $datavalue; /** * @param $snaktype string * @param $property string * @param $datatype string * @param $datavalue mixed */ public function __construct( $snaktype, $property, $datatype, $datavalue = null ) { $this->setSnakType( $snaktype ) ->setProperty( $property ) ->setDataType( $datatype ); if( null !== $datavalue ) { $this->setDataValue( $datavalue ); } } /** * Get the property * * @return string */ public function getProperty() { return $this->property; } /** * Get the snak type */ public function getSnakType() { return $this->snaktype; } /** * Get the data type */ public function getDataType() { return $this->datatype; } /** * Get the data value * * @return DataValue */ public function getDataValue() { return $this->datavalue; } /** * Set the snak type * * @param $snaktype * @param self */ public function setSnakType( $snaktype ) { $this->snaktype = $snaktype; return $this; } /** * Set the property * * @param $property string * @param self */ public function setProperty( $property ) { $this->property = $property; return $this; } /** * Get an human property label, if available in cache */ public function getPropertyLabel() { return self::propertyLabel( $this->getProperty() ); } /** * Set the data type * * @param $datatype * @param self */ public function setDataType( $datatype ) { $this->datatype = $datatype; return $this; } /** * Set the data value * * @param $datavalue DataValue * @param self */ public function setDataValue( DataValue $datavalue ) { $this->datavalue = $datavalue; return $this; } /** * Check if the snak has an hash * * @return bool */ public function hasHash() { return isset( $this->hash ); } /** * Get the hash * * @return string|null */ public function getHash() { return $this->hash; } /** * Set the hash * * @param $hash string * @return self */ public function setHash( $hash ) { $this->hash = $hash; return $this; } /** * Get a wikilink to this property * * @return string */ protected function getPropertyWLink() { $prop = $this->getProperty(); $label = $this->getPropertyLabel(); if( ! $label ) { $label = $prop; } return sprintf( '[[P:%s|%s]]', $prop, $label ); } /** * Try to read the property label from the cache * * @TODO: ask also the site * @return string|false */ public static function propertyLabel( $property ) { return \wm\Wikidata::propertyLabel( $property ); } /** * Create a snak from raw data * * @param $data array * @return self */ public static function createFromData( $data ) { if( ! isset( $data['snaktype'], $data['property'], $data['datatype'] ) ) { var_dump( $data ); throw new WrongDataException( __CLASS__ ); } $snak = new self( $data['snaktype'], $data['property'], $data['datatype'] ); if( isset( $data['datavalue'] ) ) { $snak->setDataValue( DataValue::createFromData( $data['datavalue'] ) ); } if( isset( $data['hash'] ) ) { $snak->setHash( $data['hash'] ); } return $snak; } /** * Convert this object to an associative array suitable for JSON encoding * * @return array */ public function toData() { $data = []; // it may have an hash if( $this->hasHash() ) { $data['hash'] = $this->getHash(); } $data['snaktype'] = $this->getSnakType(); $data['property'] = $this->getProperty(); $data['datatype'] = $this->getDataType(); $data['datavalue'] = $this->getDataValue(); + if( $data['datavalue'] ) { + $data['datavalue'] = $data['datavalue']->toData(); + } + return $data; } /** * @return string */ public function __toString() { return sprintf( '%s: %s', $this->getPropertyWLink(), $this->getDataValue() ); } }