Skip to content

Commit

Permalink
Merge pull request masayuki0812#6 from evalphobia/refactor-convert-co…
Browse files Browse the repository at this point in the history
…mponent

Refactored to parse component
  • Loading branch information
masayuki0812 committed Jul 13, 2014
2 parents 443c7fc + c7758f6 commit 05015cf
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions DynamoDBWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,21 +227,15 @@ public function createTable($tableName, $hashKey, $rangeKey = null, $options = n
$keySchema = array();

// HashKey
$hashKeyComponents = explode('::', $hashKey);
if (count($hashKeyComponents) < 2) {
$hashKeyComponents[1] = 'S';
}
$hashKeyComponents = $this->convertComponents($hashKey);
$hashKeyName = $hashKeyComponents[0];
$hashKeyType = $hashKeyComponents[1];
$attributeDefinitions []= array('AttributeName' => $hashKeyName, 'AttributeType' => $hashKeyType);
$keySchema[] = array('AttributeName' => $hashKeyName, 'KeyType' => 'HASH');

// RangeKey
if (isset($rangeKey)) {
$rangeKeyComponents = explode('::', $rangeKey);
if (count($rangeKeyComponents) < 2) {
$rangeKeyComponents[1] = 'S';
}
$rangeKeyComponents = $this->convertComponents($rangeKey);
$rangeKeyName = $rangeKeyComponents[0];
$rangeKeyType = $rangeKeyComponents[1];
$attributeDefinitions[] = array('AttributeName' => $rangeKeyName, 'AttributeType' => $rangeKeyType);
Expand Down Expand Up @@ -339,10 +333,7 @@ protected function convertAttributes($targets)
{
$newTargets = array();
foreach ($targets as $k => $v) {
$attrComponents = explode('::', $k);
if (count($attrComponents) < 2) {
$attrComponents[1] = 'S';
}
$attrComponents = $this->convertComponents($k);
$newTargets[$attrComponents[0]] = array($attrComponents[1] => $this->asString($v));
}
return $newTargets;
Expand All @@ -352,10 +343,7 @@ protected function convertUpdateAttributes($targets)
{
$newTargets = array();
foreach ($targets as $k => $v) {
$attrComponents = explode('::', $k);
if (count($attrComponents) < 2) {
$attrComponents[1] = 'S';
}
$attrComponents = $this->convertComponents($k);
$newTargets[$attrComponents[0]] = array(
'Action' => $v[0],
'Value' => array($attrComponents[1] => $this->asString($v[1])),
Expand All @@ -369,10 +357,7 @@ protected function convertConditions($conditions)
$ddbConditions = array();
foreach ($conditions as $k => $v) {
// Get attr name and type
$attrComponents = explode('::', $k);
if (count($attrComponents) < 2) {
$attrComponents[1] = 'S';
}
$attrComponents = $this->convertComponents($k);
$attrName = $attrComponents[0];
$attrType = $attrComponents[1];

Expand Down Expand Up @@ -454,4 +439,18 @@ protected function convertItems($items)
}
return $converted;
}

/**
* convert string attribute paramter to array components.
*
* @param string $attribute double colon separated string "<Attribute Name>::<Attribute type>"
* @return array parsed parameter. [0]=<Attribute Name>, [1]=<Attribute type>
*/
protected function convertComponents($attribute){
$components = explode('::', $attribute);
if (count($components) < 2) {
$components[1] = 'S';
}
return $components;
}
}

0 comments on commit 05015cf

Please sign in to comment.