Skip to content

Commit 0a20615

Browse files
committed
chore: created trait to get Language and UnitSystem options list without value repetition
1 parent 0597200 commit 0a20615

File tree

3 files changed

+28
-53
lines changed

3 files changed

+28
-53
lines changed

src/Language/Language.php

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Language;
44

5+
use ProgrammatorDev\OpenWeatherMap\Util\ClassConstantsTrait;
6+
57
class Language
68
{
9+
use ClassConstantsTrait;
10+
711
public const AFRIKAANS = 'af';
812
public const ALBANIAN = 'al';
913
public const ARABIC = 'ar';
@@ -53,53 +57,6 @@ class Language
5357

5458
public static function getList(): array
5559
{
56-
return [
57-
self::AFRIKAANS,
58-
self::ALBANIAN,
59-
self::ARABIC,
60-
self::AZERBAIJANI,
61-
self::BULGARIAN,
62-
self::CATALAN,
63-
self::CZECH,
64-
self::DANISH,
65-
self::GERMAN,
66-
self::GREEK,
67-
self::ENGLISH,
68-
self::SPANISH,
69-
self::BASQUE,
70-
self::PERSIAN_FARSI,
71-
self::FINNISH,
72-
self::FRENCH,
73-
self::GALICIAN,
74-
self::HEBREW,
75-
self::HINDI,
76-
self::CROATIAN,
77-
self::HUNGARIAN,
78-
self::INDONESIAN,
79-
self::ITALIAN,
80-
self::JAPANESE,
81-
self::KOREAN,
82-
self::LATVIAN,
83-
self::LITHUANIAN,
84-
self::MACEDONIAN,
85-
self::NORWEGIAN,
86-
self::DUTCH,
87-
self::POLISH,
88-
self::PORTUGUESE,
89-
self::PORTUGUESE_BRAZIL,
90-
self::ROMANIAN,
91-
self::RUSSIAN,
92-
self::SWEDISH,
93-
self::SLOVAK,
94-
self::SLOVENIAN,
95-
self::SERBIAN,
96-
self::THAI,
97-
self::TURKISH,
98-
self::UKRAINIAN,
99-
self::VIETNAMESE,
100-
self::CHINESE_SIMPLIFIED,
101-
self::CHINESE_TRADITIONAL,
102-
self::ZULU
103-
];
60+
return (new Language)->getClassConstants(self::class);
10461
}
10562
}

src/UnitSystem/UnitSystem.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\UnitSystem;
44

5+
use ProgrammatorDev\OpenWeatherMap\Util\ClassConstantsTrait;
6+
57
class UnitSystem
68
{
9+
use ClassConstantsTrait;
10+
711
public const METRIC = 'metric';
812
public const IMPERIAL = 'imperial';
913
public const STANDARD = 'standard';
1014

1115
public static function getList(): array
1216
{
13-
return [
14-
self::METRIC,
15-
self::IMPERIAL,
16-
self::STANDARD
17-
];
17+
return (new UnitSystem)->getClassConstants(self::class);
1818
}
1919
}

src/Util/ClassConstantsTrait.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Util;
4+
5+
trait ClassConstantsTrait
6+
{
7+
private function getClassConstants(string $className): array
8+
{
9+
$reflection = new \ReflectionClass($className);
10+
$constants = $reflection->getConstants();
11+
12+
// Sort by alphabetical order
13+
// to be more intuitive when listing values for error messages
14+
\asort($constants);
15+
16+
return $constants;
17+
}
18+
}

0 commit comments

Comments
 (0)