Formatter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 9 2
A isSupported() 0 4 1
A getSupported() 0 4 1
1
<?php
2
3
/*
4
 * soluble-flexstore library
5
 *
6
 * @author    Vanvelthem Sébastien
7
 * @link      https://github.com/belgattitude/soluble-flexstore
8
 * @copyright Copyright (c) 2016-2017 Vanvelthem Sébastien
9
 * @license   MIT License https://github.com/belgattitude/soluble-flexstore/blob/master/LICENSE.md
10
 *
11
 */
12
13
namespace Soluble\FlexStore;
14
15
class Formatter
16
{
17
    const FORMATTER_CURRENCY = 'currency';
18
    const FORMATTER_NUMBER = 'number';
19
    const FORMATTER_UNIT = 'unit';
20
21
    /**
22
     * @var array
23
     */
24
    protected static $formattersMap = [
25
        self::FORMATTER_CURRENCY => 'Formatter\CurrencyFormatter',
26
        self::FORMATTER_NUMBER => 'Formatter\NumberFormatter',
27
        self::FORMATTER_UNIT => 'Formatter\UnitFormatter',
28
    ];
29
30
    /**
31
     * @param string $formatter_name
32
     *
33
     * @throws Exception\InvalidArgumentException
34
     *
35
     * @return \Soluble\FlexStore\Formatter\FormatterInterface
36
     */
37 3
    public static function create($formatter_name, array $params = [])
38
    {
39 3
        if (!self::isSupported($formatter_name)) {
40 1
            throw new Exception\InvalidArgumentException(__METHOD__ . " Formatter '$formatter_name' is not supported.");
41
        }
42 2
        $class = __NAMESPACE__ . '\\' . self::$formattersMap[strtolower($formatter_name)];
43
44 2
        return new $class($params);
45
    }
46
47
    /**
48
     * Whether a formatter is supported.
49
     *
50
     * @param string $formatter_name
51
     *
52
     * @return bool
53
     */
54 3
    public static function isSupported($formatter_name)
55
    {
56 3
        return array_key_exists(strtolower($formatter_name), self::$formattersMap);
57
    }
58
59
    /**
60
     * Return supported formatters.
61
     *
62
     * @return array
63
     */
64 1
    public static function getSupported()
65
    {
66 1
        return array_keys(self::$formattersMap);
67
    }
68
}
69