public function visitDuration($data, Property\DurationType $property, $path) { if ($this->validate) { parent::visitDuration($data, $property, $path); } else { $this->assertRequired($data, $property, $path); } if ($data instanceof \DateInterval) { return $this->createSimpleProperty(Duration::fromDateInterval($data), $property); } try { return $this->createSimpleProperty(new Duration($data), $property); } catch (\Exception $e) { throw new ValidationException($path . ' must be an valid duration format [ISO8601]'); } }
public function testFromDateInterval() { $this->assertEquals('P2015Y4M25DT19H35M20S', Duration::fromDateInterval(new DateInterval('P2015Y4M25DT19H35M20S'))->toString()); $this->assertEquals('PT60S', Duration::fromDateInterval(new DateInterval('PT60S'))->toString()); }
public function visitDuration($data, Property\DurationType $property, $path) { $this->assertRequired($data, $property, $path); if ($data === null) { return true; } elseif ($data instanceof \DateInterval) { return true; } elseif (is_string($data)) { $result = preg_match('/^' . DateTime\Duration::getPattern() . '$/', $data); if ($result) { return true; } } throw new ValidationException($path . ' must be an valid duration format [ISO8601]'); }