Completed
Push — master ( 01a024...a47183 )
by Marcus
02:07
created

AbstractType::getCsv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Abstract Collmex Type Class
4
 *
5
 * @author    Marcus Jaschen <[email protected]>
6
 * @license   http://www.opensource.org/licenses/mit-license MIT License
7
 * @link      https://github.com/mjaschen/collmex
8
 */
9
10
namespace MarcusJaschen\Collmex\Type;
11
12
use JsonSerializable;
13
use MarcusJaschen\Collmex\Csv\GeneratorInterface;
14
use MarcusJaschen\Collmex\Csv\SimpleGenerator;
15
use MarcusJaschen\Collmex\Type\Exception\InvalidFieldNameException;
16
17
/**
18
 * Abstract Collmex Type Class
19
 *
20
 * @author   Marcus Jaschen <[email protected]>
21
 * @license  http://www.opensource.org/licenses/mit-license MIT License
22
 * @link     https://github.com/mjaschen/collmex
23
 */
24
abstract class AbstractType implements JsonSerializable
25
{
26
    /**
27
     * All data fields for the type, ordered.
28
     *
29
     * @var array
30
     */
31
    protected $template;
32
33
    /**
34
     * The type data fields (ordered)
35
     *
36
     * @var array
37
     */
38
    protected $data;
39
40
    /**
41
     * @var array
42
     */
43
    protected $validationErrors;
44
45
    /**
46
     * @var GeneratorInterface
47
     */
48
    protected $csvGenerator;
49
50
    /**
51
     * @param array $data type data
52
     * @param \MarcusJaschen\Collmex\Csv\GeneratorInterface $generator This
53
     * argument is optional, the SimpleGenerator is used if argument is
54
     * omitted
55
     */
56
    public function __construct($data, GeneratorInterface $generator = null)
57
    {
58
        $this->populateData($data);
59
60
        if (is_null($generator)) {
61
            $this->csvGenerator = new SimpleGenerator();
62
        }
63
    }
64
65
    /**
66
     * @param GeneratorInterface $generator
67
     */
68
    public function setCsvGenerator(GeneratorInterface $generator)
69
    {
70
        $this->csvGenerator = $generator;
71
    }
72
73
    /**
74
     * Builds the CSV representation of the Collmex type.
75
     *
76
     * @return array of CSV lines
77
     */
78
    public function getCsv()
79
    {
80
        return $this->csvGenerator->generate([$this->data]);
81
    }
82
83
    /**
84
     * Returns the complete data array
85
     *
86
     * @return array
87
     */
88
    public function getData()
89
    {
90
        return $this->data;
91
    }
92
93
    /**
94
     * Returns the complete data as JSON
95
     *
96
     * @return string
97
     */
98
    public function getJSON()
99
    {
100
        return json_encode($this->data);
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function jsonSerialize()
107
    {
108
        return $this->data;
109
    }
110
111
    /**
112
     * Read record field
113
     *
114
     * @param string $name The field name
115
     *
116
     * @throws \MarcusJaschen\Collmex\Type\Exception\InvalidFieldNameException
117
     *
118
     * @return mixed
119
     */
120
    public function __get($name)
121
    {
122
        if (isset($this->data[$name])) {
123
            return $this->data[$name];
124
        }
125
126
        throw new InvalidFieldNameException("Cannot read field value; field '{$name}' does not exist in class " . get_class($this));
127
    }
128
129
    /**
130
     * @param string $name The field name
131
     * @param mixed $value The new field value
132
     *
133
     * @throws \MarcusJaschen\Collmex\Type\Exception\InvalidFieldNameException
134
     */
135
    public function __set($name, $value)
136
    {
137
        if ($name == 'type_identifier') {
138
            throw new InvalidFieldNameException("Cannot overwrite type identifier");
139
        }
140
141
        if (array_key_exists($name, $this->template)) {
142
            $this->data[$name] = $value;
143
144
            return;
145
        }
146
147
        throw new InvalidFieldNameException("Cannot set field value; field '{$name}' does not exist in class " . get_class($this));
148
    }
149
150
    /**
151
     * @param string $name The field name
152
     *
153
     * @return bool
154
     */
155
    public function __isset($name)
156
    {
157
        return isset($this->data[$name]);
158
    }
159
160
    /**
161
     * Populates the $data attribute with the given array
162
     *
163
     * @param array $data if the array is indexed by numeric keys (first key
164
     * is checked), we'll merge the data by index order.
165
     */
166
    protected function populateData($data)
167
    {
168
        if (! isset($data[0])) {
169
            $this->data = array_merge($this->template, $data);
170
171
            return;
172
        }
173
174
        $index = 0;
175
176
        foreach ($this->template as $key => $value) {
177
            if (isset($data[$index])) {
178
                $this->data[$key] = $data[$index];
179
            }
180
            $index++;
181
        }
182
    }
183
}
184