Completed
Push — master ( f21c3c...9414a7 )
by Vojta
02:32
created

BaseService::priceStringToFloat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace VojtaSvoboda\CnbRates\Services;
4
5
use VojtaSvoboda\CnbRates\Providers\CnbDataProvider;
6
7
class BaseService
8
{
9
    protected $dataProvider;
10
11
    protected $baseUrl;
12
13
    protected $ident;
14
15
    public function __construct(CnbDataProvider $dataProvider)
16
    {
17
        $this->dataProvider = $dataProvider;
18
    }
19
20
    /**
21
     * Return service data
22
     *
23
     * @param $date
24
     *
25
     * @return array
26
     */
27
    public function getDataSource($date = null)
28
    {
29
        return $this->dataProvider->getData($this->getSourceUrl($date), $date, $this->ident);
30
    }
31
32
    /**
33
     * Get data as array
34
     *
35
     * @param null $date
36
     * @param int $keyIndex
37
     *
38
     * @return array
39
     *
40
     * @throws \Exception
41
     */
42
    public function getData($date = null, $keyIndex = 0)
43
    {
44
        $data = $this->dataProvider->getData($this->getSourceUrl($date), $date, $this->ident);
45
46
        return $this->transformSourceToArray($data, $keyIndex);
47
    }
48
49
    /**
50
     * Get service identifier
51
     *
52
     * @return mixed
53
     */
54
    public function getIdent()
55
    {
56
        return $this->ident;
57
    }
58
59
    /**
60
     * Get service source URL
61
     *
62
     * @param $date
63
     *
64
     * @return mixed
65
     */
66
    public function getSourceUrl($date = null)
67
    {
68
        if ($date) {
69
            $dateObj = new \DateTime($date);
70
            $date = '?datum=' . $dateObj->format('d.m.Y');
71
        }
72
73
        return $this->baseUrl . $date;
74
    }
75
76
    /**
77
     * Returns service base URL
78
     *
79
     * @return mixed
80
     */
81
    public function getBaseUrl()
82
    {
83
        return $this->baseUrl;
84
    }
85
86
    /**
87
     * Convert price string to float number
88
     *
89
     * @param $string
90
     * @param $rounding
91
     *
92
     * @return float
93
     */
94
    public function priceStringToFloat($string, $rounding = 2)
95
    {
96
        $stringWithDots = strtr($string, [',' => '.']);
97
98
        return round(floatval($stringWithDots), $rounding);
99
    }
100
101
    /**
102
     * Transform data source to array
103
     *
104
     * @param $source
105
     * @param int $keyIndex
106
     *
107
     * @return array
108
     */
109
    private function transformSourceToArray($source, $keyIndex = 0)
110
    {
111
        $return = [];
112
        $lines = preg_split('/\r\n|\n|\r/', trim($source));
113
114
        foreach($lines as $key => $line)
115
        {
116
            // file headers
117
            if ($key < 3) {
118
                continue;
119
            }
120
121
            // file lines
122
            $data = explode('|', $line);
123
            $return[$data[$keyIndex]] = $data;
124
        }
125
126
        return $return;
127
    }
128
}