From faca7e31dbf5c5ffea5eeb9e459f1ce04ad538f6 Mon Sep 17 00:00:00 2001 From: Francois Zaninotto Date: Sun, 16 Oct 2011 22:07:44 +0200 Subject: [PATCH] Add DateTime provider. Closes #2 --- readme.md | 20 ++++++++ src/Documentor.php | 2 + src/Factory.php | 2 +- src/Provider/DateTime.php | 100 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 src/Provider/DateTime.php diff --git a/readme.md b/readme.md index 0d4c8495..735c55e1 100644 --- a/readme.md +++ b/readme.md @@ -38,6 +38,26 @@ echo $faker->lorem; Each of the generator properties (like `name`, `address`, and `lorem`) are called "formatters". A faker generator has many of them, packaged in "providers". Here is a list of the bundled formatters in the default locale. +### `Faker\Provider\DateTime` + + amPm() // 'am' + century() // 'V' + date() // '2008-11-27' + dateTime() // 2005-08-16 20:39:21 + dateTimeBetween() // 1999-02-02 11:42:52 + dateTimeThisCentury() // 1964-04-04 11:02:02 + dateTimeThisDecade() // 2010-03-10 05:18:58 + dateTimeThisMonth() // 2011-10-05 12:51:46 + dateTimeThisYear() // 2011-09-19 09:24:37 + dayOfMonth() // '29' + dayOfWeek() // 'Thursday' + iso8601() // '2003-10-21T16:05:52+0000' + month() // '06' + monthName() // 'November' + time() // '15:02:34' + unixTime() // 1061306726 + year() // '1991' + ### `Faker\Provider\Internet` domainName() // 'mueller.info' diff --git a/src/Documentor.php b/src/Documentor.php index 5bf6530a..f410688e 100644 --- a/src/Documentor.php +++ b/src/Documentor.php @@ -26,6 +26,8 @@ class Documentor $example = $this->generator->format($methodName); if (is_array($example)) { $example = "array('". join("', '", $example) . "')"; + } elseif($example instanceof \DateTime) { + $example = $example->format('Y-m-d H:i:s'); } elseif (is_string($example)) { $example = var_export($example, true); } diff --git a/src/Factory.php b/src/Factory.php index e6fb68b3..fbe45a0c 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -8,7 +8,7 @@ class Factory { const DEFAULT_LOCALE = 'en_US'; - protected static $defaultProviders = array('Name', 'Address', 'PhoneNumber', 'Company', 'Lorem', 'Internet'); + protected static $defaultProviders = array('Name', 'Address', 'PhoneNumber', 'Company', 'Lorem', 'Internet', 'DateTime'); public static function create($locale = self::DEFAULT_LOCALE) { diff --git a/src/Provider/DateTime.php b/src/Provider/DateTime.php new file mode 100644 index 00000000..6b354562 --- /dev/null +++ b/src/Provider/DateTime.php @@ -0,0 +1,100 @@ +format($format); + } + + public static function time($format = 'H:i:s') + { + return static::dateTime()->format($format); + } + + public static function dateTimeBetween($startDate = "-30 years", $endDate = "now") + { + $startTimestamp = strtotime($startDate); + $endTimestamp = strtotime($endDate); + $timestamp = mt_rand($startTimestamp, $endTimestamp); + + return new \DateTime('@' . $timestamp); + } + + public static function dateTimeThisCentury() + { + return static::dateTimeBetween("-100 year"); + } + + public static function dateTimeThisDecade() + { + return static::dateTimeBetween("-10 year"); + } + + public static function dateTimeThisYear() + { + return static::dateTimeBetween("-1 year"); + } + + public static function dateTimeThisMonth() + { + return static::dateTimeBetween("-1 month"); + } + + public static function amPm() + { + return static::dateTime()->format('a'); + } + + public static function dayOfMonth() + { + return static::dateTime()->format('d'); + } + + public static function dayOfWeek() + { + return static::dateTime()->format('l'); + } + + public static function month() + { + return static::dateTime()->format('m'); + } + + public static function monthName() + { + return static::dateTime()->format('F'); + } + + public static function year() + { + return static::dateTime()->format('Y'); + } + + public static function century() + { + return static::randomElement(static::$century); + } + +} \ No newline at end of file