Lumen opgezet met eenvoudige modelklasse
This commit is contained in:
23
common/classes/Model.php
Normal file
23
common/classes/Model.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Model;
|
||||
|
||||
class Model {
|
||||
protected function ConvertToDateTime(&$field) {
|
||||
if($field) {
|
||||
$field = new DateTime($field);
|
||||
if($field === false || $field->getTimestamp() == 0) {
|
||||
$field = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function __construct($data) {
|
||||
$class = get_class($this);
|
||||
foreach($data as $key => $val) {
|
||||
if(property_exists($class, $key)) {
|
||||
$this->$key = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
35
common/classes/NewsItem.php
Normal file
35
common/classes/NewsItem.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Model;
|
||||
|
||||
class NewsItem extends Model {
|
||||
public $id;
|
||||
public $title;
|
||||
public $content;
|
||||
public $published;
|
||||
public $edited;
|
||||
public $images;
|
||||
|
||||
public function __construct($data) {
|
||||
parent::__construct($data);
|
||||
parent::ConvertToDateTime($this->published);
|
||||
parent::ConvertToDateTime($this->edited);
|
||||
|
||||
//$this->image = new stdClass();
|
||||
//$this->image->url = "";
|
||||
//$this->image->title = $this->title;
|
||||
}
|
||||
|
||||
public function detailsUrl() {
|
||||
return "/{$this->id}/" . urlencode($this->title);
|
||||
}
|
||||
|
||||
public function excerpt() {
|
||||
$hasImages = count($this->images) > 0;
|
||||
$maxLength = $hasImages ? 200 : 500;
|
||||
return '<p class="news-excerpt ' . ($hasImages ? 'short' : 'long') . '">' .
|
||||
substr($this->content, 0, $maxLength) .
|
||||
(strlen($this->content) > $maxLength ? '...' : '') .
|
||||
'</p>';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user