thedomeffm/sapphire 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

thedomeffm/sapphire

Composer 安装命令:

composer require thedomeffm/sapphire

包简介

A simple PHP DynamoDB ODM

README 文档

README

composer require thedomeffm/sapphire

Info

This library is WIP! Not ready for production!

Sapphire is simple PHP DynamoDB ODM. Made to be fast and tiny. Ideal for serverless PHP functions.

The ODM does just the mapping part and does not handle queries at all! You have to use async-aws or the aws-sdk in order to work with the ODM.

The mapping just work with typed properties and just uses php attributes! There is no support for annotation!

Requirements

  • php: >=8.0
  • typed properties!
  • composer: >=2.0

The package has probably some problems if used with an old composer 1 version

What does the library do?

You add Attributes to your PHP Classes and with the help of the DynamoManager you can convert to PHP Object to an Array that the AWS-SDK or async-aws can work with and vice versa. The library does not care about generating missing tables. You can use CloudFormation or Terraform with this.

Example

<?php

use Symfony\Component\Uid\UuidV4;
use TheDomeFfm\Sapphire\Attribute\DynamoClass;
use TheDomeFfm\Sapphire\Attribute\DynamoField;

#[DynamoClass('products')]
class Product
{
    #[DynamoField]
    private ?string $id;
    
    #[DynamoField]
    private ?string $name;
    
    #[DynamoField]
    private ?float $price;
    
    public function __construct() {
        $this->id = (string) UuidV4::v4();
    }
}

Save item in DynamoDB

<?php

// ...

$product = new Product();
$product->name = $form['name'];
$product->prive = $form['price'];

$dm = new DynamoManager();

$putItem = $dm->preparePutAction($product);
$dynamoDbClient->putItem($putItem);

Cast AWS DynamoDB Item to your PHP Object

<?php

// ...

$dm = new DynamoManager();

$getItem = [
    'TableName' => $dm->getTableName(Product::class),
    'Key' => [
        'id' => ['S' => $id]
    ]
];

$product = $dynamoDbClient->getItem($getItem)->getItem();

$product = $dm->getObject($product, Product::class);

More complex Examples

Typed arrays => Dynamo Data Sets

#[DynamoClass('CustomerDataSet')]
class Example {
    #[DynamoField]
    private ?string $id;

    #[DynamoField(arrayType: 'string')]
    private array $stringArray = [
        'one', 'two', 'five'
    ];

    #[DynamoField(arrayType: 'mixed')]
    private array $mixedArray = [
        'one', 2.5, 'eight'
    ];

    #[DynamoField(arrayType: 'number')]
    private array $numberArray = [
        1, 2.5, 3.01
    ];

    #[DynamoField(arrayType: 'binary')]
    private array $binArray = [
        1337, 'i like cheesecake', null, 'potato'
    ];

    public function __construct()
    {
        $this->id = (string) UuidV4::v4();
    }
    
    // ...
}

IMPORTANT INFO The content of the array is not checked by the ODM (for performance reasons)!

arrayType DynamoDB Set
mixed L
string SS
number NS
binary BS

Embedded Documents

#[DynamoEmbeddedClass]
class Category
{
    #[DynamoField]
    private string $id;

    #[DynamoField]
    private ?string $name = null;

    public function __construct()
    {
        $this->id = (string) UuidV4::v4();
    }
}

#[DynamoClass('products')]
class Product
{
    #[DynamoField]
    private ?string $id;
    
    #[DynamoField]
    private ?string $name;
    
    #[DynamoField]
    private ?float $price;
    
    #[DynamoField]
    private ?Category $category;
    
    public function __construct() {
        $this->id = (string) UuidV4::v4();
    }
}

Info The embedded document will be saved as 'M' => Map

See documentation

Binary

You need to use string or ?string to use isBinary!

#[DynamoClass('customers')]
class Customer
{
    // ...
    
    #[DynamoField(isBinary: true)]
    public ?string $icon = null;
    
    // ...
}


$customer = new Customer();
// ...
$customer->icon = file_get_contents('user_icon.png');

INFO The value will be converted to base64! If you need the binary use base64_decode() on the property.

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固