<?php
class Filters
{
/**
* @return array
*/
public function toArray()
{
return array_map(function ($value) {
return json_encode($value);
}, get_object_vars($this));
}
/**
* @param array $arrayOfFilters
* @return Filters
*/
public static function fromArray($arrayOfFilters)
{
$instance = new self;
foreach ($arrayOfFilters as $key => $value) {
$instance->{$key} = $value;
}
return $instance;
}
}
final class UserFilters extends Filters
{
/** @var array */
private $registeredAtRange;
/**
* @param \DateTimeInterface $start
* @param \DateTimeInterface|null $end
*/
public function setRegisteredAtRange(\DateTimeInterface $start, \DateTimeInterface $end = null)
{
if (is_null($end)) {
$end = new \DateTime;
}
$this->registeredAtRange = [
'start' => $start->format('Y-m-d H:i:s'),
'end' => $end->format('Y-m-d H:i:s'),
];
}
/**
* Will return 2 date time strings or null.
*
* @return array|null
*/
public function getRegisteredAtRange()
{
return $this->registeredAtRange;
}
}
$a = new UserFilters;
$a->setRegisteredAtRange(new DateTime('now'));
$b = $a->toArray();
var_dump($b);
var_dump(UserFilters::fromArray($b));
- Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
- array(0) {
}
object(Filters)#3 (0) {
}
- Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.28
- array(0) {
}
object(Filters)#2 (0) {
}
preferences:
191.62 ms | 401 KiB | 234 Q