<?php

/**
 * @author Anton Terekhov <anton@netmonsters.ru>
 * @copyright Copyright Anton Terekhov, NetMonsters LLC, 2011-2013
 * @license https://github.com/AntonTerekhov/OrientDB-PHP/blob/master/LICENSE
 * @link https://github.com/AntonTerekhov/OrientDB-PHP
 * @package OrientDB-PHP
 */

/**
 * dataclusterCount() command for OrientDB-PHP
 *
 * @author Anton Terekhov <anton@netmonsters.ru>
 * @package OrientDB-PHP
 * @subpackage Command
 */
class OrientDBCommandDataclusterCount extends OrientDBCommandAbstract
{

    public function __construct($parent)
    {
        parent::__construct($parent);
        $this->opType = OrientDBCommandAbstract::DATACLUSTER_COUNT;
    }

    public function prepare()
    {
        parent::prepare();
        if (count($this->attribs) != 1) {
            throw new OrientDBWrongParamsException('This command requires cluster IDs in array');
        }
        if (!is_array($this->attribs[0])) {
            throw new OrientDBWrongParamsException('Array expected');
        }
        // Add count
        $this->addShort(count($this->attribs[0]));
        // Add clusterIDs
        for ($i = 0; $i < count($this->attribs[0]); $i++) {
            $this->addShort($this->attribs[0][$i]);
        }
        // Flag - Tombstone
        if ($this->parent->protocolVersion >= 13) {
            $this->addByte(0);
        }
    }

    /**
     * (non-PHPdoc)
     * @see OrientDBCommandAbstract::parseResponse()
     * @return int
     */
    protected function parseResponse()
    {
        $this->debugCommand('count');
        $count = $this->readLong();
        return $count;
    }
}
