diff --git a/include/wb/Claims.php b/include/wb/Claims.php index 3cdfd0d..3f3aaf6 100644 --- a/include/wb/Claims.php +++ b/include/wb/Claims.php @@ -1,139 +1,140 @@ . # Wikibase namespace wb; /** * Claim collector */ class Claims { /** * All the claims * * @TODO: check if it can be declared as private without breaking casts :^) * @var array */ public $claims = []; /** * Constructor * * @param $claims array */ public function __construct( $claims = [] ) { foreach( $claims as $claim ) { $this->add( $claim ); } } /** * Add a claim * * @param $claim $Claim * @return self */ public function add( Claim $claim ) { $this->claims[] = $claim; return $this; } /** * Count all * * @return int */ public function count() { return count( $this->claims ); } /** * Get all the claims in a certain property * * @param $property string * @return array */ public function getInProperty( $property ) { $claims = []; foreach( $this->claims as $claim ) { if( $claim->getMainSnak()->getProperty() === $property ) { $claims[] = $claim; } } return $claims; } /** * Check if there are claims in a certain property * * @param $property string * @return bool */ public function haveProperty( $property ) { foreach( $this->claims as $claim ) { $snak = $claim->getMainsnak(); if( $snak && $snak->getProperty() === $property ) { return true; } } return false; } /** * Get all the claims * * @return array */ public function getAll() { return $this->claims; } /** * Get all the claims indexed by their property * * Note that you can obtain also a dummy property * * @return array */ public function getAllGrouped() { $properties = []; foreach( $this->getAll() as $claim ) { $property = $claim->getPropertyAlsoDummy(); if( ! isset( $properties[ $property ] ) ) { $properties[ $property ] = []; } $properties[ $property ][] = $claim; } return $properties; } /** * Convert this object to an associative array suitable for JSON encoding * * @return array */ public function toData() { - $all = $this->getAllGrouped(); - foreach( $all as & $claims ) { + $data = []; + foreach( $this->getAllGrouped() as $prop => $claims ) { + $data[ $prop ] = []; foreach( $claims as & $claim ) { - $claim = $claim->toData(); + $data[ $prop ][] = $claim->toData(); } } - return $all; + return $data; } }