Skip to content

Commit

Permalink
tidying up, use return types
Browse files Browse the repository at this point in the history
  • Loading branch information
TRPB committed Mar 10, 2019
1 parent 601ce1c commit 898d590
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion JokeList/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function output(\JokeSite\JokeList $model): string {
foreach ($model->getJokes() as $joke) {
$output .= '<li>' . $joke['text'];

$output .= ' <a href="index.php?edit=delete&amp;id=' . $joke['id'] . '">Edit</a>';
$output .= ' <a href="index.php?route=edit&amp;id=' . $joke['id'] . '">Edit</a>';
$output .= '<form action="index.php?route=delete" method="POST">
<input type="hidden" name="id" value="' . $joke['id'] . '" />
<input type="submit" value="Delete" />
Expand Down
27 changes: 12 additions & 15 deletions Model/JokeList.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
<?php
namespace JokeSite;
class JokeList {
/* database connection */
private $pdo;
/* sort method */
private $sort = 'oldest';
/* search keywork if set */
private $keyword;

public function __construct(\PDO $pdo) {
public function __construct(\PDO $pdo, string $sort = 'oldest', string $keyword = '') {
$this->pdo = $pdo;
$this->sort = $sort;
$this->keyword = $keyword;
}

public function sort($dir): self {
$model = clone $this;
$model->sort = $dir;
return $model;
return new self($this->pdo, $dir, $this->keyword);
}


public function search($keyword) {
$model = clone $this;

$model->keyword = $keyword;

return $model;

public function search($keyword): self {
return new self($this->pdo, $this->sort, $keyword);
}

public function getKeyword() {
public function getKeyword(): string {
return $this->keyword;
}

public function getSort() {
public function getSort(): string {
return $this->sort;
}

Expand All @@ -40,7 +37,7 @@ public function delete($id): self {
return $this;
}

public function getJokes() {
public function getJokes(): array {
$parameters = [];

if ($this->sort == 'newest') {
Expand Down
2 changes: 1 addition & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
$model = new \JokeSite\JokeList($pdo);
$view = new \JokeList\View();
}
if ($route == 'edit') {
else if ($route == 'edit') {
$model = new \JokeSite\JokeForm($pdo);
$controller = new \JokeForm\Controller();

Expand Down

0 comments on commit 898d590

Please sign in to comment.