Skip to content

Commit 28b1c0d

Browse files
committed
Create Item Entity to build products to store into DB
1 parent 199a48f commit 28b1c0d

File tree

6 files changed

+168
-32
lines changed

6 files changed

+168
-32
lines changed

src/Dal/FoodItemDal.php

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,56 @@
22

33
namespace PH7\ApiSimpleMenu\Dal;
44

5-
6-
use Ramsey\Uuid\Uuid;
5+
use PH7\ApiSimpleMenu\Entity\Item as ItemEntity;
76
use RedBeanPHP\R;
7+
88
class FoodItemDal
99
{
1010
public const TABLE_NAME = 'fooditems'; // Cannot have underscore. Use one word
1111

12-
public static function get(string $itemUuid): ?array
12+
public static function get(string $itemUuid): ItemEntity
1313
{
1414
$bindings = ['itemUuid' => $itemUuid];
1515
$itemBean = R::findOne(self::TABLE_NAME, 'item_uuid = :itemUuid', $bindings);
1616

17-
return $itemBean?->export();
17+
return (new ItemEntity())->unserialize($itemBean?->export());
1818
}
1919

2020
public static function getAll(): array
2121
{
22-
return R::findAll(self::TABLE_NAME);
22+
$itemsBean = R::findAll(self::TABLE_NAME);
23+
24+
$areAnyItems = $itemsBean && count($itemsBean);
25+
26+
if (!$areAnyItems) {
27+
// if no items found, return empty array
28+
return [];
29+
}
30+
31+
return array_map(
32+
function (object $itemBean): array {
33+
$itemEntity = (new ItemEntity())->unserialize($itemBean?->export());
34+
35+
// Select the fields we want to export and give back to the client
36+
return [
37+
'foodUuid' => $itemEntity->getItemUuid(),
38+
'name' => $itemEntity->getName(),
39+
'price' => $itemEntity->getPrice(),
40+
'available' => $itemEntity->getAvailable()
41+
];
42+
}, $itemsBean);
2343
}
2444

25-
public static function createDefaultItem(): int|string
45+
public static function createDefaultItem(ItemEntity $itemEntity): int|string
2646
{
2747
$itemBan = R::dispense(self::TABLE_NAME);
2848

29-
$itemBan->item_uuid = Uuid::uuid4()->toString();
30-
$itemBan->name = 'Burrito Chips';
31-
$itemBan->price = 19.55;
32-
$itemBan->available = true;
49+
// TODO Move this into Food Entity to build the item
50+
51+
$itemBan->item_uuid = $itemEntity->getItemUuid();
52+
$itemBan->name = $itemEntity->getName();
53+
$itemBan->price = $itemEntity->getPrice();
54+
$itemBan->available = $itemEntity->getAvailable();
3355

3456
// return the increment entry ID
3557
return R::store($itemBan);

src/Dal/UserDal.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,19 @@ public static function getAll(): ?array
9393
return []; // guard clause approach
9494
}
9595

96-
return array_map(function (object $userBean): array {
97-
$userEntity = (new UserEntity())->unserialize($userBean?->export());
98-
// Retrieve the User entity fields we want to expose to the client
99-
return [
100-
'userUuid' => $userEntity->getUserUuid(),
101-
'first' => $userEntity->getFirstName(),
102-
'last' => $userEntity->getLastName(),
103-
'email' => $userEntity->getEmail(),
104-
'phone' => $userEntity->getPhone(),
105-
'creationDate' => $userEntity->getCreationDate()
106-
];}, $usersBean);
96+
return array_map(function (object $userBean): array
97+
{
98+
$userEntity = (new UserEntity())->unserialize($userBean?->export());
99+
// Retrieve the User entity fields we want to expose to the client
100+
return [
101+
'userUuid' => $userEntity->getUserUuid(),
102+
'first' => $userEntity->getFirstName(),
103+
'last' => $userEntity->getLastName(),
104+
'email' => $userEntity->getEmail(),
105+
'phone' => $userEntity->getPhone(),
106+
'creationDate' => $userEntity->getCreationDate()
107+
];
108+
}, $usersBean);
107109
}
108110

109111
public static function remove(string $userUuid): bool

src/Entity/Entitable.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace PH7\ApiSimpleMenu\Entity;
4+
5+
interface Entitable
6+
{
7+
public function unserialize(?array $data): self;
8+
9+
public function setSequentialId(int $sequentialId): void;
10+
11+
public function getSequentialId(): int;
12+
}

src/Entity/Item.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace PH7\ApiSimpleMenu\Entity;
4+
5+
class Item implements Entitable
6+
{
7+
private int $sequentialId;
8+
9+
private ?string $itemUuid = null;
10+
11+
private string $name;
12+
13+
private float $price;
14+
15+
private bool $available;
16+
17+
public function setSequentialId(int $sequentialId): void
18+
{
19+
$this->sequentialId = $sequentialId;
20+
}
21+
22+
public function getSequentialId(): int
23+
{
24+
return $this->sequentialId;
25+
}
26+
27+
public function setItemUuid(string $itemUuid): void
28+
{
29+
$this->itemUuid = $itemUuid;
30+
}
31+
32+
public function getItemUuid(): ?string
33+
{
34+
return $this->itemUuid;
35+
}
36+
37+
public function setName(string $name): void
38+
{
39+
$this->name = $name;
40+
}
41+
42+
public function getName(): string
43+
{
44+
return $this->name;
45+
}
46+
47+
public function setPrice(float $price): void
48+
{
49+
$this->price = $price;
50+
}
51+
52+
public function getPrice(): float
53+
{
54+
return $this->price;
55+
}
56+
57+
public function setAvailable(bool $available): void
58+
{
59+
$this->available = $available;
60+
}
61+
62+
public function getAvailable(): bool
63+
{
64+
return $this->available;
65+
}
66+
67+
public function unserialize(?array $data): self
68+
{
69+
if (!empty($data['id'])) {
70+
$this->setSequentialId($data['id']);
71+
}
72+
73+
if (!empty($data['item_uuid'])) {
74+
$this->setItemUuid($data['item_uuid']);
75+
}
76+
77+
if (!empty($data['name'])) {
78+
$this->setName($data['name']);
79+
}
80+
81+
if (!empty($data['price'])) {
82+
$this->setPrice($data['price']);
83+
}
84+
85+
if (!empty($data['available'])) {
86+
$this->setAvailable($data['available']);
87+
}
88+
89+
return $this;
90+
}
91+
}

src/Entity/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace PH7\ApiSimpleMenu\Entity;
44

5-
class User
5+
class User implements Entitable
66
{
77
private int $sequentialId;
88
private ?string $userUuid = null;

src/Service/FoodItem.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace PH7\ApiSimpleMenu\Service;
44

55
use PH7\ApiSimpleMenu\Dal\FoodItemDal;
6+
use PH7\ApiSimpleMenu\Entity\Item as ItemEntity;
67
use PH7\ApiSimpleMenu\Validation\Exception\InvalidValidationException;
8+
use Ramsey\Uuid\Uuid;
79
use Respect\Validation\Validator as v;
810

911
class FoodItem
@@ -12,10 +14,14 @@ public function retrieve(string $itemUuid): array
1214
{
1315
if (v::uuid()->validate($itemUuid)) {
1416
if ($item = FoodItemDal::get($itemUuid)) {
15-
// Removing fields we don't want to expose
16-
unset($item['id']);
17-
18-
return $item;
17+
if ($item->getItemUuid()) {
18+
return [
19+
'itemUuid' => $item->getItemUuid(),
20+
'name' => $item->getName(),
21+
'price' => $item->getPrice(),
22+
'available' => $item->getAvailable()
23+
];
24+
}
1925
}
2026

2127
return [];
@@ -30,17 +36,20 @@ public function retrieveAll(): array
3036

3137
if (count($items) === 0) {
3238
// if no items have been added yet, create the first one
33-
FoodItemDal::createDefaultItem();
39+
$itemUuid = Uuid::uuid4()->toString();
40+
$itemEntity = new ItemEntity();
41+
$itemEntity->setItemUuid($itemUuid);
42+
$itemEntity->setName('Burrito Cheese with French Fries');
43+
$itemEntity->setPrice(19.99);
44+
$itemEntity->setAvailable(true);
45+
46+
FoodItemDal::createDefaultItem($itemEntity);
3447

3548
// then, get again all items
3649
// to retrieve the new one that just got added
3750
$items = FoodItemDal::getAll();
3851
}
3952

40-
return array_map(function (object $item): object {
41-
// Remove unnecessary "id" field
42-
unset($item['id']);
43-
return $item;
44-
}, $items);
53+
return $items;
4554
}
4655
}

0 commit comments

Comments
 (0)