-
Notifications
You must be signed in to change notification settings - Fork 17
/
TurnipsMapper.php
70 lines (59 loc) · 1.39 KB
/
TurnipsMapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace DesignPatterns\Structural\DataMapper;
use InvalidArgumentException;
/**
* Class TurnipsMapper.
*/
class TurnipsMapper
{
/**
* @var StorageAdapter
*/
protected $adapter;
/**
* TurnipsMapper constructor.
*
* @param StorageAdapter $storage
*/
public function __construct(StorageAdapter $storage)
{
$this->adapter = $storage;
}
/**
* @param int $id
*
* @throws InvalidArgumentException
* @return Turnips
*/
public function findById(int $id): Turnips
{
$result = $this->adapter->findById($id);
if ($result === null) {
throw new InvalidArgumentException("找不到 ID 為「 $id 」的島嶼。");
}
return $this->mapRowToTurnips($result);
}
/**
* @param int $id
*
* @throws InvalidArgumentException
* @return Turnips
*/
public function findByIsland(string $island): Turnips
{
$result = $this->adapter->findByIsland($island);
if ($result === null) {
throw new InvalidArgumentException("找不到名稱為「 $island 」的島嶼。");
}
return $this->mapRowToTurnips($result);
}
/**
* @param array $row
*
* @return Turnips
*/
protected function mapRowToTurnips(array $row): Turnips
{
return Turnips::fromIsland($row);
}
}