46 lines
1.5 KiB
PHP
46 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Model;
|
|
|
|
class Model {
|
|
protected function ConvertToDateTime(&$field) {
|
|
if($field) {
|
|
// PHP7 JSON-encodes to {date: "", "timezone_type": ..., "timezone": "UTC" }
|
|
// In that case $field will be an object
|
|
if(is_object($field)) {
|
|
$field = ($field->timezone)
|
|
? new \DateTime($field->date, new \DateTimeZone($field->timezone))
|
|
: (new \DateTime($field->date));
|
|
} else {
|
|
// If $field is not an object, assume it's a plain, valid, string
|
|
$field = new \DateTime($field);
|
|
}
|
|
|
|
if($field === false || $field->getTimestamp() == 0) {
|
|
// If field had data but is invalid DateTime or is 0000-00-00 00:00, set it to null.
|
|
$field = null;
|
|
} else {
|
|
// If valid, return local timezone
|
|
$field->setTimezone(new \DateTimeZone("Europe/Amsterdam"));
|
|
}
|
|
}
|
|
}
|
|
|
|
public function __construct($data) {
|
|
$class = get_class($this);
|
|
foreach($data as $key => $val) {
|
|
if(property_exists($class, $key)) {
|
|
$this->$key = $val;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function url_slug($text) {
|
|
$text = strtolower($text);
|
|
$text = preg_replace('/\b([a-z]{1,3})\b/u', '', $text);
|
|
$text = preg_replace('/[^\w_\+\s]/', '', $text);
|
|
$text = preg_replace('/\s+/', '-', $text);
|
|
return trim(strtolower($text), '-');
|
|
}
|
|
}
|