Skip to content

Commit

Permalink
Merge branch 'release/v0.1.1' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
betterthanclay committed Nov 28, 2022
2 parents 63e00b5 + f98b20e commit 3aea038
Show file tree
Hide file tree
Showing 8 changed files with 1,554 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
## v0.1.1 (2022-11-28)
* Imported time and number plugins from Dictum

## v0.1.0 (2022-11-28)
* Built initial locale and timezone management
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
}],
"require": {
"php": "^8.0",
"ext-intl": "*",

"decodelabs/exceptional": "^0.4.3",
"decodelabs/glitch-support": "^0.4.3",
"decodelabs/veneer": "^0.10.16"
Expand Down
77 changes: 76 additions & 1 deletion src/Cosmos/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use DateTimeZone as Timezone;
use DecodeLabs\Veneer;
use DecodeLabs\Veneer\Plugin;
use IntlTimeZone;
use Locale as SysLocale;

class Context
Expand Down Expand Up @@ -62,6 +63,30 @@ public function getLocale(): Locale
return $this->locale;
}

/**
* Normalize locale
*/
public function normalizeLocale(
string|Locale|null $locale = null
): Locale {
if ($locale === null) {
$locale = $this->getLocale();
} elseif (is_string($locale)) {
$locale = new Locale($locale);
}

return $locale;
}

/**
* Normalize locale
*/
public function normalizeLocaleString(
string|Locale|null $locale = null
): string {
return (string)$this->normalizeLocale($locale);
}



/**
Expand All @@ -73,11 +98,61 @@ public function setTimezone(
string|Timezone $timezone
): static {
if (!$timezone instanceof Timezone) {
$timezone = new Timezone($timezone);
$timezone = $this->createTimezone($timezone);
}

$this->timezone = $timezone;

return $this;
}

/**
* Get timezone
*/
public function getTimezone(): Timezone
{
return $this->timezone;
}

/**
* Normalize timezone
*/
public function normalizeTimezone(
string|Timezone|null $timezone
): Timezone {
if ($timezone === null) {
$timezone = $this->getTimezone();
} elseif (is_string($timezone)) {
$timezone = $this->createTimezone($timezone);
}

return $timezone;
}

/**
* Normalize timezone
*/
public function normalizeTimezoneString(
string|Timezone|null $timezone
): string {
return $this->normalizeTimezone($timezone)->getName();
}

/**
* Parse and create DateTimeZone
*/
protected function createTimezone(string $timezone): Timezone
{
if (preg_match('/^[a-z]{3}$/', $timezone)) {
$timezone = strtoupper($timezone);
} elseif (preg_match('|^([a-z\-]+)/([a-z\-]+)$|', $timezone, $matches)) {
$timezone = ucfirst($matches[1]) . '/' . ucfirst($matches[2]);
}

if (false !== ($canon = IntlTimeZone::getCanonicalID($timezone))) {
$timezone = $canon;
}

return new Timezone($timezone);
}
}
123 changes: 123 additions & 0 deletions src/Cosmos/Extension/Number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

/**
* @package Cosmos
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace DecodeLabs\Cosmos\Extension;

use DecodeLabs\Cosmos\Locale;

/**
* @template TReturn
*/
interface Number
{
/**
* @phpstan-return ($value is null ? null : TReturn)
*/
public function format(
int|float|string|null $value,
?string $unit = null,
string|Locale|null $locale = null
): mixed;

/**
* @phpstan-return ($value is null ? null : TReturn)
*/
public function pattern(
int|float|string|null $value,
string $pattern,
string|Locale|null $locale = null
): mixed;

/**
* @phpstan-return ($value is null ? null : TReturn)
*/
public function decimal(
int|float|string|null $value,
?int $precision = null,
string|Locale|null $locale = null
): mixed;

/**
* @phpstan-return ($value is null ? null : TReturn)
*/
public function currency(
int|float|string|null $value,
?string $code,
?bool $rounded = null,
string|Locale|null $locale = null
): mixed;

/**
* @phpstan-return ($value is null ? null : TReturn)
*/
public function percent(
int|float|string|null $value,
float $total = 100.0,
int $decimals = 0,
string|Locale|null $locale = null
): mixed;

/**
* @phpstan-return ($value is null ? null : TReturn)
*/
public function scientific(
int|float|string|null $value,
string|Locale|null $locale = null
): mixed;

/**
* @phpstan-return ($value is null ? null : TReturn)
*/
public function spellout(
int|float|string|null $value,
string|Locale|null $locale = null
): mixed;

/**
* @phpstan-return ($value is null ? null : TReturn)
*/
public function ordinal(
int|float|string|null $value,
string|Locale|null $locale = null
): mixed;

/**
* @phpstan-return ($diff is null ? null : TReturn)
*/
public function diff(
int|float|string|null $diff,
?bool $invert = false,
string|Locale|null $locale = null
): mixed;

/**
* @phpstan-return ($bytes is null ? null : TReturn)
*/
public function fileSize(
?int $bytes,
string|Locale|null $locale = null
): mixed;

/**
* @phpstan-return ($bytes is null ? null : TReturn)
*/
public function fileSizeDec(
?int $bytes,
string|Locale|null $locale = null
): mixed;

/**
* @phpstan-return ($counter is null ? null : TReturn)
*/
public function counter(
int|float|string|null $counter,
bool $allowZero = false,
string|Locale|null $locale = null
): mixed;
}
Loading

0 comments on commit 3aea038

Please sign in to comment.