Skip to content

Commit 1c83900

Browse files
committed
Table mapper created, BaseRepository created
1 parent a3c22ef commit 1c83900

File tree

15 files changed

+195
-48
lines changed

15 files changed

+195
-48
lines changed

app/Model/Mapper/BaseListTable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Model\Mapper;
6+
7+
abstract class BaseListTable
8+
{
9+
public int $id;
10+
}

app/Model/Mapper/Event.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Model\Mapper;
6+
7+
use App\Model\Mapper\Trait\CreatedAt;
8+
use App\Model\Mapper\Trait\EditedAt;
9+
use DateTime;
10+
11+
class Event extends BaseListTable
12+
{
13+
use CreatedAt;
14+
use EditedAt;
15+
16+
public string $title;
17+
18+
public DateTime|null $event_time;
19+
20+
public string|null $event_place;
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Model\Mapper\Trait;
6+
7+
use DateTime;
8+
9+
trait CreatedAt
10+
{
11+
public DateTime $createdAt;
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Model\Mapper\Trait;
6+
7+
use DateTime;
8+
9+
trait EditedAt
10+
{
11+
public DateTime $editedAt;
12+
}

app/Model/Mapper/User.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Model\Mapper;
6+
7+
use App\Model\Mapper\Trait\CreatedAt;
8+
use App\Model\Mapper\Trait\EditedAt;
9+
10+
class User extends BaseListTable
11+
{
12+
use CreatedAt;
13+
use EditedAt;
14+
15+
public string $email;
16+
17+
public string $password;
18+
}

app/Model/Repository/BaseRepository.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,82 @@
55
namespace App\Model\Repository;
66

77
use Dibi\Connection;
8+
use Dibi\Row;
9+
use ReflectionClass;
810

11+
/**
12+
* @template T of Row
13+
*/
914
abstract class BaseRepository
1015
{
16+
protected string $tableName;
17+
1118
public function __construct(
1219
protected Connection $dibi
1320
) {
21+
$reflect = new ReflectionClass($this);
22+
$repositoryName = str_replace('Repository', '', $reflect->getShortName());
23+
24+
$this->tableName = strtolower($repositoryName);
1425
}
26+
27+
/**
28+
* @return T
29+
*/
30+
public function find(mixed $id): Row|null
31+
{
32+
$result = $this->dibi->query("
33+
SELECT * FROM {$this->tableName} WHERE id=?
34+
",
35+
$id
36+
);
37+
38+
return $result->fetch();
39+
}
40+
41+
/**
42+
* @return T
43+
*/
44+
public function findOneBy(array $criteria, array|null $orderBy = null): Row|null
45+
{
46+
$query = "SELECT * FROM {$this->tableName} WHERE %and";
47+
48+
if ($orderBy) {
49+
$query = $query . " ORDER BY %by";
50+
}
51+
52+
$arguments = [
53+
$query,
54+
$criteria
55+
];
56+
57+
if ($orderBy) {
58+
$arguments[] = $orderBy;
59+
}
60+
61+
$result = $this->dibi->query(...$arguments);
62+
63+
return $result->fetch();
64+
}
65+
66+
/**
67+
* @return array<array-key, T>
68+
*/
69+
public function findAll(): array
70+
{
71+
$result = $this->dibi->query("
72+
SELECT * FROM {$this->tableName}
73+
");
74+
75+
return $result->fetchAll();
76+
}
77+
78+
public function getAllCount(): int
79+
{
80+
$result = $this->dibi->query("
81+
SELECT * FROM {$this->tableName}
82+
");
83+
84+
return $result->getRowCount();
85+
}
1586
}

app/Model/Repository/EventRepository.php

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,23 @@
44

55
namespace App\Model\Repository;
66

7+
use App\Model\Mapper\Event;
8+
9+
/**
10+
* @extends BaseRepository<Event>
11+
*/
712
class EventRepository extends BaseRepository
813
{
9-
private const TABLE = 'event';
10-
11-
public function findAll(): array
12-
{
13-
$result = $this->dibi
14-
->select('*')
15-
->from(self::TABLE);
16-
17-
return $result->fetchAssoc('id');
18-
}
19-
20-
public function getAllCount(): int
21-
{
22-
$result = $this->dibi
23-
->select('COUNT(id)')
24-
->from(self::TABLE);
25-
26-
return $result->fetchSingle();
27-
}
28-
2914
public function findAllForPaginator(int $limit, int $offset): array
3015
{
31-
$result = $this->dibi
32-
->select('*')
33-
->from(self::TABLE)
34-
->limit($limit)
35-
->offset($offset);
16+
$result = $this->dibi->query("
17+
SELECT * FROM {$this->tableName}
18+
LIMIT ?
19+
OFFSET ?
20+
",
21+
$limit,
22+
$offset
23+
);
3624

3725
return $result->fetchAssoc('id');
3826
}

app/Model/Repository/UserRepository.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@
44

55
namespace App\Model\Repository;
66

7-
use Dibi\Row;
7+
use App\Model\Mapper\User;
88

9+
/**
10+
* @extends BaseRepository<User>
11+
*/
912
class UserRepository extends BaseRepository
1013
{
11-
public function findOneBy(string $key, mixed $value): Row|null
12-
{
13-
$result = $this->dibi->query(
14-
"SELECT * FROM user WHERE {$key}=?",
15-
$value
16-
);
17-
18-
return $result->fetch();
19-
}
20-
}
14+
}

app/Model/Service/AuthenticationService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
*/
2424
public function authenticate(string $user, string $password): SimpleIdentity
2525
{
26-
$user = $this->userRepository->findOneBy('email', $user);
26+
$user = $this->userRepository->findOneBy(['email' => $user]);
2727

2828
if (!$user) {
2929
throw new AuthenticationException('Uzivatel nebyl nalezen');
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Module\Profile\Home;
6+
7+
use App\Module\BasePresenter;
8+
9+
class HomePresenter extends BasePresenter
10+
{
11+
}

0 commit comments

Comments
 (0)