Skip to content

Commit

Permalink
DateRange object implemented, exceptions refactored, tests update
Browse files Browse the repository at this point in the history
  • Loading branch information
misantron committed Jun 22, 2015
1 parent 65f06d5 commit a9f8dc3
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 229 deletions.
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<filter>
<whitelist>
<directory suffix=".php">src</directory>
<exclude>
<directory suffix="Interface.php">src</directory>
</exclude>
</whitelist>
</filter>
<logging>
Expand Down
17 changes: 0 additions & 17 deletions src/Exception/InvalidArgumentException.php

This file was deleted.

17 changes: 0 additions & 17 deletions src/Exception/OutOfRangeException.php

This file was deleted.

17 changes: 0 additions & 17 deletions src/Exception/RuntimeException.php

This file was deleted.

71 changes: 71 additions & 0 deletions src/Object/DateRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Utility\Object;

/**
* Class DateRange
*
* @category Object
* @package Utility\Object
* @author Alexandr Ivanov <misantron@gmail.com>
* @license MIT https://github.com/misantron/php-utils/blob/master/LICENSE
* @link https://github.com/misantron/php-utils/blob/master/src/UTime.php
*/
class DateRange implements DateRangeInterface
{
/** @var \DateTime */
private $rangeBegin;
/** @var \DateTime */
private $rangeEnd;

/**
* @param mixed $dateBegin
* @param mixed $dateEnd
*/
function __construct($dateBegin, $dateEnd)
{
if (is_int($dateBegin)) {
$this->rangeBegin = (new \DateTime())->setTimestamp($dateBegin);
} elseif (is_string($dateBegin)) {
$this->rangeBegin = new \DateTime($dateBegin);
} elseif ($dateBegin instanceof \DateTime) {
$this->rangeBegin = $dateBegin;
} else {
throw new \InvalidArgumentException('$dateBegin argument format is invalid.');
}

if (is_int($dateEnd)) {
$this->rangeEnd = (new \DateTime())->setTimestamp($dateEnd);
} elseif (is_string($dateEnd)) {
$this->rangeEnd = new \DateTime($dateEnd);
} elseif ($dateEnd instanceof \DateTime) {
$this->rangeEnd = $dateEnd;
} else {
throw new \InvalidArgumentException('$dateEnd argument format is invalid.');
}
}

/**
* @return \DateTime
*/
public function getRangeBegin()
{
return $this->rangeBegin;
}

/**
* @return \DateTime
*/
public function getRangeEnd()
{
return $this->rangeEnd;
}

/**
* @return \DateTime[]
*/
public function toArray()
{
return [$this->rangeBegin, $this->rangeEnd];
}
}
25 changes: 25 additions & 0 deletions src/Object/DateRangeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Utility\Object;

/**
* Interface DateRangeInterface
*
* @category Object
* @package Utility\Object
* @author Alexandr Ivanov <misantron@gmail.com>
* @license MIT https://github.com/misantron/php-utils/blob/master/LICENSE
* @link https://github.com/misantron/php-utils/blob/master/src/UTime.php
*/
interface DateRangeInterface
{
/**
* @return \DateTime
*/
public function getRangeBegin();

/**
* @return \DateTime
*/
public function getRangeEnd();
}
5 changes: 2 additions & 3 deletions src/UAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Utility;

use Utility\Exception\InvalidArgumentException;
use Utility\Exception\NonStaticCallException;
use Utility\Translation\UAbstractTranslation;

Expand Down Expand Up @@ -32,7 +31,7 @@ function __construct()
*
* @return array
*
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*
* @codeCoverageIgnore
*/
Expand All @@ -44,7 +43,7 @@ protected static function loadTranslations($method)
$className = str_replace('Utility', 'Utility\\Translation', get_called_class()) . 'Translation';
$dictionary = $className::load($method);
if ($dictionary === null) {
throw new InvalidArgumentException('Can not load translation for method.');
throw new \InvalidArgumentException('Can not load translation for method.');
}
return $dictionary;
}
Expand Down
76 changes: 45 additions & 31 deletions src/UArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Utility;

use Utility\Exception\InvalidArgumentException;

/**
* Class UArray
*
Expand All @@ -29,7 +27,7 @@ class UArray extends UAbstract
*
* @return mixed
*
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public static function get(&$arr, $key, $default = null)
{
Expand All @@ -38,7 +36,7 @@ public static function get(&$arr, $key, $default = null)
$value = $arr[$key];
} else {
if (func_num_args() < 3) {
throw new InvalidArgumentException('Element with key "' . $key . '" not found.');
throw new \InvalidArgumentException('Element with key "' . $key . '" not found.');
}
$value = $default;
}
Expand Down Expand Up @@ -159,7 +157,7 @@ public static function searchKey(&$arr, $key)
*
* @return array
*
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public static function insertBefore(&$arr, $needleKey, $element, $withKey = null)
{
Expand All @@ -170,7 +168,7 @@ public static function insertBefore(&$arr, $needleKey, $element, $withKey = null
}
$offset = static::searchKey($arr, $needleKey);
if ($offset === false) {
throw new InvalidArgumentException('Element with key "' . $needleKey . '" not found.');
throw new \InvalidArgumentException('Element with key "' . $needleKey . '" not found.');
}
return array_merge(
array_slice($arr, 0, $offset, true),
Expand Down Expand Up @@ -210,26 +208,24 @@ public static function insertAfter(&$arr, $needleKey, $element, $withKey = null)
* Merge several arrays recursive.
* Basically use for two arrays.
*
* @param array $arr1
* @param array $arr2
* @param array $firstArr
* @param array $secondArr
*
* @return array
*/
public static function mergeRecursive($arr1, $arr2)
public static function mergeRecursive($firstArr, $secondArr)
{
$args = func_get_args();
$result = array_shift($args);
while (!empty($args)) {
while (sizeof($args) > 0) {
$next = array_shift($args);
foreach ($next as $k => $v) {
if (is_integer($k)) {
if (isset($result[$k])) {
if (isset($result[$k])) {
if (is_integer($k)) {
$result[] = $v;
} else {
$result[$k] = $v;
} elseif (is_array($v) && is_array($result[$k])) {
$result[$k] = static::mergeRecursive($result[$k], $v);
}
} elseif (is_array($v) && isset($result[$k]) && is_array($result[$k])) {
$result[$k] = static::mergeRecursive($result[$k], $v);
} else {
$result[$k] = $v;
}
Expand Down Expand Up @@ -283,33 +279,51 @@ public static function map(&$arr, $keyColumn, $valColumn = null)
* @param int|array $direction
* @param int|array $sortFlag
*
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public static function multisort(&$arr, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR)
public static function multiSort(&$arr, $key, $direction = SORT_ASC, $sortFlag = SORT_REGULAR)
{
$keys = is_array($key) ? $key : [$key];
if (empty($keys) || empty($arr)) {
throw new InvalidArgumentException('Params $arr or $key is invalid for sorting.');
// @codeCoverageIgnoreStart
$keys = static::prepareArguments($arr, $key, $direction, $sortFlag);
// @codeCoverageIgnoreEnd
$args = [];
foreach ($keys as $i => $key) {
$args[] = static::extractColumn($arr, $key);
$args[] = $direction[$i];
$args[] = $sortFlag[$i];
}
$args[] = &$arr;
call_user_func_array('array_multisort', $args);
}

/**
* @param array $arr
* @param string|array $key
* @param int|array $direction
* @param int|array $sortFlag
*
* @return array
*
* @throws \InvalidArgumentException
*/
private static function prepareArguments(&$arr, &$key, &$direction = SORT_ASC, &$sortFlag = SORT_REGULAR)
{
$keys = is_array($key) ? $key : [$key];
$keysCount = sizeof($keys);
if ($keysCount < 1 || empty($arr)) {
throw new \InvalidArgumentException('Params $arr or $key is invalid for sorting.');
}
if (is_scalar($direction)) {
$direction = array_fill(0, $keysCount, $direction);
} elseif (sizeof($direction) !== $keysCount) {
throw new InvalidArgumentException('The length of $direction and $keys params must be equal.');
throw new \InvalidArgumentException('The length of $direction and $keys params must be equal.');
}
if (is_scalar($sortFlag)) {
$sortFlag = array_fill(0, $keysCount, $sortFlag);
} elseif (sizeof($sortFlag) !== $keysCount) {
throw new InvalidArgumentException('The length of $sortFlag and $keys params must be equal.');
throw new \InvalidArgumentException('The length of $sortFlag and $keys params must be equal.');
}
$args = [];
foreach ($keys as $i => $key) {
$args[] = static::extractColumn($arr, $key);
$args[] = $direction[$i];
$args[] = $sortFlag[$i];
}
$args[] = &$arr;
call_user_func_array('array_multisort', $args);
return $keys;
}

/**
Expand Down
10 changes: 4 additions & 6 deletions src/UString.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Cocur\Slugify\Slugify;
use Utility\Exception\ExtensionNotLoadedException;
use Utility\Exception\InvalidArgumentException;
use Utility\Exception\RuntimeException;

/**
* Class UString
Expand Down Expand Up @@ -83,12 +81,12 @@ public static function truncateWords($str, $count, $ending = '')
*
* @return string
*
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public static function plural($number, $forms, $withNumber = false)
{
if (sizeof($forms) < 3) {
throw new InvalidArgumentException('Param $forms must contains three words.');
throw new \InvalidArgumentException('Param $forms must contains three words.');
}
$text = $number % 10 == 1 && $number % 100 != 11 ? $forms[0] : ($number % 10 >= 2 && $number % 10 <= 4 && ($number % 100 < 10 || $number % 100 >= 20) ? $forms[1] : $forms[2]);
return $withNumber ? $number . ' ' . $text : $text;
Expand Down Expand Up @@ -126,7 +124,7 @@ public static function random($length = 16, $humanFriendly = false)
* @return string
*
* @throws ExtensionNotLoadedException
* @throws RuntimeException
* @throws \RuntimeException
*/
public static function secureRandom($length = 16)
{
Expand All @@ -139,7 +137,7 @@ public static function secureRandom($length = 16)
$bytes = openssl_random_pseudo_bytes($length, $cryptStrong);
// @codeCoverageIgnoreStart
if (static::byteLength($bytes) < $length || !$cryptStrong) {
throw new RuntimeException('Unable to generate random bytes.');
throw new \RuntimeException('Unable to generate random bytes.');
}
$bytes = static::byteSubstr($bytes, 0, $length);
// @codeCoverageIgnoreEnd
Expand Down
Loading

0 comments on commit a9f8dc3

Please sign in to comment.