Maslosoft Manganel API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/manganel
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
* @link https://maslosoft.com/manganel/
*/
namespace Maslosoft\Manganel\Sanitizers;
use DateTime;
use Maslosoft\Mangan\Sanitizers\DateSanitizer;
use MongoDate;
/**
* UnixDateSanitizer
*
* This sanitizer allow storing date in elasticsearch in string format,
* while reading it as MongoDate object.
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class DateWriteEsSanitizer extends DateSanitizer
{
const ISODate = 'Y-m-d*H:i:s*';
public function read($model, $dbValue)
{
// Don't touch
if ($dbValue instanceof MongoDate)
{
return $dbValue;
}
// Assume current time
if ((int) $dbValue === 0 || empty($dbValue))
{
$dbValue = time();
}
// Assume timestamp
if (is_int($dbValue) || (is_string($dbValue) && preg_match('~^\d+$~', $dbValue)))
{
return new MongoDate((int) $dbValue);
}
elseif (is_string($dbValue))
{
// Assume ISO date
$dt = DateTime::createFromFormat(DateTime::ISO8601, $dbValue);
if (empty($dt))
{
return new MongoDate(time());
}
$time = $dt->format('U');
return new MongoDate($time);
}
// Create from date time string
$dt = new DateTime($dbValue);
$time = $dt->format('U');
return new MongoDate($time);
}
public function write($model, $dbValue)
{
if ($dbValue instanceof MongoDate)
{
$time = $dbValue->sec;
}
elseif (!empty($time))
{
$time = $dbValue;
}
else
{
$time = time();
}
if (is_int($time))
{
$dt = DateTime::createFromFormat('U', $time);
}
else
{
$dt = DateTime::createFromFormat('c', $time);
}
return date('c', (int) (new MongoDate((int) $time))->sec);
}
}
API documentation generated by ApiGen