Completed
Pull Request — master (#18)
by Propa
09:42
created

Intl::usingLocale()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 1
nop 3
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Propaganistas\LaravelIntl\Contracts;
4
5
abstract class Intl
6
{
7
    /**
8
     * Get the current locale.
9
     *
10
     * @return string
11
     */
12
    abstract public function getLocale();
13
14
    /**
15
     * Set the current locale.
16
     *
17
     * @param $locale
18
     * @return $this
19
     */
20
    abstract public function setLocale($locale);
21
22
    /**
23
     * Get the fallback locale.
24
     *
25
     * @return string
26
     */
27
    abstract public function getFallbackLocale();
28
29
    /**
30
     * Set the fallback locale.
31
     *
32
     * @param $locale
33
     * @return $this
34
     */
35
    abstract public function setFallbackLocale($locale);
36
37
    /**
38
     * Run the given callable while using another locale.
39
     *
40
     * @param string $locale
41
     * @param callable $callback
42
     * @param string|null $fallbackLocale
43
     * @return mixed
44
     */
45 15
    public function usingLocale($locale, callable $callback, $fallbackLocale = null)
46
    {
47 15
        $originalLocale = $this->getLocale();
48 15
        $originalFallbackLocale = $this->getFallbackLocale();
49
50 15
        $this->setLocale($locale);
51 15
        $this->setFallbackLocale($fallbackLocale ?: $originalFallbackLocale);
52
53 15
        $result = $callback($this);
54
55 15
        $this->setFallbackLocale($originalFallbackLocale);
56 15
        $this->setLocale($originalLocale);
57
58 15
        return $result;
59
    }
60
}