AbstractConverter   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 36
dl 0
loc 133
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setDefaultOutputDirectory() 0 3 1
A convertArrays() 0 5 2
A save() 0 19 5
A getFields() 0 11 3
A setFields() 0 5 1
A recursiveImplode() 0 12 2
A setOutputDirectory() 0 9 2
1
<?php
2
3
namespace MLD\Converter;
4
5
/**
6
 * Class AbstractConverter
7
 * @package MLD\Converter
8
 */
9
abstract class AbstractConverter implements ConverterInterface
10
{
11
12
    /**
13
     * @var string path of the output directory
14
     */
15
    private $outputDirectory;
16
17
    /** @var array defines the fields to keep */
18
    private $fields;
19
20
    /** @var array */
21
    protected $countries;
22
23
    /**
24
     * @param array $countries
25
     */
26
    public function __construct(array $countries)
27
    {
28
        $this->countries = $countries;
29
    }
30
31
    /**
32
     * Save the data to disk
33
     * @param string $outputFile name of the output file
34
     * @return int|bool
35
     */
36
    public function save($outputFile = '')
37
    {
38
        if (empty($this->outputDirectory)) {
39
            $this->setDefaultOutputDirectory();
40
        }
41
        if (!is_dir($this->outputDirectory)) {
42
            mkdir($this->outputDirectory);
43
        }
44
        if (empty($outputFile)) {
45
            $outputFile = date('Ymd-His', time()) . '-countries';
46
        }
47
48
        // keep only the specified fields
49
        if (!empty($this->fields)) {
50
            array_walk($this->countries, function (&$country) {
51
                $country = array_intersect_key($country, array_flip($this->fields));
52
            });
53
        }
54
        return file_put_contents($this->outputDirectory . $outputFile, $this->convert());
55
    }
56
57
    /**
58
     * Set the directory to which output will be written.
59
     *
60
     * @param string $outputDirectory
61
     * @return $this
62
     */
63
    public function setOutputDirectory($outputDirectory)
64
    {
65
        if (substr($outputDirectory, strlen($outputDirectory) - 1, 1) !== DIRECTORY_SEPARATOR) {
66
            $outputDirectory .= DIRECTORY_SEPARATOR;
67
        }
68
69
        $this->outputDirectory = $outputDirectory;
70
71
        return $this;
72
    }
73
74
    /**
75
     * Defines the fields to keep
76
     * @param array $fields
77
     * @return $this
78
     */
79
    public function setFields(array $fields)
80
    {
81
        $this->fields = $fields;
82
83
        return $this;
84
    }
85
86
    /**
87
     * Gets fields that will currently be output.
88
     * @return array A list of field names.
89
     */
90
    public function getFields()
91
    {
92
        if ($this->fields !== null) {
93
            return $this->fields;
94
        }
95
96
        if (empty($this->countries)) {
97
            return array();
98
        }
99
100
        return array_keys($this->countries[0]);
101
    }
102
103
    /**
104
     * Converts arrays to comma-separated strings
105
     * @param array $input
106
     * @param string $glue
107
     * @return array
108
     */
109
    protected function convertArrays(array &$input, $glue = ',')
110
    {
111
        return array_map(function ($value) use ($glue) {
112
            return is_array($value) ? $this->recursiveImplode($value, $glue) : $value;
113
        }, $input);
114
    }
115
116
    /**
117
     * Set the default output directory
118
     */
119
    private function setDefaultOutputDirectory()
120
    {
121
        $this->outputDirectory = __DIR__ . DIRECTORY_SEPARATOR . 'dist' . DIRECTORY_SEPARATOR;
122
    }
123
124
    /**
125
     * Recursively implode elements
126
     * @param array $input
127
     * @param string $glue
128
     * @return string the array recursively imploded with the glue
129
     */
130
    private function recursiveImplode(array $input, $glue)
131
    {
132
        // remove empty strings from the array
133
        $input = array_filter($input, function ($entry) {
134
            return $entry !== '';
135
        });
136
        array_walk($input, function (&$value) use ($glue) {
137
            if (is_array($value)) {
138
                $value = $this->recursiveImplode($value, $glue);
139
            }
140
        });
141
        return implode($glue, $input);
142
    }
143
}