1<?php
2
3namespace Basho\Riak\Command\TimeSeries;
4
5use Basho\Riak;
6use Basho\Riak\Command;
7use Basho\Riak\CommandInterface;
8
9/**
10 * Used to store data within a TS table
11 *
12 * @author Christopher Mancini <cmancini at basho d0t com>
13 */
14class Store extends Command implements CommandInterface
15{
16    protected $method = 'POST';
17
18    /**
19     * Stores the table name
20     *
21     * @var string|null
22     */
23    protected $table = NULL;
24
25    /**
26     * Stores the rows
27     *
28     * @var array $rows
29     */
30    protected $rows = [];
31
32    public function getTable()
33    {
34        return $this->table;
35    }
36
37    public function getData()
38    {
39        return $this->rows;
40    }
41
42    public function getEncodedData()
43    {
44        $rows = [];
45        foreach ($this->getData() as $row) {
46            $cells = [];
47            foreach ($row as $cell) {
48                /** @var $cell Riak\TimeSeries\Cell */
49                if ($cell->getType() == Riak\TimeSeries\Cell::BLOB_TYPE) {
50                    $cells[$cell->getName()] = base64_encode($cell->getValue());
51                } else {
52                    $cells[$cell->getName()] = $cell->getValue();
53                }
54            }
55            $rows[] = $cells;
56        }
57        return json_encode($rows);
58    }
59
60    public function __construct(Command\Builder\TimeSeries\StoreRows $builder)
61    {
62        parent::__construct($builder);
63
64        $this->table = $builder->getTable();
65        $this->rows = $builder->getRows();
66    }
67}