Skip to content

Commit

Permalink
Removed divideCollectionIntoGroups(), compareDates(), getTodayDay(), …
Browse files Browse the repository at this point in the history
…getDate() and getTime() methods from Helpers class
  • Loading branch information
micheleangioni committed May 17, 2017
1 parent c19b031 commit ff1b2d4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 130 deletions.
115 changes: 7 additions & 108 deletions src/MicheleAngioni/Support/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace MicheleAngioni\Support;

use DateTime;
use Illuminate\Support\Collection;
use InvalidArgumentException;

class Helpers
{
Expand All @@ -20,9 +18,9 @@ class Helpers
*
* @return bool
*/
static public function isInt($int, $min = false, $max = false)
static public function isInt($int, $min = false, $max = false): bool
{
if (is_object($int) || is_array($int)) {
if (is_object($int) || is_array($int) || is_callable($int)) {
return false;
}

Expand Down Expand Up @@ -69,7 +67,7 @@ static public function randInArray(array $array)
*
* @return bool
*/
static public function checkDate($date, $format = 'Y-m-d')
static public function checkDate($date, $format = 'Y-m-d'): bool
{
$d = DateTime::createFromFormat($format, $date);

Expand All @@ -83,18 +81,14 @@ static public function checkDate($date, $format = 'Y-m-d')
*
* @return bool
*/
static public function checkDatetime($datetime)
static public function checkDatetime($datetime): bool
{
$format = 'Y-m-d H:i:s';

$d = DateTime::createFromFormat($format, $datetime);

return $d && $d->format($format) == $datetime;
return self::checkDate($datetime, 'Y-m-d H:i:s');
}

/**
* Split two 'Y-m-d'-format dates into an array of dates. Returns false on failure.
* $first_date must be < than $second_date
* $firstDate must be < than $secondDate
* Third optional parameter indicates max days difference allowed (0 = no limits).
*
* @param string $firstDate
Expand Down Expand Up @@ -169,104 +163,9 @@ static public function daysBetweenDates($date1, $date2)
return (count($dates) - 1);
}

/**
* Compare $date with $referenceDate. Return true if $date is more recent, false otherwise (included if the two dates are identical).
*
* @param string $date
* @param string $referenceDate
*
* @return bool
*/
static public function compareDates($date, $referenceDate)
{
$dateTimestamp = strtotime($date);
$referenceDateTimestamp = strtotime($referenceDate);

if ($dateTimestamp > $referenceDateTimestamp) {
return true;
} else {
return false;
}
}

/**
* Split a Collection into groups of equal numbers. $groupsNumber must be a multiplier of 2.
*
* @param Collection $collection
* @param int $groupsNumber = 2
*
* @throws InvalidArgumentException
*
* @return Collection|false
*/
static public function divideCollectionIntoGroups(Collection $collection, $groupsNumber = 2)
{
if (!(Helpers::isInt($groupsNumber, 2) && !($groupsNumber % 2))) {
return false;
}

$elementsPerGroup = (int)ceil(count($collection) / $groupsNumber);

$newCollection = new Collection([]);

for ($i = 0; $i <= $groupsNumber - 1; $i++) {
$newCollection[$i] = $collection->slice($i * $elementsPerGroup, $elementsPerGroup);
}

return $newCollection;
}


// <<<--- DATE TIME METHODS --->>>

/*
* These DateTime methods are thought in order to allow Date / Time mocking in tests and other useful uses.
*/

/**
* Return today's day.
*
* @return string
*/
public function getTodayDay()
{
$datetime = new \DateTime("now");

return $datetime->format("D");
}

/**
* Return today's day in format Y-m-d. Offset in days.
* Customize $format to receive the wanted date format.
*
* @param int $offset = 0
* @param string $format = 'Y-m-d'
*
* @return string
*/
public function getDate($offset = 0, $format = 'Y-m-d')
{
return date($format, strtotime($offset . ' day'));
}

/**
* Return today's time in format H:i:s. Offset in minutes.
* Customize $format to receive the wanted time format.
*
* @param int $offset = 0
* @param string $format = 'H:i:s'
*
* @return string
*/
public function getTime($offset = 0, $format = 'H:i:s')
{
return date($format, strtotime($offset . ' minutes'));
}



// <<<--- PSEUDO-RANDOM NUMBERS METHODS --->>>


/**
* Return a random value between input $min and $max values by using the MCRYPT_DEV_URANDOM source.
* N.B. Use only on *nix servers!
Expand Down
27 changes: 5 additions & 22 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public function testIsInt()
{
$object = new stdClass();

$callable = function () {
echo 'I am a Callable';
};

$this->assertFalse(H::isInt($callable));
$this->assertFalse(H::isInt($object));
$this->assertFalse(H::isInt(['value']));
$this->assertFalse(H::isInt(['key' => 'value']));
Expand Down Expand Up @@ -78,28 +83,6 @@ public function testDaysBetweenDates()
$this->assertFalse(H::daysBetweenDates('2014-12-23', '2014-12-24x'));
}

public function testCompareDates()
{
date_default_timezone_set('UTC');
$this->assertTrue(H::compareDates('2014-12-24', '2014-12-22'));
$this->assertFalse(H::compareDates('2014-12-22', '2014-12-24'));
$this->assertFalse(H::compareDates('2014-12-24', '2014-12-24'));
}

public function testDivideCollectionIntoGroups()
{
$collection = new \Illuminate\Support\Collection;
$collection->push(1);
$collection->push(2);
$collection->push(3);
$collection->push(4);

$newCollection = H::divideCollectionIntoGroups($collection);
$this->assertEquals(2, $newCollection->count());

$this->assertFalse(H::divideCollectionIntoGroups($collection, 3));
}

public function testGetUniqueRandomValues()
{
$helpers = new H;
Expand Down

0 comments on commit ff1b2d4

Please sign in to comment.