Utils::readFileToArray()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 39
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 39
rs 8.439
cc 6
eloc 30
nc 4
nop 4
1
<?php
2
/**
3
 * Utils
4
 *
5
 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6
 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
7
 *
8
 * Permission is hereby granted to use or copy this program
9
 * for any purpose, provided the above notices are retained on all copies.
10
 * Permission to modify the code and to distribute modified code is granted,
11
 * provided the above notices are retained, and a notice that the code was
12
 * modified is included with the above copyright notice.
13
 *
14
 * @category  Wp
15
 * @package   Punction
16
 * @author    Andrzej Marcinkowski <[email protected]>
17
 * @copyright 2014 Wojewódzki Szpital Zespolony, Kalisz
18
 * @license   MIT http://opensource.org/licenses/MIT
19
 * @version   1.0 $Id: 654aa9ca96042e8f8814f3d117791957e31d6088 $ $Format:%H$
20
 * @link      http://
21
 * @since     File available since Release 1.0.0
22
 * PHP Version 5
23
 */
24
namespace Hospitalplugin\utils;
25
26
/**
27
 * Utils
28
 *
29
 * @category  Wp
30
 * @package   Punction
31
 * @author    Andrzej Marcinkowski <[email protected]>
32
 * @copyright 2014 Wojewódzki Szpital Zespolony, Kalisz
33
 * @license   MIT http://opensource.org/licenses/MIT
34
 * @version   1.0 $Id: 654aa9ca96042e8f8814f3d117791957e31d6088 $ $Format:%H$
35
 * @link      http://
36
 * @since     File available since Release 1.0.0
37
 *
38
 */
39
class Utils
40
{
41
42
    /**
43
     * diff in days
44
     *
45
     * @param $date1 $date1 string parsable date strtotime(date)           
0 ignored issues
show
Documentation introduced by
The doc-type $date1 could not be parsed: Unknown type name "$date1" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
46
     * @param $date2 $date2 string parsable date strtotime(date)          
0 ignored issues
show
Documentation introduced by
The doc-type $date2 could not be parsed: Unknown type name "$date2" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
47
     * @return number
48
     */
49
    public static function diffDates($date1, $date2)
50
    {
51
        $ts1 = strtotime($date1);
52
        $ts2 = strtotime($date2);
53
        
54
        $seconds_diff = $ts2 - $ts1;
55
        
56
        return floor($seconds_diff / 3600 / 24);
57
    }
58
59
    /**
60
     * next day in Y-m-d format
61
     *
62
     * @param $month int month 1-12         
63
     * @param $day int day 1-31          
64
     * @return string Y-m-d
65
     */
66
    public static function getNextDay($month, $day)
67
    {
68
        return Utils::getNextDayDate('Y-' . $month . '-' . $day);
69
    }
70
71
    /**
72
     * next day in Y-m-d format
73
     *
74
     * @param $month int month 1-12
75
     * @param $day int day 1-31
76
     * @return string Y-m-d
77
     */
78
    public static function getNextDayDate($date)
79
    {
80
        $dateString = strtotime(date($date));
81
        return date('Y-m-d', strtotime('+1 day', $dateString));
82
    }
83
84
    /**
85
     * precious day in Y-m-d format
86
     *
87
     * @param $date sttring Y-m-d
88
     * @return string Y-m-d
89
     */
90
    public static function getPreviousDay($date)
91
    {
92
        $dateMinus1 = strtotime('-1 day', strtotime($date));
93
        return date('Y-m-d', $dateMinus1);
94
    }
95
96
    /**
97
     * first day of next month
98
     *
99
     * @param month int no def val 
100
     * @param day int no def val         
101
     * @return string Y-m-d
102
     */
103
    public static function getNextMonthFirstDay($month, $day)
0 ignored issues
show
Unused Code introduced by
The parameter $day is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
    {
105
        return date('Y-m-d', strtotime('+1 month', strtotime(date('Y-' . $month . '-01'))));
106
    }
107
108
    /**
109
     * getStartDate
110
     *
111
     * Returns today if no date specified,
112
     * First date of month if just month specified,
113
     * Exact date if month and date specified.
114
     * @example getStartDate() will return today (2014-09-28)
115
     * @example getStartDate(2) will return 2014-02-01
116
     * @example getStartDate(12,7) will return 2014-12-07
117
     *
118
     * @return string date
119
     *
120
     *
121
     * @param $month int default null
122
     * @param $day int default null            
123
     */
124
    public static function getStartDate($month = null, $day = null)
125
    {
126
        if ($month == null && $day == null) {
127
            // today
128
            $date = new \DateTime(date('Y-m-d'));
129
        } else 
130
            if ($day == null) {
131
                // first day of mnth
132
                $date = new \DateTime(date('Y-') . $month . '-01');
133
            } else {
134
                // date
135
                $date = new \DateTime(date('Y-' . $month . '-' . $day));
136
            }
137
        return $date->format('Y-m-d');
138
    }
139
140
    /**
141
     * getEndDate
142
     *
143
     * @param $month int default null
144
     * @param $day int default null
145
     */
146
    public static function getEndDate($month = null, $day = null)
147
    {
148
        if ($month == null && $day == null) {
149
            // tomorrow
150
            $date = new \DateTime(Utils::getNextDay(date('m'), date('d')));
151
        } else 
152
            if ($day == null) {
153
                // first day of next mnth
154
                $date = new \DateTime(Utils::getNextMonthFirstDay($month, $day));
155
            } else {
156
                // date +1 day
157
                $date = new \DateTime(Utils::getNextDay($month, $day));
158
            }
159
        return $date->format('Y-m-d');
160
    }
161
162
    /**
163
     * getStartEndDate
164
     * 
165
     * pobierz date $_GET['date']; / domyslnie dzisiaj
166
     * 0 - dzisiaj; 1 - wczoraj; 7 - week
167
     * 
168
     * @param unknown $dateParam
169
     * @return unknown
170
     */
171
    public static function getStartEndDate($dateParam)
172
    {
173
        if (empty($dateParam) || ! in_array($dateParam, array(
174
            0,
175
            1,
176
            7
177
        ))) {
178
            $dateParam = 0;
179
        } 
180
        $date = array();
181
        $today = (new \DateTime("now"))->format("Y-m-d");
182
        $tomorrow = Utils::getNextDayDate($today);
183
        $yesterday = Utils::getPreviousDay($today);
184
        switch ($dateParam) {
185
            case 0:
186
                $date['startDate'] = $today;
187
                $date['endDate'] = $tomorrow;
188
                break;
189
            case 1:
190
                $date['startDate'] = $yesterday;
191
                $date['endDate'] = $today;
192
                break;
193
            case 7:
194
                $date['endDate'] = $tomorrow;
195
                $date['startDate'] = Utils::getDateWeekAgo();
196
                break;
197
        }
198
        return $date;
199
    }
200
201
    /**
202
     * getDateWeekAgo
203
     *
204
     * @param $month int default null
205
     * @param $day int default null
206
     */
207
    public static function getDateWeekAgo()
208
    {
209
        // today
210
        $date = new \DateTime(date('Y-m-d'));
211
        // minus 7 days
212
        $date = $date->sub(new \DateInterval('P7D'));
213
        return $date->format('Y-m-d');
214
    }
215
216
    /**
217
     * read file to array
218
     *
219
     * @param $url file url
220
     * @param $delm default ;
221
     * @param $encl default \
222
     * @param $head default false
223
     * @return array of strings
224
     */
225
    public static function readFileToArray($url, $delm = ";", $encl = "\"", $head = false)
226
    {
227
        $csvxrow = file($url); // ---- csv rows to array ----
228
        
229
        $csvxrow[0] = chop($csvxrow[0]);
230
        $csvxrow[0] = str_replace($encl, '', $csvxrow[0]);
231
        $keydata = explode($delm, $csvxrow[0]);
232
        $keynumb = count($keydata);
233
        $csv_data = array();
234
        $out = array();
235
        if ($head === true) {
236
            $anzdata = count($csvxrow);
237
            $z = 0;
238
            for ($x = 1; $x < $anzdata; $x ++) {
239
                $csvxrow[$x] = chop($csvxrow[$x]);
240
                $csvxrow[$x] = str_replace($encl, '', $csvxrow[$x]);
241
                $csv_data[$x] = explode($delm, $csvxrow[$x]);
242
                $i = 0;
243
                foreach ($keydata as $key) {
244
                    $out[$z][$key] = $csv_data[$x][$i];
245
                    $i ++;
246
                }
247
                $z ++;
248
            }
249
        } else {
250
            $i = 0;
251
            foreach ($csvxrow as $item) {
252
                $item = chop($item);
253
                $item = str_replace($encl, '', $item);
254
                $csv_data = explode($delm, $item);
255
                for ($y = 0; $y < $keynumb; $y ++) {
256
                    $out[$i][$y] = $csv_data[$y];
257
                }
258
                $i ++;
259
            }
260
        }
261
        
262
        return $out;
263
    }
264
265
    /**
266
     * PL chars conv iso8859-2 => win1250 => utf8
267
     *
268
     * @param text string with PL chars           
269
     * @return string encoded
270
     */
271
    public static function w1250_to_utf8($text)
272
    {
273
        // map based on:
274
        // http://konfiguracja.c0.pl/iso02vscp1250en.html
275
        // http://konfiguracja.c0.pl/webpl/index_en.html#examp
276
        // http://www.htmlentities.com/html/entities/
277
        $map = array(
278
            chr(0x8A) => chr(0xA9),
279
            chr(0x8C) => chr(0xA6),
280
            chr(0x8D) => chr(0xAB),
281
            chr(0x8E) => chr(0xAE),
282
            chr(0x8F) => chr(0xAC),
283
            chr(0x9C) => chr(0xB6),
284
            chr(0x9D) => chr(0xBB),
285
            chr(0xA1) => chr(0xB7),
286
            chr(0xA5) => chr(0xA1),
287
            //chr(0xBC) => chr(0xA5),
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
288
            
289
            // chr ( 0x9F ) => chr ( 0xBC ),
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
290
            chr(0xB9) => chr(0xB1),
291
            chr(0x9A) => chr(0xB9),
292
            chr(0xBE) => chr(0xB5),
293
            chr(0x9E) => chr(0xBE),
294
            chr(0x80) => '&euro;',
295
            chr(0x82) => '&sbquo;',
296
            chr(0x84) => '&bdquo;',
297
            chr(0x85) => '&hellip;',
298
            chr(0x86) => '&dagger;',
299
            chr(0x87) => '&Dagger;',
300
            chr(0x89) => '&permil;',
301
            chr(0x8B) => '&lsaquo;',
302
            chr(0x91) => '&lsquo;',
303
            chr(0x92) => '&rsquo;',
304
            chr(0x93) => '&ldquo;',
305
            chr(0x94) => '&rdquo;',
306
            chr(0x95) => '&bull;',
307
            chr(0x96) => '&ndash;',
308
            chr(0x97) => '&mdash;',
309
            chr(0x99) => '&trade;',
310
            chr(0x9B) => '&rsquo;',
311
            chr(0xA9) => '&copy;',
312
            chr(0xAB) => '&laquo;',
313
            chr(0xAE) => '&reg;',
314
            chr(0xB1) => '&plusmn;',
315
            chr(0xB5) => '&micro;',
316
            chr(0xB7) => '&middot;',
317
            chr(0xBB) => '&raquo;',
318
            
319
            // ś
320
            chr(0xB6) => '&#347;',
321
            
322
            // ą
323
            chr(0xB1) => '&#261;',
324
            
325
            // Ś
326
            chr(0xA6) => '&#346;',
327
            
328
            // ż
329
            chr(0xBF) => '&#380;',
330
            
331
            // ź
332
            chr(0x9F) => '&#378;',
333
        	chr(0xBC) => '&#378;'
334
        );
335
        return html_entity_decode(mb_convert_encoding(strtr($text, $map), 'UTF-8', 'ISO-8859-2'), ENT_QUOTES, 'UTF-8');
336
    }
337
338
    /**
339
     * Class casting
340
     *
341
     * @param string|object $destination
342
     * @param object $sourceObject
343
     * @return object
344
     */
345
    static function cast($destination, $sourceObject, $arguments = NULL)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
346
    {
347
        if (is_string($destination)) {
348
            $destination = new $destination($arguments);
349
        }
350
        $sourceReflection = new \ReflectionObject($sourceObject);
351
        $destinationReflection = new \ReflectionObject($destination);
352
        $sourceProperties = $sourceReflection->getProperties();
353
        foreach ($sourceProperties as $sourceProperty) {
354
            $sourceProperty->setAccessible(true);
355
            $name = $sourceProperty->getName();
356
            $value = $sourceProperty->getValue($sourceObject);
357
            if ($destinationReflection->hasProperty($name)) {
358
                $propDest = $destinationReflection->getProperty($name);
359
                $propDest->setAccessible(true);
360
                $propDest->setValue($destination, $value);
361
            } else {
362
                $destination->$name = $value;
363
            }
364
        }
365
        return $destination;
366
    }
367
}
368
369