attribute ( $value, $format, $type_override )

Formats a value into the DynamoDB attribute value format (e.g. array('S' => 'value') ).

Access

public

Parameters

Parameter

Type

Required

Description

$value

mixed

Required

The value to be formatted.

$format

string

Optional

The format of the result (based loosely on the type of operation)

$type_override

string

Optional

Any valid attribute type to override the calculated type.

Returns

Type

Description

array

An attribute value suitable for DynamoDB.

Examples

Marshal values into DynamoDB-ready formats (single values)

// Instantiate the class
$ddb = new AmazonDynamoDB();

$attribute1 = $ddb->attribute('foo');
$attribute2 = $ddb->attribute(1);
$attribute3 = $ddb->attribute(array('foo', 'bar', 'baz'));
$attribute4 = $ddb->attribute(array(1, 2, 3));

var_dump($attribute1, $attribute2, $attribute3, $attribute4);
Result:
array(1) {
  ["S"]=>
  string(3) "foo"
}
array(1) {
  ["N"]=>
  string(1) "1"
}
array(1) {
  ["SS"]=>
  array(3) {
    [0]=>
    string(3) "foo"
    [1]=>
    string(3) "bar"
    [2]=>
    string(3) "baz"
  }
}
array(1) {
  ["NS"]=>
  array(3) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
    [2]=>
    string(1) "3"
  }
}

Marshal attribute values into DynamoDB-ready formats (single value for update)

// Instantiate the class
$ddb = new AmazonDynamoDB();

$attribute1 = $ddb->attribute(1, 'put');
$attribute2	= $ddb->attribute(1, 'update');

var_dump($attribute1, $attribute2);
Result:
array(1) {
  ["N"]=>
  string(1) "1"
}
array(1) {
  ["Value"]=>
  array(1) {
    ["N"]=>
    string(1) "1"
  }
}

Source

Method defined in services/dynamodb.class.php | Toggle source view (33 lines) | View on GitHub

public function attribute($value, $format = 'put', $type_override = null)
{
    $info = $this->get_attribute_value_info($value);

    if (!$info)
    {
        return null;
    }

    if ($type_override)
    {
        static $valid_types = array(
            self::TYPE_STRING,
            self::TYPE_NUMBER,
            self::TYPE_BINARY,
            self::TYPE_STRING_SET,
            self::TYPE_NUMBER_SET,
            self::TYPE_BINARY_SET,
        );

        $info['type'] = in_array($type_override, $valid_types) ? $type_override : $info['type'];
    }

    $result = array($info['type'] => $info['value']);

    // In some cases the result needs to be wrapped with "Value"
    if ($format === 'update' || $format === 'expected')
    {
        $result = array('Value' => $result);
    }

    return $result;
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback