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

Intl   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 2
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
getLocale() 0 1 ?
setLocale() 0 1 ?
getFallbackLocale() 0 1 ?
setFallbackLocale() 0 1 ?
A usingLocale() 0 15 2
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
}