From 69c4a2430139fbbc6a8324672f7e70235054c025 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Tue, 12 May 2020 22:28:19 -0500 Subject: [PATCH] feat: initial commit Basic functionality and tests and all that. --- .coveralls.yml | 1 + .gitignore | 4 ++ .travis.yml | 40 +++++++++++++ FUNDING.yml | 1 + LICENSE | 21 +++++++ README.md | 42 ++++++++++++++ composer.json | 33 +++++++++++ phpunit.xml | 23 ++++++++ src/RandomDate.php | 122 +++++++++++++++++++++++++++++++++++++++ tests/RandomDateTest.php | 20 +++++++ 10 files changed, 307 insertions(+) create mode 100644 .coveralls.yml create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 FUNDING.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml create mode 100644 src/RandomDate.php create mode 100644 tests/RandomDateTest.php diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..9160059 --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1 @@ +service_name: travis-ci diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a2144a3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +composer.lock +composer.phar +.phpunit.result.cache +/vendor/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..36d3649 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,40 @@ +language: php +dist: bionic +sudo: required + +matrix: + include: + - php: 5.3 + dist: precise + - php: 5.4 + dist: trusty + - php: 5.5 + dist: trusty + - php: 5.6 + dist: trusty + - php: 7.0 + dist: xenial + - php: 7.1 + - php: 7.2 + - php: 7.3 + - php: 7.4 + env: COVERAGE=true + - php: nightly + allow_failures: + - php: nightly + +install: + - php --version + - composer install + +before_script: + - mkdir -p build/logs + +script: + - vendor/bin/phpunit --coverage-clover build/logs/clover.xml + +after_success: + - | + if [[ $COVERAGE ]]; then + travis_retry php vendor/bin/php-coveralls --config .coveralls.yml -v + fi diff --git a/FUNDING.yml b/FUNDING.yml new file mode 100644 index 0000000..9396c28 --- /dev/null +++ b/FUNDING.yml @@ -0,0 +1 @@ +github: joshtronic diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9b29397 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2020 Josh Sherman + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..564302b --- /dev/null +++ b/README.md @@ -0,0 +1,42 @@ +# php-randomdate + +Random time/date generator fully compatible with PHP's date function. + +Compatible with PHP 5.3+. + +## Installation + +```shell +composer require joshtronic/php-randomdate +``` + +## Usage + +```php +$rd = new joshtronic\RandomDate(); + +// Between the Unix Epoch and now +$rd->date(/* format string, default = 'c' */); + +// Between January 1st, 1900 and now +$rd->min('1900-01-01')->date('Y-m-d'); + +// Between the Unix Epoch and next month +$rd->max('next month')->date('r'); + +// Between yesterday and tomorrow (at midnight) +$rd->between('yesterday', 'midnight tomorrow')->date('r'); + +// Reset minimum and maximum to defaults +$rd->reset->date(); +``` + +## Compatibility with PHP functions + +By design, this library aims to keep things simple by using the familiar +format string provided by PHP's existing [`date`][php-date] as well as the +extremely powerful [`strtotime`][php-strtotime] for setting the range of dates +used for generation. + +[php-date]: https://www.php.net/manual/en/function.date.php +[php-strtotime]: https://www.php.net/manual/en/function.strtotime.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..3d9e15a --- /dev/null +++ b/composer.json @@ -0,0 +1,33 @@ +{ + "name": "joshtronic/randomdate", + "description": "Random time/date generator fully compatible with PHP's date function.", + "version": "0.1.0", + "type": "library", + "keywords": [ + "random", + "date", + "time", + "generator" + ], + "homepage": "https://github.com/joshtronic/php-randomdate", + "license": "MIT", + "authors": [ + { + "name": "Josh Sherman", + "email": "hello@joshtronic.com", + "homepage": "https://joshtronic.com" + } + ], + "require": { + "php": ">=5.3" + }, + "require-dev": { + "php-coveralls/php-coveralls": ">=1", + "phpunit/phpunit": ">=4" + }, + "autoload": { + "psr-4": { + "joshtronic\\": "src/" + } + } +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..1fdd5e8 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,23 @@ + + + + + tests + + + + + ./src + + + diff --git a/src/RandomDate.php b/src/RandomDate.php new file mode 100644 index 0000000..08dfc98 --- /dev/null +++ b/src/RandomDate.php @@ -0,0 +1,122 @@ + + * @copyright Copyright 2020 Josh Sherman + * @license http://www.opensource.org/licenses/mit-license.html + * @link https://github.com/joshtronic/php-randomdate + */ + +namespace joshtronic; + +class RandomDate +{ + /** + * Min + * + * Minimum timestamp to use when generating time/date. Default value (null) + * is converted to the Unix Epoch (0). + * + * @access private + * @var null | integer + */ + private $min = null; + + /** + * Max + * + * Maximum timestamp to use when generating time/date. Default value (null) + * is converted to the current timestamp. + * + * @access private + * @var null | integer + */ + private $max = null; + + /** + * Min + * + * Set the minimum timestamp from date/time string. + * + * @access public + * @param string $min + * @return object + */ + public function min($min) + { + $this->min = strtotime($min); + return $this; + } + + /** + * Max + * + * Set the maximum timestamp from date/time string. + * + * @access public + * @param string $max + * @return object + */ + public function max($max) + { + $this->max = strtotime($max); + return $this; + } + + /** + * Between + * + * Set the minimum and maximum timestamps from date/time strings. + * + * @access public + * @param string $min + * @param string $max + * @return object + */ + public function between($min, $max) + { + $this->min($min); + $this->max($max); + return $this; + } + + /** + * Reset + * + * Set the minimum and maximum timestamps to their respective defaults. + * + * @access public + * @return object + */ + public function reset() + { + $this->min = null; + $this->max = null; + return $this; + } + + /** + * Date + * + * Returns a string formatted according to the given format string using + * the given minimum and maximum timestamps. + * + * @access public + * @param string $format `date()` compatible format string. + * @return string + */ + public function date($format = 'c') + { + $min = ($this->min == null ? 0 : $this->min); + $max = ($this->max == null ? time() : $this->max); + return date($format, mt_rand($min, $max)); + } +} + diff --git a/tests/RandomDateTest.php b/tests/RandomDateTest.php new file mode 100644 index 0000000..3907953 --- /dev/null +++ b/tests/RandomDateTest.php @@ -0,0 +1,20 @@ +assertRegExp('/^[0-9]{4}(-[0-9]{2}){2}$/i', $rd->date('Y-m-d')); + } +} +