Last active
October 6, 2019 14:48
-
-
Save lolychank/90f222e62985d012213175fce89ae209 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TaskRepository | |
{ | |
// __construct() | |
public function getById(Id $id): TaskView | |
{ | |
$sql = ' | |
SELECT | |
t.id, | |
t.name, | |
t.project_id AS project, | |
GROUP_CONCAT(sbt.name) AS subTasks, | |
t.date | |
FROM | |
tasks t | |
LEFT JOIN | |
sub_tasks sbt | |
WHERE | |
id = :id | |
'; | |
$stmt = $this->connection->prepare($sql); | |
$stmt->execute(['id' => $id->getValue()]); | |
$stmt->setFetchMode(\PDO::FETCH_CLASS, TaskView::class); | |
$task = $stmt->fetch(); | |
if (!$task) { | |
throw new EntityNotFoundException(); | |
} | |
return $task; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TaskView | |
{ | |
private $id; | |
private $name; | |
private $project; | |
/** | |
* @var array | |
*/ | |
private $subTasks; | |
/** | |
* @var \DateTimeImmutable | |
*/ | |
private $date; | |
public function getId(): string | |
{ | |
return $this->id; | |
} | |
public function getName(): string | |
{ | |
return $this->name; | |
} | |
public function getProjectId(): string | |
{ | |
return $this->project; | |
} | |
public function getSubTasks(): array | |
{ | |
return \explode(',', $this->subTasks); | |
} | |
public function getDate(): \DateTimeImmutable | |
{ | |
return \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $this->date); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment