Passed
Push — master ( b7fcc1...3c5a70 )
by Sebastian
04:36
created

Localization_Event_LocaleChanged::getPrevious()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * File containing the {@link \AppLocalize\Localization\Event\LocaleChanged} class.
4
 * @package Localization
5
 * @subpackage Events
6
 * @see \AppLocalize\Localization\Event\LocaleChanged
7
 */
8
9
declare(strict_types=1);
10
11
namespace AppLocalize\Localization\Event;
12
13
use AppLocalize\Localization;
14
use AppLocalize\Localization_Event;
15
use AppLocalize\Localization_Exception;
16
use AppLocalize\Localization_Locale;
17
18
/**
19
 * Event class: used for the "LocaleChanged" event. Provides
20
 * an easy-to-use API to work with the event details.
21
 * 
22
 * @package Localization
23
 * @subpackage Events
24
 * @author Sebastian Mordziol <[email protected]>
25
 *
26
 * @see Localization::selectLocaleByNS()
27
 */
28
class LocaleChanged extends Localization_Event
29
{
30
    public const ERROR_NO_CURRENT_LOCALE_SPECIFIED = 91401;
31
32
    /**
33
    * The locale that was used before the change, if any.
34
    * @return Localization_Locale|NULL
35
    */
36
    public function getPrevious() : ?Localization_Locale
37
    {
38
        $arg = $this->getArgument(1);
39
        if($arg instanceof Localization_Locale) {
40
            return $arg;
41
        }
42
        
43
        return null;
44
    }
45
46
    /**
47
     * The locale that is used now after the change.
48
     *
49
     * @return Localization_Locale
50
     * @throws Localization_Exception
51
     */
52
    public function getCurrent() : Localization_Locale
53
    {
54
        $arg = $this->getArgument(2);
55
56
        if($arg instanceof Localization_Locale) {
57
            return $arg;
58
        }
59
60
        throw new Localization_Exception(
61
            'No current locale available in event',
62
            'The current locale parameter was not a locale instance.',
63
            self::ERROR_NO_CURRENT_LOCALE_SPECIFIED
64
        );
65
    }
66
    
67
   /**
68
    * The namespace in which the locale change occurred.
69
    * @return string
70
    */
71
    public function getNamespace() : string
72
    {
73
        return strval($this->getArgument(0));
74
    }
75
76
   /**
77
    * Whether the change occurred for an application locale.
78
    * @return bool
79
    */
80
    public function isAppLocale() : bool
81
    {
82
        return $this->getNamespace() === Localization::NAMESPACE_APPLICATION;
83
    }
84
    
85
   /**
86
    * Whether the change occurred for a content locale.
87
    * @return bool
88
    */
89
    public function isContentLocale() : bool
90
    {
91
        return $this->getNamespace() === Localization::NAMESPACE_CONTENT;
92
    }
93
}
94