數(shù)據(jù)映射器是一個(gè)數(shù)據(jù)訪問(wèn)層,用于將數(shù)據(jù)在持久性數(shù)據(jù)存儲(chǔ)(通常是一個(gè)關(guān)系數(shù)據(jù)庫(kù))和內(nèi)存中的數(shù)據(jù)表示(領(lǐng)域?qū)樱┲g進(jìn)行相互轉(zhuǎn)換。其目的是為了將數(shù)據(jù)的內(nèi)存表示、持久存儲(chǔ)、數(shù)據(jù)訪問(wèn)進(jìn)行分離。該層由一個(gè)或者多個(gè)映射器組成(或者數(shù)據(jù)訪問(wèn)對(duì)象),并且進(jìn)行數(shù)據(jù)的轉(zhuǎn)換。映射器的實(shí)現(xiàn)在范圍上有所不同。通用映射器將處理許多不同領(lǐng)域的實(shí)體類(lèi)型,而專(zhuān)用映射器將處理一個(gè)或幾個(gè)。
此模式的主要特點(diǎn)是,與Active Record不同,其數(shù)據(jù)模式遵循單一職責(zé)原則(Single Responsibility Principle)。
User.php
<?php declare(strict_types=1); namespace DesignPatterns\Structural\DataMapper; class User { public static function fromState(array $state): User { // validate state before accessing keys! return new self( $state['username'], $state['email'] ); } public function __construct(private string $username, private string $email) { } public function getUsername(): string { return $this->username; } public function getEmail(): string { return $this->email; } }
UserMapper.php
<?php declare(strict_types=1); namespace DesignPatterns\Structural\DataMapper; use InvalidArgumentException; class UserMapper { public function __construct(private StorageAdapter $adapter) { } /** * finds a user from storage based on ID and returns a User object located * in memory. Normally this kind of logic will be implemented using the Repository pattern. * However the important part is in mapRowToUser() below, that will create a business object from the * data fetched from storage */ public function findById(int $id): User { $result = $this->adapter->find($id); if ($result === null) { throw new InvalidArgumentException("User #$id not found"); } return $this->mapRowToUser($result); } private function mapRowToUser(array $row): User { return User::fromState($row); } }
StorageAdapter.php
<?php declare(strict_types=1); namespace DesignPatterns\Structural\DataMapper; class StorageAdapter { public function __construct(private array $data) { } /** * @return array|null */ public function find(int $id) { if (isset($this->data[$id])) { return $this->data[$id]; } return null; } }
Tests/DataMapperTest.php
<?php declare(strict_types=1); namespace DesignPatterns\Structural\DataMapper\Tests; use InvalidArgumentException; use DesignPatterns\Structural\DataMapper\StorageAdapter; use DesignPatterns\Structural\DataMapper\User; use DesignPatterns\Structural\DataMapper\UserMapper; use PHPUnit\Framework\TestCase; class DataMapperTest extends TestCase { public function testCanMapUserFromStorage() { $storage = new StorageAdapter([1 => ['username' => 'domnikl', 'email' => 'liebler.dominik@gmail.com']]); $mapper = new UserMapper($storage); $user = $mapper->findById(1); $this->assertInstanceOf(User::class, $user); } public function testWillNotMapInvalidData() { $this->expectException(InvalidArgumentException::class); $storage = new StorageAdapter([]); $mapper = new UserMapper($storage); $mapper->findById(1); } }
更多建議: