Skip to content
This repository was archived by the owner on Aug 4, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion lib/phpcassa/Schema/DataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class DataType
const LEXICAL_UUID_TYPE = "LexicalUUIDType";
const UUID_TYPE = "UUIDType";
const DATE_TYPE = "DateType";
const SET_TYPE = "SetType";
const LIST_TYPE = "ListType";
const MAP_TYPE = "MapType";

public static $class_map;

Expand All @@ -39,7 +42,10 @@ public static function init() {
'UUIDType' => 'phpcassa\Schema\DataType\UUIDType',
'BooleanType' => 'phpcassa\Schema\DataType\BooleanType',
'DateType' => 'phpcassa\Schema\DataType\DateType',
'Int32Type' => 'phpcassa\Schema\DataType\Int32Type',
'Int32Type' => 'phpcassa\Schema\DataType\Int32Type',
'SetType' => 'phpcassa\Schema\DataType\SetType',
'ListType' => 'phpcassa\Schema\DataType\ListType',
'MapType' => 'phpcassa\Schema\DataType\MapType'
);
}

Expand Down Expand Up @@ -81,6 +87,22 @@ public static function get_type_for($typestr) {
return new CompositeType(self::get_inner_types($typestr));
} else if (strpos($typestr, 'ReversedType') !== false) {
return self::get_type_for(self::get_inner_type($typestr));
} else if (preg_match('/(?<type>.+[^\(])(\(((?<keytype>.+[^\)]),(?<valuetype>.+[^\)]))\))/is',$typestr,$columnType)){
$type_name = self::extract_type_name($columnType['type']);
$type_class = self::$class_map[$type_name];
$keytype_name = self::extract_type_name($columnType['keytype']);
$keytype_class = self::$class_map[$keytype_name];
$valuetype_name = self::extract_type_name($columnType['valuetype']);
$valuetype_class = self::$class_map[$valuetype_name];

return new $type_class(new $keytype_class,new $valuetype_class);
} else if (preg_match('/(?<type>.+[^\(])(\(((?<valuetype>.+[^\)]))\))/is',$typestr,$columnType)){
$type_name = self::extract_type_name($columnType['type']);
$type_class = self::$class_map[$type_name];
$valuetype_name = self::extract_type_name($columnType['valuetype']);
$valuetype_class = self::$class_map[$valuetype_name];

return new $type_class(new $valuetype_class);
} else {
$type_name = self::extract_type_name($typestr);
$type_class = self::$class_map[$type_name];
Expand Down
55 changes: 55 additions & 0 deletions lib/phpcassa/Schema/DataType/ListType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace phpcassa\Schema\DataType;

/**
* Handles any type of List.
*
* @package phpcassa\Schema\DataType
*/
class ListType extends CassandraType
{
protected $valueType;

public function __construct(CassandraType $valueType){
$this->valueType = $valueType;
}

private function readUInt16BE($value,$offset)
{
list(, $int) = unpack('s*', strrev(substr($value,$offset,2)));
return $int;
}

private function writeUInt16BE($value)
{
return strrev(pack('s*', $value));
}

public function pack(Array $data) {
$return = $this->writeUInt16BE(count($data));

foreach ($data as $value){
$packed = $this->valueType->pack($value);
$return.= $this->writeUInt16BE(strlen($packed));
$return.= $packed;
}

return $return;
}

public function unpack($data) {
$offset = 0;
$total = $this->readUInt16BE($data,$offset);
$offset += 2;

$items = [];
for ($i = 0;$i < $total;$i++){
$length = $this->readUInt16BE($data,$offset);
$offset += 2;
$items[] = $this->valueType->unpack(substr($data,$offset,$length));
$offset += $length;
}

return $items;
}
}
66 changes: 66 additions & 0 deletions lib/phpcassa/Schema/DataType/MapType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
namespace phpcassa\Schema\DataType;

/**
* Handles any type of Map.
*
* @package phpcassa\Schema\DataType
*/
class MapType extends CassandraType
{
protected $keyType;
protected $valueType;

public function __construct(CassandraType $keyType, CassandraType $valueType){
$this->keyType = $keyType;
$this->valueType = $valueType;
}

private function readUInt16BE($value,$offset)
{
list(, $int) = unpack('s*', strrev(substr($value,$offset,2)));
return $int;
}

private function writeUInt16BE($value)
{
return strrev(pack('s*', $value));
}

public function pack(Array $data) {
$return = $this->writeUInt16BE(count($data));

foreach ($data as $key=>$value){
$packed = $this->keyType->pack($key);
$return.= $this->writeUInt16BE(strlen($packed));
$return.= $packed;
$packed = $this->valueType->pack($value);
$return.= $this->writeUInt16BE(strlen($packed));
$return.= $packed;
}

return $return;
}

public function unpack($data) {
$offset = 0;
$total = self::readUInt16BE($data,$offset);
$offset += 2;

$items = [];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't do this in 5.3.

for ($i = 0;$i < $total;$i++){
$keyLength = self::readUInt16BE($data,$offset);
$offset += 2;
$key = $this->keyType->unpack(substr($data,$offset,$keyLength));
$offset += $keyLength;
$valueLength = self::readUInt16BE($data,$offset);
$offset += 2;
$value = $this->keyType->unpack(substr($data,$offset,$valueLength));
$offset += $valueLength;

$items[$key] = $value;
}

return $items;
}
}
12 changes: 12 additions & 0 deletions lib/phpcassa/Schema/DataType/SetType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace phpcassa\Schema\DataType;

/**
* Handles any type of Set.
*
* @package phpcassa\Schema\DataType
*/
class SetType extends ListType
{

}