diff --git "a/class-wb\\Claims.php" "b/class-wb\\Claims.php" index 72a5e90..13471ee 100644 --- "a/class-wb\\Claims.php" +++ "b/class-wb\\Claims.php" @@ -1,63 +1,73 @@ . # Wikibase namespace wb; /** * A Claim collector. */ class Claims { var $claims = []; public function __construct( $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 getInProperty( $property ) { + $claims = []; + foreach( $this->claims as $claim ) { + if( $claim->getMainSnak()->getProperty() === $property ) { + $claims[] = $claim; + } + } + return $claims; + } + public function haveProperty( $property ) { foreach( $this->claims as $claim ) { if( $claim->getMainSnak()->getProperty() === $property ) { return true; } } return false; } public function getAll() { $properties = []; foreach( $this->claims as $claim ) { $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 5bdd174..ffa7b8c 100644 --- "a/class-wb\\DataModel.php" +++ "b/class-wb\\DataModel.php" @@ -1,127 +1,139 @@ . # Wikibase namespace wb; class DataModel { private $labels; private $descriptions; private $claims; public function __construct() { $this->labels = new Labels(); $this->descriptions = new Descriptions(); $this->claims = new Claims(); } public function getLabels() { return $this->labels->getAll(); } public function getDescriptions() { return $this->descriptions->getAll(); } public function getClaims() { 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 getClaimsInProperty( $property ) { + return $this->claims->getInProperty( $property ); + } + public function countClaims() { return $this->claims->count(); } + public function hasLabelsInLanguage( $language ) { + return $this->labels->have( $language ); + } + /** * Set, delete, preserve if it exists, a label. */ public function setLabel( $label ) { $this->labels->set( $label ); return $this; } + public function hasDescriptionsInLanguage( $language ) { + return $this->descriptions->have( $language ); + } + /** * Set, delete, preserve if it exists, a description. */ public function setDescription( $description ) { $this->descriptions->set( $description ); return $this; } 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( ! empty( $data['labels'] ) ) { foreach( $data['labels'] as $label ) { $dataModel->setLabel( Label::createFromData( $label ) ); } } if( ! empty( $data['descriptions'] ) ) { foreach( $data['descriptions'] as $description ) { $dataModel->setDescription( Description::createFromData( $description ) ); } } if( ! empty( $data['claims'] ) ) { foreach( $data['claims'] as $claims ) { foreach( $claims as $claim ) { $dataModel->addClaim( Claim::createFromData( $claim ) ); } } } return $dataModel; } public static function createFromObject( $object ) { return self::createFromData( self::object2array( $object ) ); } private static function object2array( $object ) { if( ! is_object( $object) && ! is_array( $object ) ) { return $object; } $array = []; foreach( $object as $k => $v ) { $array[ $k ] = self::object2array( $v ); } return $array; } }