walnut / lib_modelmapper_recordstorage

此包的最新版本(dev-main)没有可用的许可信息。

dev-main 2022-05-13 21:35 UTC

This package is not auto-updated.

Last update: 2024-09-30 04:22:06 UTC


README

将记录存储作为模型映射器使用的适配器

示例

这是一个非常简单的数据模型 - 客户端。

class Client {
	public function __construct(
		public /*readonly*/ string $id,
		public /*readonly*/ string $name
	) {}
}

需要一个序列化/反序列化逻辑。

/**
 * @implements ModelBuilder<Client>
 * @implements ModelParser<Client>
 */
final class ClientSerializer implements ModelBuilder, ModelParser {

	/**
	 * @param array $source
	 * @return Client
	 */
	public function build(array $source): object {
		return new Client(
			$source['id'] ?? '',
			$source['name'] ?? ''
		);
	}

	/**
	 * @param Client $source
	 * @return array
	 */
	public function parse(object $source): array {
		return [
			'id' => $source->id,
			'name' => $source->name
		];
	}
}

所有序列化器可以聚合到一个工厂类中

final class MapperFactory implements ModelBuilderFactory, ModelParserFactory {

	/**
	 * @param string $className
	 * @return ModelBuilder&ModelParser
	 */
	private function getSerializer(string $className): object /*ModelBuilder&ModelParser*/ {
		return match($className) {
			Client::class => new ClientSerializer,
			default => throw new RuntimeException("Unknown class $className")
		};
	}

	public function getBuilder(string $className): ModelBuilder {
		return $this->getSerializer($className);
	}

	public function getParser(string $className): ModelParser {
		return $this->getSerializer($className);
	}
	
}

使用文件内的JSON序列化存储

$storage = new SerializedRecordStorage(
    new JsonArrayDataSerializer(
        new PhpJsonSerializer
    ),
    //uncomment to use in-memory instead: new InMemoryKeyValueStorage(['clients' => '{}'])
    new InFileKeyValueStorage(
        new PerFileKeyToFileNameMapper('./storage', '.json'),
        new PhpFileAccessor    
    )
);
$serializerFactory = new MapperFactory;

//Mapping between the models and their storage keys:
$configuration = new RecordStorageModelMapperConfiguration(
    [Client::class => 'clients']
);

//Mapper factory covering all models
$modelMapperFactory = new RecordStorageModelMapperFactory(
    $configuration,
    new ArrayDataAccessorFactory($storage),
    $serializerFactory,
    $serializerFactory
);

//Take the mapper for the Client model
$mapper = $modelMapperFactory->getMapper(Client::class);

//Prepare two records
$firstClient = new Client('cl-1', 'Client 1');
$secondClient = new Client('cl-2', 'Client 2');

//Using the storage
$mapper->all(); //0 entries

$mapper->store($firstClient->id, $firstClient);
$mapper->store($secondClient->id, $secondClient);

$mapper->all(); //2 entries

$updatedSecondClient = new Client('cl-2', 'Client 2 new name');

$mapper->store($updatedSecondClient->id, $updatedSecondClient);
$mapper->all(); //2 entries

$mapper->byId($firstClient->id)->name; //Client 1
$mapper->byId($updatedSecondClient->id)->name; //Client 2 new name

$mapper->remove($firstClient->id);
$mapper->byId($firstClient->id); //null

$mapper->all(); //1 entry