Failed Conditions
Pull Request — master (#32)
by Moesjarraf
06:49
created

NumberFormat   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 39
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D applyToNode() 0 29 11
1
<?php
2
3
namespace LegalThings\DataEnricher\Processor;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
use NumberFormatter;
8
9
/**
10
 * NumberFormat processor
11
 */
12
class NumberFormat implements Processor
13
{
14
    use Processor\Implementation;
15
    
16
    /**
17
     * Apply processing to a single node
18
     * 
19
     * @param Node $node
20
     */
21
    public function applyToNode(Node $node)
22
    {
23
        $instruction = $node->getInstruction($this);
24
        
25
        if (is_array($instruction) || is_object($instruction)) {
26
            $instruction = (object)$instruction;
27
        }
28
        
29
        if (!isset($instruction) || !isset($instruction->input)) {
30
            return;
31
        }
32
        
33
        $locale = isset($instruction->locale) ? $instruction->locale : 'en_US';
34
        $decimals = isset($instruction->decimals) ? $instruction->decimals : null;
35
        $currency = isset($instruction->currency) ? $instruction->currency : null;
36
        $style = $currency ? NumberFormatter::CURRENCY : NumberFormatter::DECIMAL;
37
        
38
        $number = new NumberFormatter($locale, $style);
39
        
40
        if (isset($decimals)) {
41
            $number->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimals);
42
        }
43
        
44
        $result = $currency ?
45
            $number->formatCurrency($instruction->input, $currency) :
46
            $number->format($instruction->input);
47
        
48
        $node->setResult($result);
49
    }
50
}
51