bemit/dynamodb

为 DynamoDB 定制的 PHP 服务类,包含项目数据转换器

0.0.2 2021-11-04 09:45 UTC

This package is auto-updated.

Last update: 2024-09-04 16:07:49 UTC


README

Latest Stable Version Latest Unstable Version codecov Total Downloads Github actions Build PHP Version Require

PHP DynamoDB 服务类,包含项目 <> 数据转换器。

composer require bemit/dynamodb

用法

use Bemit\DynamoDB\DynamoService;

$service = new DynamoService(
    string $region,
    string $dynamo_key, string $dynamo_secret,
    ?string $endpoint = null,
    $debug = false,
    // optional, overwrite the converters:
    ConvertFromItemInterface $from_item = null,
    ConvertToItemInterface $to_item = null,
);

// just the dynamodb client:
$client = $service->client();

//
// Convert from array / stdClass to DynamoDB Item:

// $arr = ['some_key' => 'the-text']
$item = $service->toItem($arr);

// or as stdClass:
// $std = new stdClass;
// $std->some_key = 'the-text';
$item = $service->toItem($std);

// single value:
// $arr_e = 'the-text'
$item_p = $service->toItemValue($arr_e);

//
// Convert from DynamoDB Item to array / stdClass:

// $item = ['some_key' => ['S' => 'the-text']]
$arr = $service->fromItem($item);
// $item_p = ['S' => 'the-text']]
$arr_p = $service->fromItemValue($item_p);

//
// Convert NS/SS from array / stdClass to DynamoDB:
//
// NS + SS needs a "key schema" when converting from array to item,
// nested usages of NS/SS are not automated and would result in a `L` the next save

// $arr_ss = ['s1', 's2', 's3']
$item_ss = $service->toItemValue($arr_ss, 'SS');
// $arr_ns = [1, 2, 3]
$item_ns = $service->toItemValue($arr_ns, 'NS');

// or:
// $obj = ['prop1' => ['s1', 's2', 's3']]
$item_obj = $service->toItem($obj, ['prop1' => 'SS']);

// 
// Ignore Nulls using when converting from array / stdClass to DynamoDB

// `true` as  third parameter of `toItem` will ignore null values in the root level of the item 
// $arr = ['k1' => 's1', 'k2' => null]
$item = $service->toItem($arr, [], true);

支持的模式,具有自动检测和转换

  • S,字符串
  • N,数字,通过 (float) 转换,保持数字不变,例如在 JSON 中(无 .0
  • BOOL,布尔值
  • M,映射,stdClass 或关联数组
    • 为了最安全的用法,使用 stdClass 进行 M 转换
    • 对于应用端,您应该使用例如非关联的 json_decode
    • 支持嵌套映射和列表
  • L,列表 / 数组 或空数组
    • 支持嵌套列表和映射
  • NULL,空值

toItem 处支持的模式带有类型,在 fromItem 处自动

  • SS,字符串集合 / 字符串列表
  • NS,字符串集合 / 字符串列表

开发者注意事项

设置和运行测试等命令

# on windows:
docker run -it --rm -v %cd%:/app composer install --ignore-platform-reqs

docker run -it --rm -v %cd%:/var/www/html php:8.1-rc-cli-alpine sh

docker run --rm -v %cd%:/var/www/html php:8.1-rc-cli-alpine sh -c "cd /var/www/html && ./vendor/bin/phpunit --testdox -c phpunit-ci.xml"

#docker-compose run --rm test sh -c "cd /var/www/html && composer update"
docker-compose run --rm test sh

cd /var/www/html && ./vendor/bin/phpunit --coverage-text --testdox -c phpunit-ci.xml

cd /var/www/html && ./vendor/bin/phpunit --coverage-html coverage --testdox -c phpunit-ci.xml

# on unix:
docker run -it --rm -v `pwd`:/app composer install

docker run -it --rm -v `pwd`:/var/www/html php:8.1-rc-cli-alpine sh

docker run --rm -v `pwd`:/var/www/html php:8.1-rc-cli-alpine sh -c "cd /var/www/html && ./vendor/bin/phpunit --testdox -c phpunit-ci.xml"

版本

本项目遵循 semver直到 1.0.0,并从 0.1.0 开始:所有 0.x.0 版本都像主版本一样,所有 0.0.x 都像次版本号或修订号,0.1.0 以下的模块应被视为实验性的。

许可证

本项目是免费软件,根据MIT 许可证分发。

Amazon DynamoDB® 是 Amazon.com, Inc. 的商标。使用这些标志不表示 Amazon.com, Inc. 的认可。

贡献者

通过将代码提交到代码库,您同意根据库附带的 MIT 许可证发布代码。

Michael Becker 维护