FormatterAwareTrait::setFormatter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Shared\Message\Formatter;
16
17
/**
18
 * FormatterAwareTrait
19
 *
20
 * One implementation of `FormatterAwareInterface`
21
 *
22
 * @package Phossa2\Shared
23
 * @author  Hong Zhang <[email protected]>
24
 * @see     FormatterAwareInterface
25
 * @version 2.0.0
26
 * @since   2.0.0 added
27
 */
28
trait FormatterAwareTrait
29
{
30
    /**
31
     * Message formatter
32
     *
33
     * Formatter is shared by all descendant message classes.
34
     *
35
     * @var    FormatterInterface
36
     * @access private
37
     */
38
    private static $formatter;
39
40
    /**
41
     * Set formatter
42
     *
43
     * @param  FormatterInterface $formatter the message formatter
44
     * @access public
45
     * @api
46
     */
47
    public static function setFormatter(
48
        FormatterInterface $formatter
49
    ) {
50
        self::$formatter = $formatter;
51
    }
52
53
    /**
54
     * Unset formatter
55
     *
56
     * @access public
57
     * @api
58
     */
59
    public static function unsetFormatter()
60
    {
61
        self::$formatter = null;
62
    }
63
64
    /**
65
     * Get formatter. if not set, use the `DefaultFormatter`
66
     *
67
     * @return FormatterInterface
68
     * @access protected
69
     */
70
    protected static function getFormatter()/*# : FormatterInterface */
71
    {
72
        if (null === self::$formatter) {
73
            self::$formatter = new DefaultFormatter();
74
        }
75
        return self::$formatter;
76
    }
77
}
78