Hoa central
Date.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Realdom;
38 
39 use Hoa\Math;
40 
49 class Date extends Realdom
50 {
56  const NAME = 'date';
57 
64  protected static $_constants = null;
65 
71  protected $_arguments = [
72  'RealdomString format' => 'c',
73  'Integer timestamp' => -1
74  ];
75 
76 
77 
83  protected function construct()
84  {
85  if (null === static::$_constants) {
86  $reflection = new \ReflectionClass('DateTime');
87  static::$_constants = $reflection->getConstants();
88  }
89 
90  $constants = &static::$_constants;
91  $this['format']['value'] = preg_replace_callback(
92  '#__(\w+)__#',
93  function (Array $matches) use (&$constants) {
94  $c = $matches[1];
95 
96  if (!isset($constants[$c])) {
97  return $matches[0];
98  }
99 
100  return $constants[$c];
101  },
102  $this['format']->getConstantValue()
103  );
104 
105  if ($this['timestamp'] instanceof Constinteger &&
106  -1 === $this['timestamp']->getConstantValue()) {
107  $this['timestamp'] = new Constinteger(time());
108  }
109 
110  return;
111  }
112 
119  protected function _predicate($q)
120  {
121  $format = $this['format']->getConstantValue();
122 
123  try {
124  $datetime = \DateTime::createFromFormat($format, $q);
125  } catch (\Exception $e) {
126  return false;
127  }
128 
129  return
130  false !== $datetime &&
131  $this['timestamp']->predicate(intval($datetime->format('U')));
132  }
133 
140  protected function _sample(Math\Sampler $sampler)
141  {
142  return date(
143  $this['format']->getConstantValue(),
144  $this['timestamp']->sample($sampler)
145  );
146  }
147 }
_sample(Math\Sampler $sampler)
Definition: Date.php:140
_predicate($q)
Definition: Date.php:119
sample(Math\Sampler $sampler=null)
Definition: Realdom.php:686
const NAME
Definition: Date.php:56
static $_constants
Definition: Date.php:64