diff --git a/todos/php/README.md b/todos/php/README.md new file mode 100644 index 0000000..da7b130 --- /dev/null +++ b/todos/php/README.md @@ -0,0 +1,14 @@ +# TODO-MVP in PHP + +This implementation is written in [PHP](https://www.php.net). + +- PHP version 7.3+. + +HTML copied from Ruby implementation. + +## Instructions + +1. Clone the repository +2. Requires PHP installed. +3. From the php/ directory run: `php -S localhost:8000` +4. Open browser to http://localhost:8000 diff --git a/todos/php/autoload.php b/todos/php/autoload.php new file mode 100644 index 0000000..4355ae0 --- /dev/null +++ b/todos/php/autoload.php @@ -0,0 +1,33 @@ + '/classes/' + ]; + + foreach($loaders as $prefix => $base_dir){ + $len = strlen($prefix); + if(strncmp($prefix, $class,$len) !== 0){ + continue; + } + $relative_class = substr($class, $len); + + $file = __DIR__ . $base_dir . + str_replace('\\', '/', + $relative_class) . + '.php'; + + if (file_exists($file)) { + require $file; + return; + } + } //end foreach +}); diff --git a/todos/php/classes/controller.php b/todos/php/classes/controller.php new file mode 100644 index 0000000..693beef --- /dev/null +++ b/todos/php/classes/controller.php @@ -0,0 +1,75 @@ +requestPost = $post; + $this->requestGet = $get; + } + + /** + * Route: / or /index + * If a http POST has been received, add the item, otherwise list items. + * + * @param todo $todo + * @return void + */ + public function index(todo &$todo){ + if (isset($this->requestPost['item'])) { + $todo->addItem($this->requestPost['item']); + } + return 'page'; + } + + /** + * Route: /done + * + * @param todo $todo + * @return void + */ + public function done(todo &$todo){ + if (isset($this->requestPost['item'])) { + $todo->setItemDone($this->requestPost['item']); + } + return 'page'; + } + + /** + * Route: /notdone + * + * @param todo $todo + * @return void + */ + public function notdone(todo &$todo){ + if (isset($this->requestPost['item'])) { + $todo->setItemNotDone($this->requestPost['item']); + } + return 'page'; + } + + /** + * Route: /delete + * + * @param todo $todo + * @return string + */ + public function delete(todo &$todo){ + if (isset($this->requestPost['item'])) { + $todo->delete($this->requestPost['item']); + } + return 'page'; + } +} diff --git a/todos/php/classes/todo.php b/todos/php/classes/todo.php new file mode 100644 index 0000000..d7cee75 --- /dev/null +++ b/todos/php/classes/todo.php @@ -0,0 +1,86 @@ +items = $list; + } + + /** + * Add a new todo item, default to not done. + * + * @param string $name + * @return void + */ + public function addItem(string $name) + { + $this->items[] = array('name' => $name, 'done' => 0); + } + + /** + * Delete an item at the given number. + * + * @param int $num + * @return void + */ + public function delete(int $num) + { + unset($this->items[$num]); + } + + /** + * Fetch our current list of todo items as an array. + * + * @return array + */ + public function getItems(): array + { + return $this->items; + } + + /** + * Get our array of current items as a JSON string. + * For passing to Javascript or persistence in hidden form fields. + * + * @return string + */ + public function getItemsJSON(): string + { + return json_encode($this->items); + } + + /** + * Mark a todo item as complete. + * + * @param integer $num + * @return void + */ + public function setItemDone(int $num) + { + $this->items[$num]['done'] = 1; + } + + /** + * Mark a todo item as incomplete. + * + * @param integer $num + * @return void + */ + public function setItemNotDone(int $num) + { + $this->items[$num]['done'] = 0; + } +} diff --git a/todos/php/index.php b/todos/php/index.php new file mode 100644 index 0000000..628eeb4 --- /dev/null +++ b/todos/php/index.php @@ -0,0 +1,46 @@ +Return home."); + exit(); +} + +// Set up values for our 'template': +$tpl_todos = $todoList->getItems(); +$tpl_currentList = htmlspecialchars($todoList->getItemsJSON()); + +// Include our html page template, which uses the above values. +require_once($template.'.tpl.php'); diff --git a/todos/php/page.tpl.php b/todos/php/page.tpl.php new file mode 100644 index 0000000..d118fde --- /dev/null +++ b/todos/php/page.tpl.php @@ -0,0 +1,89 @@ + + +
+