Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toLocaleString() method to array #47

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ From Hacktoberfest:
- Test coverage at 100%
- Arr::isEmpty() methods thanks to @Tautve for the implementation and @RoadSigns for the review
- Allow Arr::flatMap() to support multidimensional arrays by @RoadSigns
- Add toLocaleString() to get the string representation of the elements in the array

## 0.0.9 - 2022-10-02
From Hacktoberfest:
Expand Down
20 changes: 20 additions & 0 deletions doc/arr.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,23 @@ var_dump($arr->copyWithin(-2, -3, -1));

```

## Return a string representing the elements in the array using a locale and timezone

Default locale is en_US, and the timezone is UTC, in order to use a different locale make sure it is installed.

To check available locales in linux based systems run `locale -a`

To install a missing locale in linux based systems run `sudo apt-get install language-pack-XX`

```php
use HiFolks\DataType\Arr;

$arr = Arr::make([1, 2, 3, 'a', 'abc', 123456.4, "2022/10/01"]);
var_dump($arr->toLocaleString()); // Using en_US as default locale and UTC as default timezone
// 1,2,3,a,abc,123,456.40,Sat 01 Oct 2022 12:00:00 AM UTC

$arr = Arr::make([1, 2, 3, 'a', 'abc', 123456.4, "2022/10/01"]);
var_dump($arr->toLocaleString("fr_FR.utf8", "Europe/Paris")); // Using provided locale and timezone
// [1,2,3,a,abc,123 456,40,sam. 01 oct. 2022 00:00:00]
```

6 changes: 6 additions & 0 deletions examples/cheatsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,9 @@ function print_result(mixed $something): void
print_result($arr->copyWithin(-2, -3, -1));
// [1, 2, 3, 3, 4]
}

// Return a string representing the elements of the array
$arr = Arr::make([1, 2, 3, 'a', 'abc', 123456.4, '2022/10/01']);

print_r($arr->toLocaleString()); // Default locale and timezone
print_r($arr->toLocaleString('fr_FR.utf8', 'Europe/Paris')); // Provided locale and timezone
53 changes: 53 additions & 0 deletions src/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,4 +726,57 @@ public function isEmpty(): bool
{
return $this->length() === 0;
}

/**
* Returns a string representing the elements of the array
* @return string the string representation
*/
public function toLocaleString($locale = 'en_US.utf8', $timezone = 'UTC'): string
xanaDev marked this conversation as resolved.
Show resolved Hide resolved
{
if ($this->length() <= 0) {
xanaDev marked this conversation as resolved.
Show resolved Hide resolved
return '';
}

date_default_timezone_set($timezone);
$currentLocale = setlocale(LC_ALL, $locale);
$localeConfig = localeconv();

$result = $this->map(function ($item) use ($localeConfig) {
if (is_numeric($item)) {
$item = number_format(
$item,
is_float($item) ? 2 : 0,
$localeConfig['decimal_point'] ?? '.',
$localeConfig["thousands_sep"] ?? ','
);
} elseif ($this->isDate($item)) {
$date = date_parse($item);
$item = strftime(
'%c',
mktime($date['hour'], $date['minute'], $date['second'], $date['month'], $date['day'], $date['year'])
);
}

return $item;
});

return $result->toString();
}

/**
* Checks whether a value is a valid date
* @return boolean true if value is a valid date, false otherwise
*/
protected function isDate($value): bool
xanaDev marked this conversation as resolved.
Show resolved Hide resolved
{
if (!$value) {
return false;
}

$date = date_parse($value);

return $date['error_count'] == 0
&& $date['warning_count'] == 0
&& checkdate($date['month'], $date['day'], $date['year']);
}
}
45 changes: 45 additions & 0 deletions tests/ArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,3 +625,48 @@
expect($result->length())->toEqual(4);
expect($result->arr())->toEqual([2, 4, 6, 8]);
});

it('tests setLocaleString() returns an empty string for empty array', function () {
$arr = Arr::make([]);

$result = $arr->toLocaleString();
expect($result)
->toBeString()
->toEqual('');
});

it("tests setLocaleString() returns a string representing the elements of the array", function () {
$arr = Arr::make(['🥝', '🍎', 'I_DONT_KNOW', 1, 2, 3]);

$result = $arr->toLocaleString();
expect($result)
->toBeString()
->toEqual('🥝,🍎,I_DONT_KNOW,1,2,3');
});

it("tests setLocaleString() transforms dates and numbers using default locale and timezone", function () {
$arr = Arr::make([-123897.23, +123456.03, 'a', '2022-10-01']);

$result = $arr->toLocaleString(); // 'en_US.utf8', 'UTC'
expect($result)
->toBeString()
->toEqual('-123,897.23,123,456.03,a,Sat 01 Oct 2022 12:00:00 AM UTC');
});

it("tests setLocaleString() transforms dates and numbers using provided locale and timezone", function () {
$arr = Arr::make([-123897.23, +123456.03, 'a', '2022-10-01']);

$result = $arr->toLocaleString('fr_FR.utf8', 'Europe/Paris');
expect($result)
->toBeString()
->toEqual('-123 897,23,123 456,03,a,sam. 01 oct. 2022 00:00:00');
});

it("tests setLocaleString() skips nulls and invalid dates", function () {
$arr = Arr::make(['product', 123456.4, null, '2020-14-14']);

$result = $arr->toLocaleString();
expect($result)
->toBeString()
->toEqual('product,123,456.40,,2020-14-14');
});