/** * This is called before a value is returned from the model, so any * transformations from database format to PHP value should occur here. * * @param Model $model The model we're reading from * @param string $field The name of the field we're reading from * @param mixed $value The "raw" Value (read from database) * @return mixed The $value, with any changes made. */ public function get(Model $model, $field, $value) { if (is_numeric($value)) { $dt = new NativeDateTime(null, $this->options['timezone']); $dt->setTimestamp($value); } elseif ($value !== null) { $dt = new NativeDateTime($value, $this->options['timezone']); } else { $dt = $value; } return $dt; }
public function testGet() { $m = Model::factory('Solution10\\ORM\\Tests\\ActiveRecord\\Stubs\\User'); $zone = new DateTimeZone('Europe/London'); $d = new DateTime(['timezone' => $zone]); // Testing with a timestamp: $now = time(); $expected = (new NativeDateTime(null, $zone))->setTimestamp($now); $this->assertInstanceOf('\\DateTime', $d->get($m, 'created', $now)); $this->assertEquals($expected->format('Y-m-d H:i:s'), $d->get($m, 'created', $now)->format('Y-m-d H:i:s')); // Testing with a date style format from a db: $date = '2014-07-02'; $this->assertInstanceOf('\\DateTime', $d->get($m, 'created', $date)); $this->assertEquals($date, $d->get($m, 'created', $date)->format('Y-m-d')); // Testing with a datetime style format from a db: $date = '2014-07-02 08:45:32'; $this->assertInstanceOf('\\DateTime', $d->get($m, 'created', $date)); $this->assertEquals($date, $d->get($m, 'created', $date)->format('Y-m-d H:i:s')); }