LocaleCollectionSortService::addLocaleByMode()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 3
dl 0
loc 17
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lochmueller\LanguageDetection\Service;
6
7
use Lochmueller\LanguageDetection\Domain\Collection\LocaleCollection;
8
use Lochmueller\LanguageDetection\Domain\Model\Dto\LocaleValueObject;
9
10
class LocaleCollectionSortService
11
{
12
    public const SORT_DEFAULT = self::SORT_AFTER;
13
    public const SORT_AFTER = 'after';
14
    public const SORT_BEFORE = 'before';
15
    public const SORT_REPLACE = 'replace';
16
17
    public function addLocaleByMode(LocaleCollection $collection, LocaleValueObject $locale, string $mode = self::SORT_DEFAULT): LocaleCollection
18
    {
19
        $base = $collection->toArray();
20
        switch ($mode) {
21
            case self::SORT_BEFORE:
22
                array_unshift($base, $locale);
23
                break;
24
            case self::SORT_REPLACE:
25
                $base = [$locale];
26
                break;
27
            case self::SORT_AFTER:
28
            default:
29
                $base[] = $locale;
30
                break;
31
        }
32
33
        return LocaleCollection::fromArrayLocales($base);
34
    }
35
}
36