Skip to content

Commit

Permalink
improved array, datetime functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sauls committed Feb 7, 2018
1 parent ff38944 commit 5063ccd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 27 deletions.
53 changes: 39 additions & 14 deletions src/array.php
Expand Up @@ -17,24 +17,44 @@ function array_merge(... $arrays): array
$result = \array_shift($arrays);
while (!empty($arrays)) {
$nextArray = \array_shift($arrays);
foreach ($nextArray as $key => $value) {
if (\is_int($key)) {
if (\array_key_exists($key, $result)) {
$result[] = $value;
} else {
$result[$key] = $value;
}
} elseif (\is_array($value) && isset($result[$key]) && \is_array($result[$key])) {
$result[$key] = array_merge($result[$key], $value);
} else {
$result[$key] = $value;
}
}
array_merge_with_next_array($nextArray, $result);
}

return $result;
}

function array_merge_with_next_array($nextArray, &$result): void
{
foreach ($nextArray as $key => $value) {
array_merge_with_current_array_values($key, $value, $result);
}
}

function array_merge_with_current_array_values($key, $value, &$result): void
{
if (\is_int($key)) {
array_merge_integer_keyed_value($key, $value, $result);
} elseif (can_merge_two_value_arrays($key, $value, $result)) {
$result[$key] = array_merge($result[$key], $value);
} else {
$result[$key] = $value;
}
}

function can_merge_two_value_arrays($key, $value, $result): bool
{
return \is_array($value) && isset($result[$key]) && \is_array($result[$key]);
}

function array_merge_integer_keyed_value($key, $value, &$result): void
{
if (\array_key_exists($key, $result)) {
$result[] = $value;
} else {
$result[$key] = $value;
}
}

/**
* @param mixed $array
* @param mixed $key
Expand Down Expand Up @@ -122,7 +142,7 @@ function array_remove_key(&$array, $key, $default = null)

$key = \array_shift($keys);

if (\is_array($array) && (isset($array[$key]) || \array_key_exists($key, $array))) {
if (can_remove_array_key($array, $key)) {
$value = $array[$key];
unset($array[$key]);

Expand All @@ -132,6 +152,11 @@ function array_remove_key(&$array, $key, $default = null)
return $default;
}

function can_remove_array_key($array, $key): bool
{
return \is_array($array) && (isset($array[$key]) || \array_key_exists($key, $array));
}

function array_key_exists(array $array, $key): bool
{
$keys = parse_array_key_path($key);
Expand Down
30 changes: 17 additions & 13 deletions src/datetime.php
Expand Up @@ -64,33 +64,37 @@ function print_elapsed_time_long($date, array $elapsedTimeLabels = []): string
return implode(' ', elapsed_time($date, $elapsedTimeLabels));
}

/**
* @param string|\DateTime $date
*/
function elapsed_time($date, array $elapsedTimeLabels = []): array
{
$elapsedTimeLabels = array_merge(DEFAULT_ELAPSED_TIME_LABELS, $elapsedTimeLabels);

if (\is_string($date)) {
$date = new \DateTime($date);
}
$date = create_date($date);

$time = $date->getTimestamp();
$diff = time() - $time;
$timeLeft = [];
$timeDifference = time() - $time;
$timeLeftValues = [];

foreach (DEFAULT_ELAPSED_TIME_OFFSETS as list($timeSingle, $timePlural, $offset)) {
if ($diff >= $offset) {
$left = floor($diff / $offset);
$diff -= ($left * $offset);
$timeLeft[] = format_elapsed_time_string((int)$left, $elapsedTimeLabels, [
if ($timeDifference >= $offset) {
$timeLeft = floor($timeDifference / $offset);
$timeDifference -= ($timeLeft * $offset);
$timeLeftValues[] = format_elapsed_time_string((int)$timeLeft, $elapsedTimeLabels, [
ELAPSED_TIME_LABEL_SINGLE => $timeSingle,
ELAPSED_TIME_LABEL_PLURAL => $timePlural,
]);
}
}

return $timeLeft;
return $timeLeftValues;
}

function create_date($date): \DateTime
{
if (\is_string($date)) {
$date = new \DateTime($date);
}

return $date;
}

function format_elapsed_time_string(int $timeLeft, array $elapsedTimeLabels, array $timeStrings): string
Expand Down

0 comments on commit 5063ccd

Please sign in to comment.