Last active
November 3, 2017 09:50
-
-
Save cblanquera/17ebdfdfc7f6e7c898a61fe26d0320ce to your computer and use it in GitHub Desktop.
Training
This file contains hidden or 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 | |
/** | |
* This is a generic Article renderer | |
* | |
* @author Christian Blanquera <[email protected]> | |
* @standard PSR-2 | |
*/ | |
class Article extends Post { | |
/** | |
* @const WEBSITE | |
*/ | |
const WEBSITE = 'http://wordpress.com'; | |
/** | |
* @int $count | |
*/ | |
public static $count = 0; | |
/** | |
* @string|null $url | |
*/ | |
private $url = null; | |
protected static $database = null; | |
protected $id = null; | |
protected $created = null; | |
protected $updated = null; | |
/** | |
* Self instantiator | |
* | |
* @param string|null $title This is the title | |
* @param string|null $detail This is the detail | |
* | |
* @return Article | |
*/ | |
public static function getInstance($title = null, $detail = null) | |
{ | |
$class = __CLASS__; | |
return new $class($title, $detail); | |
} | |
/** | |
* Constructor | |
* | |
* @param string|null $title | |
* @param string|null $detail | |
*/ | |
public function __construct($title = null, $detail = null) | |
{ | |
parent::__construct($title, $detail); | |
self::$count++; | |
} | |
/** | |
* Returns the count of all the articles made | |
* | |
* @return int | |
*/ | |
public function getCount() | |
{ | |
return self::$count; | |
} | |
/** | |
* Sets the title | |
* | |
* @param string | |
* | |
* @return Article | |
*/ | |
public function setTitle($title) | |
{ | |
parent::setTitle($title); | |
$this->setUrl($this->title); | |
return $this; | |
} | |
/** | |
* Returns the URL | |
* | |
* @return string | |
*/ | |
public function getUrl() | |
{ | |
return $this->url; | |
} | |
/** | |
* Returns a rendered version of the Article | |
* | |
* @return string | |
*/ | |
public function render() | |
{ | |
return parseTemplate(__DIR__ . '/template.html', [ | |
'id' => $this->id, | |
'title' => $this->getTitle(), | |
'website' => self::WEBSITE, | |
'url' => $this->getUrl(), | |
'detail' => $this->getDetail() | |
]); | |
} | |
public static function setDatabase($database) | |
{ | |
self::$database = $database; | |
} | |
public static function search() | |
{ | |
return self::$database->search('article'); | |
} | |
public function load($id) | |
{ | |
$results = self::$database->get('article', $id); | |
if(!is_array($results)) { | |
return $this; | |
} | |
$this->id = $results['article_id']; | |
$this->title = $results['article_title']; | |
$this->url = $results['article_url']; | |
$this->detail = $results['article_detail']; | |
$this->created = $results['article_created']; | |
$this->updated = $results['article_updated']; | |
return $this; | |
} | |
public function save() | |
{ | |
//if there's an id | |
if(is_numeric($this->id)) { | |
//update | |
self::$database->update('article', $this->id, [ | |
'article_title' => $this->title, | |
'article_url' => $this->url, | |
'article_detail' => $this->detail, | |
'article_updated' => date('Y-m-d H:i:s') | |
]); | |
return $this; | |
} | |
//other wise insert | |
self::$database->create('article', [ | |
'article_title' => $this->title, | |
'article_url' => $this->url, | |
'article_detail' => $this->detail, | |
'article_created' => date('Y-m-d H:i:s'), | |
'article_updated' => date('Y-m-d H:i:s') | |
]); | |
return $this; | |
} | |
public function remove() | |
{ | |
self::$database->remove('article', $this->id); | |
return $this; | |
} | |
/** | |
* Formats the URL | |
* | |
* @param string | |
*/ | |
private function setUrl($title) | |
{ | |
$this->url = preg_replace('/[^a-zA-Z0-9\ ]/', '', $title); | |
$this->url = str_replace(' ', '-', $this->url); | |
$this->url = strtolower($this->url); | |
} | |
} |
This file contains hidden or 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 | |
include __DIR__ . '/helpers.php'; | |
bootstrap(); |
This file contains hidden or 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 Database | |
{ | |
protected $name = null; | |
protected $user = null; | |
protected $password = null; | |
protected $connection = null; | |
public function __construct($name, $user, $password) | |
{ | |
$this->name = $name; | |
$this->user = $user; | |
$this->password = $password; | |
} | |
public function create($table, array $values) | |
{ | |
$keys = array_keys($values); | |
$questions = array_fill(0, count($values), '?'); | |
return $this->query( | |
'INSERT INTO ' . $table . ' ' | |
. '(' . implode(', ', $keys) . ')' | |
. 'VALUES (' . implode(', ', $questions) . ')', | |
array_values($values) | |
); | |
} | |
public function update($table, $id, array $values) | |
{ | |
$set = []; | |
foreach($values as $key => $value) { | |
$set[] = $key . ' = ?'; | |
} | |
$primary = $table . '_id'; | |
$values = array_values($values); | |
$values[] = $id; | |
return $this->query( | |
'UPDATE ' . $table . ' SET ' | |
. implode(', ', $set) | |
. ' WHERE ' . $primary . ' = ?', | |
$values | |
); | |
} | |
public function remove($table, $id) | |
{ | |
$primary = $table . '_id'; | |
return $this->query( | |
'DELETE FROM ' . $table | |
. ' WHERE ' . $primary . ' = ?', | |
[$id] | |
); | |
} | |
public function search($table) | |
{ | |
return $this->query('SELECT * FROM ' . $table); | |
} | |
public function get($table, $id) | |
{ | |
$results = $this->query( | |
'SELECT * FROM ' . $table | |
. ' WHERE ' . $primary . ' = ?', | |
[$id] | |
); | |
if(!isset($results[0])) { | |
return null; | |
} | |
return $results[0]; | |
} | |
public function query($query, array $values = []) | |
{ | |
if(is_null($this->connection)) { | |
$template = 'mysql:dbname={NAME};host=127.0.0.1'; | |
$template = str_replace('{NAME}', $this->name, $template); | |
$this->connection = new PDO($template, $this->user, $this->password); | |
} | |
$query = $this->connection->prepare($query); | |
$query->execute($values); | |
return $query->fetchAll(PDO::FETCH_ASSOC); | |
} | |
} |
This file contains hidden or 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
<form method="post" action="basics.php"> | |
<div> | |
<label>Title</label> | |
<input type="text" name="article_title" value="{TITLE}" /> | |
</div> | |
<div> | |
<label>Detail</label> | |
<textarea name="article_detail" rows="10">{DETAIL}</textarea> | |
</div> | |
<input type="hidden" name="article_id" value="{ID}" /> | |
<button type="submit">Create</button> | |
</form> |
This file contains hidden or 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 | |
include __DIR__ . '/Database.php'; | |
include __DIR__ . '/PostInterface.php'; | |
include __DIR__ . '/Post.php'; | |
include __DIR__ . '/Article.php'; | |
function parseTemplate($file, $variables) | |
{ | |
$template = file_get_contents($file); | |
foreach($variables as $key => $value) { | |
$template = str_replace('{'.strtoupper($key).'}', $value, $template); | |
} | |
return $template; | |
} | |
function create() | |
{ | |
if(!empty($_POST) && !is_numeric($_POST['article_id'])) { | |
Article::getInstance() | |
->setTitle($_POST['article_title']) | |
->setDetail($_POST['article_detail']) | |
->save(); | |
} | |
} | |
function remove() | |
{ | |
if(isset($_GET['remove']) && is_numeric($_GET['remove'])) { | |
Article::getInstance() | |
->load($_GET['remove']) | |
->remove(); | |
} | |
} | |
function update() | |
{ | |
if(!empty($_POST) && is_numeric($_POST['article_id'])) { | |
Article::getInstance() | |
->load($_POST['article_id']) | |
->setTitle($_POST['article_title']) | |
->setDetail($_POST['article_detail']) | |
->save(); | |
} | |
} | |
function bootstrap() | |
{ | |
Article::setDatabase(new Database('blog', 'root', '')); | |
remove(); | |
$form = [ | |
'id' => null, | |
'title' => null, | |
'detail' => null | |
]; | |
if(isset($_GET['update']) && is_numeric($_GET['update'])) { | |
$article = Article::getInstance()->load($_GET['update']); | |
$form = [ | |
'id' => $_GET['update'], | |
'title' => $article['title'], | |
'detail' => $article['detail'] | |
]; | |
} | |
create(); | |
update(); | |
echo parseTemplate(__DIR__ . '/form.html', $form); | |
foreach(Article::search() as $article) { | |
echo Article::getInstance()->load($article['article_id']); | |
} | |
} |
This file contains hidden or 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 | |
/** | |
* This is a generic Post | |
* | |
* @author Christian Blanquera <[email protected]> | |
* @standard PSR-2 | |
*/ | |
abstract class Post implements PostInterface, ArrayAccess { | |
/** | |
* @string|null $title | |
*/ | |
protected $title = null; | |
/** | |
* @string|null $detail | |
*/ | |
public $detail = null; | |
/** | |
* Returns a rendered version of the Article | |
* | |
* @return string | |
*/ | |
abstract public function render(); | |
/** | |
* Constructor | |
* | |
* @param string|null $title | |
* @param string|null $detail | |
*/ | |
public function __construct($title = null, $detail = null) | |
{ | |
$this | |
->setTitle($title) | |
->setDetail($detail); | |
} | |
/** | |
* Converts object to string | |
* | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
return $this->render(); | |
} | |
/** | |
* Returns the title | |
* | |
* @return string | |
*/ | |
public function getTitle() | |
{ | |
return $this->title; | |
} | |
/** | |
* Sets the title | |
* | |
* @param string | |
* | |
* @return Article | |
*/ | |
public function setTitle($title) | |
{ | |
$this->title = $title; | |
return $this; | |
} | |
/** | |
* Returns the detail | |
* | |
* @return string | |
*/ | |
public function getDetail() | |
{ | |
return $this->detail; | |
} | |
/** | |
* Sets the detail | |
* | |
* @param string | |
* | |
* @return Article | |
*/ | |
public function setDetail($detail) | |
{ | |
$this->detail = $detail; | |
return $this; | |
} | |
public function offsetSet($offset, $value) | |
{ | |
$method = 'set' . ucfirst($offset); | |
if(method_exists($this, $method)) { | |
$this->$method($value); | |
} | |
} | |
public function offsetExists($offset) | |
{ | |
return isset($this->$offset) || is_null($this->$offset); | |
} | |
public function offsetUnset($offset) | |
{ | |
if($this->offsetExists($offset)) { | |
$this->$offset = null; | |
} | |
} | |
public function offsetGet($offset) | |
{ | |
$method = 'get' . ucfirst($offset); | |
if(method_exists($this, $method)) { | |
return $this->$method($value); | |
} | |
return null; | |
} | |
} |
This file contains hidden or 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 | |
/** | |
* This is a generic Post | |
* | |
* @author Christian Blanquera <[email protected]> | |
* @standard PSR-2 | |
*/ | |
interface PostInterface { | |
/** | |
* Returns the title | |
* | |
* @return string | |
*/ | |
public function getTitle(); | |
/** | |
* Sets the title | |
* | |
* @param string | |
* | |
* @return Article | |
*/ | |
public function setTitle($title); | |
/** | |
* Returns the detail | |
* | |
* @return string | |
*/ | |
public function getDetail(); | |
/** | |
* Sets the detail | |
* | |
* @param string | |
* | |
* @return Article | |
*/ | |
public function setDetail($detail); | |
} |
This file contains hidden or 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
<h3>{TITLE}</h3> | |
<a href="{WEBSITE}/{URL}">{URL}</a> | |
| <a href="?update={ID}">Edit</a> | |
| <a href="?remove={ID}">Remove</a> | |
<p>{DETAIL}</p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment