feat: initial commit
Basic functionality and tests and all that.
This commit is contained in:
commit
69c4a24301
10 changed files with 307 additions and 0 deletions
1
.coveralls.yml
Normal file
1
.coveralls.yml
Normal file
|
@ -0,0 +1 @@
|
|||
service_name: travis-ci
|
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
composer.lock
|
||||
composer.phar
|
||||
.phpunit.result.cache
|
||||
/vendor/
|
40
.travis.yml
Normal file
40
.travis.yml
Normal file
|
@ -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
|
1
FUNDING.yml
Normal file
1
FUNDING.yml
Normal file
|
@ -0,0 +1 @@
|
|||
github: joshtronic
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -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.
|
42
README.md
Normal file
42
README.md
Normal file
|
@ -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
|
33
composer.json
Normal file
33
composer.json
Normal file
|
@ -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/"
|
||||
}
|
||||
}
|
||||
}
|
23
phpunit.xml
Normal file
23
phpunit.xml
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
failOnRisky="true"
|
||||
stopOnError="true"
|
||||
stopOnFailure="true"
|
||||
stopOnIncomplete="true"
|
||||
verbose="true"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="Random Date">
|
||||
<directory>tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
<whitelist addUncoveredFilesFromWhitelist="true">
|
||||
<directory>./src</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
122
src/RandomDate.php
Normal file
122
src/RandomDate.php
Normal file
|
@ -0,0 +1,122 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Random Date
|
||||
*
|
||||
* PHP version 5.3+
|
||||
*
|
||||
* Licensed under The MIT License.
|
||||
* Redistribution of these files must retain the above copyright notice.
|
||||
*
|
||||
* @author Josh Sherman <hello@joshtronic.com>
|
||||
* @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));
|
||||
}
|
||||
}
|
||||
|
20
tests/RandomDateTest.php
Normal file
20
tests/RandomDateTest.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
require_once './src/RandomDate.php';
|
||||
|
||||
if (
|
||||
!class_exists('\PHPUnit_Framework_TestCase')
|
||||
&& class_exists('\PHPUnit\Framework\TestCase')
|
||||
) {
|
||||
class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
|
||||
}
|
||||
|
||||
class RandomDateTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testDate()
|
||||
{
|
||||
$rd = new joshtronic\RandomDate();
|
||||
$this->assertRegExp('/^[0-9]{4}(-[0-9]{2}){2}$/i', $rd->date('Y-m-d'));
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue