diff --git a/include/wb/Snaks.php b/include/wb/Snaks.php index cfef4dd..58264dd 100644 --- a/include/wb/Snaks.php +++ b/include/wb/Snaks.php @@ -1,131 +1,131 @@ . # Wikibase namespace wb; /** * A Snak collector * * @since 2019-07-18 */ class Snaks { /** * All the Snaks indexed by property */ private $snaks = []; /** * Constructor * * @param array $snaks Initial set of snaks */ public function __construct( $snaks = [] ) { foreach( $snaks as $snak ) { $this->add( $snak ); } } /** * Add a Snak * * @param object $snak * @return this */ public function add( Snak $snak ) { // eventually init the property container of snaks $property = $snak->getProperty(); if( !isset( $this->snaks[ $property ] ) ) { $this->snaks[ $property ] = []; } // append $this->snaks[ $property ][] = $snak; return $this; } /** * Get all the snaks from a certain property * * @param string $property Property name e.g. 'P123' * @return array */ public function getInProperty( $property ) { if( $this->hasInProperty( $property ) ) { return $this->snaks[ $property ]; } return []; } /** * Check if there are some snaks in this property */ public function hasInProperty( $property ) { return !empty( $this->snaks[ $property ] ); } /** * Check if there are not snaks * * @return boolean */ public function isEmpty() { - return !empty( $this->snaks ); + return empty( $this->snaks ); } /** * Get all the snaks indexed by property * * @return array */ public function getAllByProperty() { return $this->snaks; } /** * Count all the snaks * * @return int */ public function count() { $n = 0; foreach( $this->getAll() as $property => $snaks ) { $n += count( $snaks ); } return $n; } /** * Convert this object to an associative array suitable for JSON encoding * * @return array */ public function toData() { $data = []; foreach( $this->getAllByProperty() as $property => $snaks ) { $data[ $property ] = []; foreach( $snaks as $snak ) { $data[ $property ][] = $snak->toData(); } } return $data; } }