diff options
| author | David T. Sadler <davidtsadler@googlemail.com> | 2021-09-18 12:20:59 +0100 |
|---|---|---|
| committer | David T. Sadler <davidtsadler@googlemail.com> | 2021-09-18 12:20:59 +0100 |
| commit | 2b15ee9eae6a98fba871b4c587cdd8cfc1b1b48a (patch) | |
| tree | e3ceb3a87c11b925f0df0088da82427f793b2e14 | |
| parent | 3431dc10bc8c9925dcda272855de379321d832b2 (diff) | |
Beginning of rewrite
| -rw-r--r-- | autoload.php | 14 | ||||
| -rw-r--r-- | public/index.php | 27 | ||||
| -rw-r--r-- | src/DTS/Bookmark.php | 27 | ||||
| -rw-r--r-- | src/DTS/BookmarkRepository.php | 87 |
4 files changed, 155 insertions, 0 deletions
diff --git a/autoload.php b/autoload.php new file mode 100644 index 0000000..cb673fa --- /dev/null +++ b/autoload.php @@ -0,0 +1,14 @@ +<?php + +declare(strict_types=1); + +spl_autoload_register(function (string $class) +{ + $base_dir = __DIR__ . '/src/'; + + $file = $base_dir . str_replace('\\', '/', $class) . '.php'; + + if (file_exists($file)) { + require $file; + } +}); diff --git a/public/index.php b/public/index.php new file mode 100644 index 0000000..df80ef3 --- /dev/null +++ b/public/index.php @@ -0,0 +1,27 @@ +<?php + +declare(strict_types=1); + +use DTS\BookmarkRepository; + +require_once(__DIR__.'/../autoload.php'); + +$config = require_once(__DIR__.'/../config.php'); + +$sort = $_GET['sort'] ?? null; + +$tag = $_GET['tag'] ?? null; + +$bookmarks = new BookmarkRepository($config['path_to_repository']); + +$bookmarks->load(); + +if ($tag !== null) { + $bookmarks->filter($tag); +} + +$bookmarks->sort($sort === 'asc'); + +foreach ($bookmarks as $bookmark) { + echo $bookmark->url.' '.$bookmark->addedAt."<br/>"; +} diff --git a/src/DTS/Bookmark.php b/src/DTS/Bookmark.php new file mode 100644 index 0000000..93c7a29 --- /dev/null +++ b/src/DTS/Bookmark.php @@ -0,0 +1,27 @@ +<?php + +declare(strict_types=1); + +namespace DTS; + +class Bookmark +{ + public string $id; + + public string $url; + + public string $tag; + + public string $addedAt; + + public bool $read; + + function __construct(string $id, string $url, string $tag, string $addedAt, bool $read) + { + $this->id = $id; + $this->url = $url; + $this->tag = $tag; + $this->addedAt = $addedAt; + $this->read = $read; + } +} diff --git a/src/DTS/BookmarkRepository.php b/src/DTS/BookmarkRepository.php new file mode 100644 index 0000000..deb188c --- /dev/null +++ b/src/DTS/BookmarkRepository.php @@ -0,0 +1,87 @@ +<?php + +declare(strict_types=1); + +namespace DTS; + +class BookmarkRepository implements \Iterator +{ + private string $pathToRepository; + + private array $repository = []; + + private int $position = 0; + + function __construct(string $pathToRepository) + { + $this->pathToRepository = $pathToRepository; + } + + public function load(): void + { + if (!is_file($this->pathToRepository) || !is_readable($this->pathToRepository)) { + throw new \Exception("Unable to locate repository {$this->pathToRepository}"); + } + + if (($fp = fopen($this->pathToRepository, 'r')) === FALSE) { + throw new \Exception("Unable to read from repository {$this->pathToRepository}"); + } + + while (($data = fgetcsv($fp)) !== FALSE) { + $this->repository[] = new Bookmark( + $data[0], // Id. + $data[1], // Url. + $data[2], // Tag. + $data[3], // Added At. + (bool)$data[4], // Read. + ); + } + + fclose($fp); + } + + public function sort(bool $asc = true): BookmarkRepository + { + usort($this->repository, function ($a, $b) use ($asc) { + return $asc ? $a->addedAt <=> $b->addedAt + : $b->addedAt <=> $a->addedAt; + }); + + return $this; + } + + public function filter(string $tag): BookmarkRepository + { + $this->repository = array_filter( + $this->repository, + fn($bookmark) => $bookmark->tag === $tag + ); + + return $this; + } + + public function current(): mixed + { + return $this->repository[$this->position]; + } + + public function key(): mixed + { + return $this->position; + } + + public function next(): void + { + ++$this->position; + } + + public function rewind(): void + { + $this->position = 0; + } + + public function valid(): bool + { + return isset($this->repository[$this->position]); + } +} |
