ElectricityXMLParser::setXMLElement()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Dalen\OWLPacketInterceptor\Parser\SpecificParser;
4
5
use Dalen\OWLPacketInterceptor\Parser\IParser;
6
use Dalen\OWLPacketInterceptor\Packet\Electricity\Electricity;
7
8
/**
9
 * Description of ElectricityXMLParser
10
 *
11
 * @author danieleorler
12
 */
13
class ElectricityXMLParser implements IParser
14
{
15
    private $xmlElement;
16
    
17
    /*
18
     * sets the XMLElement containing packet's data
19
     * @parameter \SimpleXMLElement $xmlElement
20
     */
21 3
    public function setXMLElement(\SimpleXMLElement $xmlElement)
22
    {
23 3
        $this->xmlElement = $xmlElement;
24 3
    }
25
    
26
    /*
27
     * returns the Electricity implementation of IPacket
28
     */
29 2
    public function parse()
30
    {
31 2
        $data            = array();
32 2
        $data['id']      = (string)$this->xmlElement->attributes()['id'];
33 2
        $data['chan']    = array();
34
35 2
        foreach($this->xmlElement->signal->attributes() AS $k => $v)
36
        {
37 2
            $data['signal'][$k] = (string)$v;
38 2
        }
39
        
40 2
        foreach($this->xmlElement->battery->attributes() AS $k => $v)
41
        {
42 2
            $data['battery'][$k] = (string)$v;
43 2
        }
44
        
45 2
        foreach($this->xmlElement->chan AS $chan)
46
        {
47 2
            $channel                    = array();
48 2
            $channel['id']              = (int)$chan->attributes()['id'];
49 2
            $channel['curr']['units']   = (string)$chan->curr->attributes()['units'];
50 2
            $channel['curr']['value']   = (float)$chan->curr;
51 2
            $channel['day']['units']    = (string)$chan->day->attributes()['units'];
52 2
            $channel['day']['value']    = (float)$chan->day;
53
            
54 2
            $data['chan'][]      = $channel;
55 2
        }
56
        
57 2
        return new Electricity($data);
58
    }
59
}
60