/** * Try to parse a date interval specification string. * * @param string|DateInterval|PHPDateInterval|null $dateIntervalSpec [optional] A date interval specification, a * relative date and time string, a DateInterval or PHPDateInterval instance or null to create a zero * configuration. * @param mixed|null $default [optional] The default value returned on failure. * * @return string|mixed The parsed date interval specification or the default value on failure. */ public static function parse($dateIntervalSpec = null, $default = null) { // Return a zero specification if the specification is set to null if (empty($dateIntervalSpec)) { return DateIntervalSpec::createZero(); } // Parse strings if (is_string($dateIntervalSpec)) { // Check whether the string is already a valid specification if (static::isValid($dateIntervalSpec)) { return $dateIntervalSpec; } // Check whether the string has relative keywords if (DateTime::hasRelativeKeywords($dateIntervalSpec)) { try { // Try to parse the string as relative date and time $dateInterval = DateInterval::createFromDateString($dateIntervalSpec); // Get and return the date interval specification return $dateInterval->toSpecString(); } catch (Exception $ex) { } } } // Parse DateInterval objects if ($dateIntervalSpec instanceof DateInterval) { return $dateIntervalSpec->toSpecString(); } // Parse PHPDateInterval objects if ($dateIntervalSpec instanceof PHPDateInterval) { if (($spec = DateIntervalSpec::create($dateIntervalSpec->y, $dateIntervalSpec->m, null, $dateIntervalSpec->d, $dateIntervalSpec->h, $dateIntervalSpec->i, $dateIntervalSpec->s)) !== null) { return $spec; } } // Couldn't parse the string, return the default value return $default; }
public static function createGuess($firstName, $lastName, $mail, $weight) { // Make sure the name is valid if (!AccountUtils::isValidName($firstName) || !AccountUtils::isValidName($firstName)) { throw new Exception('The name is invalid.'); } // Make sure the mail is valid if (!AccountUtils::isValidMail($mail)) { throw new Exception('The mail is invalid.'); } // TODO: Validate the weight! // Get the session ID $sessionId = getSessionKey(); // Determine the creation date time $dateTime = DateTime::now(); // Get the guess IP $ip = IpUtils::getClientIp(); // Prepare a query for the picture being added $statement = Database::getPDO()->prepare('INSERT INTO ' . static::getDatabaseTableName() . ' (guess_session_id, guess_first_name, guess_last_name, guess_mail, guess_weight, guess_datetime, guess_ip) ' . 'VALUES (:session_id, :first_name, :last_name, :mail, :weight, :guess_datetime, :ip)'); $statement->bindValue(':session_id', $sessionId, PDO::PARAM_STR); $statement->bindValue(':first_name', $firstName, PDO::PARAM_STR); $statement->bindValue(':last_name', $lastName, PDO::PARAM_STR); $statement->bindValue(':mail', $mail, PDO::PARAM_STR); $statement->bindValue(':weight', $weight, PDO::PARAM_STR); // TODO: Use the UTC/GMT timezone! $statement->bindValue(':guess_datetime', $dateTime->toString(), PDO::PARAM_STR); $statement->bindValue(':ip', $ip, PDO::PARAM_STR); // Execute the prepared query if (!$statement->execute()) { throw new Exception('Failed to query the database.'); } // Get and return the guess instance return new Guess(Database::getPDO()->lastInsertId()); }
/** * Check whether the result for the given sets is true based on the specified closure filter. * By default true will be returned if any object set has the result true, if <var>$all</var> is set to true all * object sets must have a result of true. False is returned otherwise. * * The number of objects in all sets must be equal, some sets may equal one. * If the number of objects in all sets equals, all objects will be compared to the object in the other set at the * same set index. If a set contains one object, this one object will be compared to the relevant objects of the * other sets. * * A non-array object may be given which is then converted into an array. * * The closure filter one argument, which is an array of all the relevant DateTime objects for that iteration. The * number of objects in this array equals the number of available sets. The filter must return true if the result * is correct, or false if not. Null may be returned on failure. * * @param Array $sets [optional] An array containing all sets as arrays, with values accepted by * DateTimeArrayUtils::parse(). * @param Closure $callback The filter to run for each object set. * @param bool $all [optional] True to require all object sets to return true, false if not. * @param mixed|null $default [optional] The default value returned on failure. * * @return bool|mixed True if any of the iterations gives a result of true, if <var>$all</var> is set to true all * iterations must give a result of true to return true, false will be returned otherwise. The default value * will be returned on failure. */ public static function compareSets($sets, Closure $callback, $all = true, $default = null) { // Make sure the sets parameter is an array if (!is_array($sets)) { return $default; } // Make sure all sets are arrays foreach ($sets as &$value) { if (!is_array($value)) { $value = array($value); } } // Unset the value reference to prevent accidental modifications unset($value); // Make sure the closure is valid if (!Utils::isValidClosure($callback)) { return $default; } // Count the values of all sets $setCounts = array(); foreach ($sets as $value) { $setCounts[] = count($value); } $countMax = max($setCounts); $setCount = count($sets); // Make sure all counts equal the maximum count or one foreach ($setCounts as $count) { if ($count != 1 && $count != $countMax) { return $default; } } // Define the variables for all set values $setValues = array_fill(0, $setCount, null); // Predefine and parse sets with a single element for better performance for ($valueIndex = 0; $valueIndex < $setCount; $valueIndex++) { if ($setCounts[$valueIndex] == 1) { if (($setValues[$valueIndex] = DateTime::parse($sets[$valueIndex], null)) === null) { return $default; } } } // Loop through the objects for ($valueIndex = 0; $valueIndex < $countMax; $valueIndex++) { // Update all values for the sets that have more than one item foreach ($sets as $setIndex => $value) { if ($setCounts[$setIndex] > 1) { $setValues[$setIndex] = $value[$valueIndex]; } } // Check whether any of the set values is an array $isArray = false; foreach ($setValues as $value) { if (is_array($value)) { $isArray = true; break; } } // Handle recursive arrays if ($isArray) { // Get the result from the recursive array $result = static::compareSets($setValues, $callback, $all, null); // Return the default value if the result is invalid if ($result === null) { return $default; } } else { // Parse all set values that have sets with more than one item, return the default value on failure foreach ($setValues as $setIndex2 => &$setValue2) { if ($setCounts[$setIndex2] > 1) { if (($setValue2 = DateTime::parse($setValue2, null)) === null) { return $default; } } } // Call the closure to get the result $result = call_user_func($callback, $setValues); // Make sure the value is boolean, or return the default value if (!is_bool($result)) { return $default; } } // Return false if the result is false while everything must be true if (!$result && $all) { return false; } // Return true if the result is true while anything must be true if ($result && !$all) { return true; } } // Return the result return (bool) $all; }
/** * Get the date and time as a string formatted according to given format. * * @param string|null $format [optional] The desired format for the date and time, or null to use the default * format. * * @return string|null The date and time as a string, or null on failure. */ public function format($format = null) { // Use the default format if the format parameter is null if ($format === null) { $format = self::getPreferredFormat(); } // Get and return the date and time with the proper format, return null on failure return ($result = parent::format($format)) === false ? null : $result; }
/** * Set a cookie. * * @param string $key Cookie key. * @param mixed $value Cookie value. * @param string|DatePeriod $duration Duration or period. * @param string|null $path [optional] Cookie path. * @param string|null $domain [optional] Cookie domain. */ public static function setCookie($key, $value, $duration, $path = null, $domain = null) { // Determine the expiration date time $expirationDateTime = DateTime::parse($duration); // Parse the cookie path and domain if ($path === null) { $path = static::$path; } if ($domain === null) { $domain = static::$domain; } // Set the cookie setcookie(static::getFullCookieKey($key), $value, $expirationDateTime->getTimestamp(), $path, $domain); }
/** * Set a registry value. If the value doesn't exist yet, it will be created. * * @param string $key The key. * @param string $value The value. * * @return RegistryValue The registry as object. * * @throws Exception Throws if an error occurred. */ public static function setValue($key, $value) { // Trim the key // TODO: Parse the registry key $key = trim($key); // Create this if it hasn't been set already if (!static::isValueWithKey($key)) { // Prepare a query for the being created $statement = Database::getPDO()->prepare('INSERT INTO ' . static::getDatabaseTableName() . ' (registry_key, registry_value, registry_modified_datetime) VALUES (:registry_key, :registry_value, :modified_datetime)'); $statement->bindValue(':registry_key', $key, PDO::PARAM_STR); $statement->bindValue(':registry_value', $value, PDO::PARAM_STR); // TODO: Use the UTC/GMT timezone! $statement->bindValue(':modified_datetime', DateTime::now()->toString(), PDO::PARAM_STR); // Execute the prepared query if (!$statement->execute()) { throw new Exception('Failed to query the database.'); } // Return the created registry as object return new RegistryValue(Database::getPDO()->lastInsertId()); } // Get the object $registryValue = static::getValue($key); // Set the registry value $registryValue->setValue($value); // Return the object return $registryValue; }
/** * Returns all transitions for the timezone in the specified time frame. * * @param int|DateTime|PHPDateTime|string|null $timestampBegin [optional] The end timestamp, a DateTime or * PHPDateTime instance, the date and time as a string or null. * * @param null $timestampEnd * * @return Array|null Returns a numerically indexed array containing associative arrays with all transitions, or * null on failure. * * @throws Exception Throws an exception on failure. * * @link http://php.net/manual/en/datetimezone.gettransitions.php */ public function getTransitions($timestampBegin = null, $timestampEnd = null) { // Try to parse the timestamps for the begin and end parameter if ($timestampBegin !== null && !is_int($timestampBegin)) { // Parse the timestamp, throw an exception on failure if (($timestampBegin = DateTime::parse($timestampBegin)) === null) { throw new Exception('Invalid begin timestamp'); } // Gather the timestamp $timestampBegin = $timestampBegin->getTimestamp(); } if ($timestampEnd !== null && !is_int($timestampEnd)) { // Parse the timestamp, throw an exception on failure if (($timestampEnd = DateTime::parse($timestampEnd)) === null) { throw new Exception('Invalid end timestamp'); } // Gather the timestamp $timestampEnd = $timestampEnd->getTimestamp(); } // Get and return the transitions, return null on failure return ($transitions = parent::getTransitions($timestampBegin, $timestampEnd)) !== false ? $transitions : null; }
/** * Parse a date and time with an optional time zone. A new instance will be created if required. * * If the $dateTime parameter is a DateTime zone instance, the instance will be returned and the $timezone * parameter is ignored. If the $dateTime parameter is anything other than a DateTime zone the date, time and the * time zone is parsed through the constructor. * * This method allows better fluent syntax because it makes method chaining possible. * * @param DateTime|PHPDateTime|string|null $dateTime [optional] A DateTime or PHPDateTime instance, the time as a string, or * null to use the now() time. * @param DateTimeZone|PHPDateTimeZone|string|DateTime|PHPDateTime|null $timezone [optional] The time zone the * specified time is in, or null to use the default time zone if the $time param isn't a DateTime instance. A * DateTime or PHPDateTime instance to use it's timezone. * @param mixed|null $default [optional] The default value returned on failure. * * @return DateTime|mixed The DateTime instance, or the default value on failure. */ public static function parse($dateTime = null, $timezone = null, $default = null) { return ($result = DateTime::parse($dateTime, $timezone)) !== null ? $result : $default; }