AbstractSegment::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
namespace Fhp\Segment;
4
5
/**
6
 * Abstract Class AbstractSegment.
7
 *
8
 * @package Fhp\Segment
9
 */
10
abstract class AbstractSegment implements SegmentInterface
11
{
12
    const SEGMENT_SEPARATOR = "'";
13
    const DEFAULT_COUNTRY_CODE = 280;
14
15
    /** @var string */
16
    protected $type;
17
    /** @var int */
18
    protected $segmentNumber;
19
    /** @var int */
20
    protected $version;
21
    /** @var array */
22
    protected $dataElements;
23
24
    /**
25
     * AbstractSegment constructor.
26
     *
27
     * @param $type
28
     * @param $segmentNumber
29
     * @param $version
30
     * @param array $dataElements
31
     */
32 4
    public function __construct($type, $segmentNumber, $version, array $dataElements = array())
33
    {
34 4
        $this->type = strtoupper($type);
35 4
        $this->version = $version;
36 4
        $this->segmentNumber = $segmentNumber;
37 4
        $this->dataElements = $dataElements;
38 4
    }
39
40
    /**
41
     * @param array $dataElements
42
     */
43 4
    public function setDataElements(array $dataElements = array())
44
    {
45 4
        $this->dataElements = $dataElements;
46 4
    }
47
48
    /**
49
     * @return array
50
     */
51 4
    public function getDataElements()
52
    {
53 4
        return $this->dataElements;
54
    }
55
56
    /**
57
     * @return string
58
     */
59 4
    public function toString()
60
    {
61 4
        $string = $this->type . ':' . $this->segmentNumber . ':' . $this->version;
62
63 4
        foreach ($this->dataElements as $de) {
64 4
            $string .= '+' . (string) $de;
65 4
        }
66
67 4
        return $string . static::SEGMENT_SEPARATOR;
68
    }
69
70
    /**
71
     * @return string
72
     */
73 4
    public function __toString()
74
    {
75 4
        return $this->toString();
76
    }
77
78
    /**
79
     * @param bool $translateCodes
80
     * @return string
81
     */
82 View Code Duplication
    public function humanReadable($translateCodes = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        return str_replace(
85
            array("'", '+'),
86
            array(PHP_EOL, PHP_EOL . "  "),
87
            $translateCodes
88
                ? NameMapping::translateResponse($this->toString())
89
                : $this->toString()
90
        );
91
    }
92
93
    /**
94
     * @return int
95
     */
96
    public function getSegmentNumber()
97
    {
98
        return $this->segmentNumber;
99
    }
100
101
    /**
102
     * @return int
103
     */
104
    public function getVersion()
105
    {
106
        return $this->version;
107
    }
108
}
109