/**
  * Validator.
  *
  * @access  public
  * @param   string   $input   Input value
  * @param   int      $age     Minimal age value
  * @param   string   $format  (optional) Date format
  * @return  boolean
  */
 public function validate($input, $age, $format = 'Y-m-d')
 {
     if (($input = Time::createFromFormat($format, $input)) === false) {
         return false;
     }
     return $input->getTimestamp() == Time::now()->modify("-{$age} years")->getTimestamp();
 }
示例#2
0
 /**
  *
  */
 public function testCreateFromFormat()
 {
     $time = Time::createFromFormat('Y-m-d H:i:s', '1983-08-30 13:37:33');
     $this->assertSame('1983-08-30 13:37:33', $time->format('Y-m-d H:i:s'));
     //
     $time = Time::createFromFormat('Y-m-d H:i:s', '1983-08-30 13:37:33', 'Asia/Tokyo');
     $this->assertSame('Asia/Tokyo', $time->getTimeZone()->getName());
     //
     $time = Time::createFromFormat('Y-m-d H:i:s', '1983-08-30 13:37:33', new DateTimeZone('Asia/Tokyo'));
     $this->assertSame('Asia/Tokyo', $time->getTimeZone()->getName());
 }
示例#3
0
 /**
  * Cast value to the appropriate type.
  *
  * @access  protected
  * @param   string     $name   Column name
  * @param   mixed      $value  Column value
  * @return  mixed
  */
 protected function cast($name, $value)
 {
     $cast = $this->getCastColumns();
     if (isset($cast[$name]) && $value !== null) {
         switch ($cast[$name]) {
             case 'int':
                 return (int) $value;
             case 'float':
                 return (double) $value;
             case 'bool':
                 return $value === 'f' ? false : (bool) $value;
             case 'date':
                 return $value instanceof DateTimeInterface ? $value : Time::createFromFormat($this->getDateFormat(), $value);
             case 'string':
                 return (string) $value;
             default:
                 throw new RunTimeException(vsprintf("%s::%s(): Unsupported type [ %s ].", [static::class, __FUNCTION__, $cast[$name]]));
         }
     }
     return $value;
 }