https://github.com/bemit/php-service-dynamodb
Custom DynamoDB service for PHP.
https://github.com/bemit/php-service-dynamodb
Last synced: 24 days ago
JSON representation
Custom DynamoDB service for PHP.
- Host: GitHub
- URL: https://github.com/bemit/php-service-dynamodb
- Owner: bemit
- License: mit
- Created: 2021-10-29T15:24:28.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-11-04T09:51:53.000Z (over 3 years ago)
- Last Synced: 2025-02-15T00:33:01.407Z (3 months ago)
- Language: PHP
- Size: 43.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP DynamoDB Service
[](https://packagist.org/packages/bemit/dynamodb)
[](https://packagist.org/packages/bemit/dynamodb)
[](https://codecov.io/gh/bemit/php-service-dynamodb)
[](https://packagist.org/packages/bemit/dynamodb)
[](https://github.com/bemit/php-service-dynamodb/actions)
[](https://packagist.org/packages/bemit/dynamodb)PHP DynamoDB service class, with item <> data converters.
```shell
composer require bemit/dynamodb
```Usage:
```php
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);
```Modes supported, with automatic detection and conversion:
- `S`, strings
- `N`, numerics, cast by `(float)`, keeps numbers the same in e.g. JSON (no `.0`)
- `BOOL`, booleans
- `M`, maps, `stdClass` or assoc arrays
- for safest usage uses `stdClass` for `M` conversion
- for app-side, you should use e.g. non-assoc `json_decode`
- supports nested maps & lists
- `L`, lists / arrays **or empty array**
- supports nested lists & maps
- `NULL`, null valuesModes supported with typing at `toItem`, automatic at `fromItem`:
- `SS`, string sets / list of strings
- `NS`, string sets / list of strings## Dev Notices
Commands to set up and run e.g. tests:
```bash
# on windows:
docker run -it --rm -v %cd%:/app composer install --ignore-platform-reqsdocker 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 shcd /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 installdocker 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"
```## Versions
This project adheres to [semver](https://semver.org/), **until `1.0.0`** and beginning with `0.1.0`: all `0.x.0` releases are like MAJOR releases and all `0.0.x` like MINOR or PATCH, modules below `0.1.0` should be considered experimental.
## License
This project is free software distributed under the [**MIT LICENSE**](LICENSE).
> Amazon DynamoDB® is a trademark of Amazon.com, Inc. No endorsements by Amazon.com, Inc. are implied by the use of these marks.
### Contributors
By committing your code to the code repository you agree to release the code under the MIT License attached to the repository.
***
Maintained by [Michael Becker](https://mlbr.xyz)