1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
<?php
namespace Basho\Riak;
use Basho\Riak;
use Basho\Riak\Api\Http\Translator\SecondaryIndex;
class Object
{
protected $data = null;
protected $indexes = [];
protected $vclock = '';
protected $content_type = 'text/plain';
protected $content_encoding = 'utf-8';
protected $charset = 'utf-8';
protected $metadata = [];
public function __construct($data = null, $headers = [])
{
$this->data = $data;
if (empty($headers) || !is_array($headers)) {
return;
}
$this->indexes = (new SecondaryIndex)->extractIndexesFromHeaders($headers);
if (!empty($headers[Riak\Api\Http::CONTENT_TYPE_KEY])) {
if (strpos($headers[Riak\Api\Http::CONTENT_TYPE_KEY], 'charset')) {
$parts = explode(';', trim($headers[Riak\Api\Http::CONTENT_TYPE_KEY]));
$this->content_type = $parts[0];
$this->charset = trim(strrpos($parts[1], '='));
} else {
$this->content_type = $headers[Riak\Api\Http::CONTENT_TYPE_KEY];
}
}
if (!empty($headers[Riak\Api\Http::VCLOCK_KEY])) {
$this->vclock = $headers[Riak\Api\Http::VCLOCK_KEY];
}
foreach($headers as $key => $value) {
if (strpos($key, Riak\Api\Http::METADATA_PREFIX) !== false) {
$this->metadata[substr($key, strlen(Riak\Api\Http::METADATA_PREFIX))] = $value;
}
}
}
public function getData()
{
return $this->data;
}
public function setData($data)
{
$this->data = $data;
return $this;
}
public function getContentType()
{
return $this->content_type;
}
public function setContentType($content_type)
{
$this->content_type = $content_type;
return $this;
}
public function getContentEncoding()
{
return $this->content_encoding;
}
public function setContentEncoding($content_encoding)
{
$this->content_encoding = $content_encoding;
return $this;
}
public function getCharset()
{
return $this->charset;
}
public function setCharset($charset)
{
$this->charset = $charset;
return $this;
}
public function getVclock()
{
return $this->vclock;
}
public function setVclock($vclock)
{
$this->vclock = $vclock;
return $this;
}
public function getIndexes()
{
return $this->indexes;
}
public function getIndex($indexName)
{
return isset($this->indexes[$indexName]) ? $this->indexes[$indexName] : null;
}
public function addValueToIndex($indexName, $value)
{
$this->validateIndexNameAndValue($indexName, $value);
if (!isset($this->indexes[$indexName])) {
$this->indexes[$indexName] = [];
}
$this->indexes[$indexName][] = $value;
return $this;
}
private function validateIndexNameAndValue($indexName, $value)
{
if (!is_scalar($value)) {
throw new \InvalidArgumentException("Invalid index type for '" . $indexName .
"'index. Expecting '*_int' for an integer index, or '*_bin' for a string index.");
}
$isIntIndex = SecondaryIndex::isIntIndex($indexName);
$isStringIndex = SecondaryIndex::isStringIndex($indexName);
if (!$isIntIndex && !$isStringIndex) {
throw new \InvalidArgumentException("Invalid index type for '" . $indexName .
"'index. Expecting '*_int' for an integer index, or '*_bin' for a string index.");
}
if ($isIntIndex && !is_int($value)) {
throw new \InvalidArgumentException("Invalid type for '" . $indexName .
"'index. Expecting 'integer', value was '" . gettype($value) . "''");
}
if ($isStringIndex && !is_string($value)) {
throw new \InvalidArgumentException("Invalid type for '" . $indexName .
"'index. Expecting 'string', value was '" . gettype($value) . "''");
}
}
public function removeValueFromIndex($indexName, $value)
{
if (!isset($this->indexes[$indexName])) {
return $this;
}
$valuePos = array_search($value, $this->indexes[$indexName]);
if ($valuePos !== false) {
array_splice($this->indexes[$indexName], $valuePos, 1);
}
if (count($this->indexes[$indexName]) == 0) {
unset($this->indexes[$indexName]);
}
return $this;
}
public function setMetaDataValue($key, $value = '')
{
$this->metadata[$key] = $value;
return $this;
}
public function getMetaDataValue($key)
{
return $this->metadata[$key];
}
public function removeMetaDataValue($key)
{
unset($this->metadata[$key]);
return $this;
}
public function getMetaData()
{
return $this->metadata;
}
}