/** * 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(); }
/** * {@inheritdoc} */ public function throttle(UserInterface $user, $maxLoginAttempts, $lockTime) { $now = Time::now(); // Reset the failed attempt count if the last failed attempt was more than $lockTime seconds ago if (($lastFailAt = $user->getLastFailAt()) !== null) { if ($now->getTimestamp() - $lastFailAt->getTimestamp() > $lockTime) { $user->resetFailedAttempts(); } } // Increment the failed attempt count and update the last fail time $user->incrementFailedAttempts(); $user->setLastFailAt($now); // Lock the account for $lockTime seconds if we have exeeded the maximum number of login attempts if ($user->getFailedAttempts() >= $maxLoginAttempts) { $user->lockUntil(Time::now()->forward($lockTime)); } // Save the changes to the user return $user->save(); }
/** * */ public function testFormatLocalized() { $time = Time::createFromDate(1983, 8, 30); $locale = setlocale(LC_TIME, null); if (setlocale(LC_TIME, ['ja_JP.UTF-8', 'ja_JP.utf8']) !== false) { $this->assertSame('8月', $time->formatLocalized('%B')); } if (setlocale(LC_TIME, ['en_US.UTF-8', 'en_US.utf8']) !== false) { $this->assertSame('August', $time->formatLocalized('%B')); } setlocale(LC_TIME, $locale); }
/** * {@inheritdoc} */ public function isLocked() { return $this->locked_until !== null && $this->locked_until->getTimestamp() >= Time::now()->getTimestamp(); }
/** * 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; }