|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CSVelte: Slender, elegant CSV for PHP |
|
4
|
|
|
* Inspired by Python's CSV module and Frictionless Data and the W3C's CSV |
|
5
|
|
|
* standardization efforts, CSVelte was written in an effort to take all the |
|
6
|
|
|
* suck out of working with CSV. |
|
7
|
|
|
* |
|
8
|
|
|
* @version v0.2 |
|
9
|
|
|
* @copyright Copyright (c) 2016 Luke Visinoni <[email protected]> |
|
10
|
|
|
* @author Luke Visinoni <[email protected]> |
|
11
|
|
|
* @license https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT) |
|
12
|
|
|
* @internal |
|
13
|
|
|
*/ |
|
14
|
|
|
namespace CSVelte; |
|
15
|
|
|
/** |
|
16
|
|
|
* CSVelte Utility Tool Belt |
|
17
|
|
|
* |
|
18
|
|
|
* This is a heinously ugly class full of static methods for performing various |
|
19
|
|
|
* useful functions such as removing an element from an array by value, averaging |
|
20
|
|
|
* the values of an erray, etc. |
|
21
|
|
|
* |
|
22
|
|
|
* *Note:* Don't get used to this class, it is almost certainly going away eventuallly |
|
23
|
|
|
* |
|
24
|
|
|
* @package CSVelte |
|
25
|
|
|
* @subpackage Utilities |
|
26
|
|
|
* @since v0.1 |
|
27
|
|
|
* @internal |
|
28
|
|
|
* @todo Either clean this up immensely, maybe by turning it into a collection |
|
29
|
|
|
* object class or move them to namespaced functions. Or, if you can stomach |
|
30
|
|
|
* the idea of adding another dependency, just use an existing utility |
|
31
|
|
|
* library such as Underscore or something. |
|
32
|
|
|
*/ |
|
33
|
|
|
class Utils |
|
34
|
|
|
{ |
|
35
|
20 |
|
public static function array_get($arr, $key, $default = null, $throwException = false) |
|
36
|
|
|
{ |
|
37
|
20 |
|
if (array_key_exists($key, $arr)) { |
|
38
|
17 |
|
return $arr[$key]; |
|
39
|
|
|
} else { |
|
40
|
15 |
|
if ($throwException) { |
|
41
|
|
|
// @todo is this the correct exception to throw? |
|
42
|
6 |
|
throw new \OutOfBoundsException('Unknown array index: ' . $key); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
9 |
|
return $default; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
public static function array_items($arr) |
|
49
|
|
|
{ |
|
50
|
1 |
|
$items = array(); |
|
51
|
1 |
|
foreach ($arr as $key => $val) { |
|
52
|
1 |
|
$items[] = array($key, $val); |
|
53
|
1 |
|
} |
|
54
|
1 |
|
return $items; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
9 |
|
public static function average($arr) |
|
58
|
|
|
{ |
|
59
|
9 |
|
if (!is_array($arr)) throw new \InvalidArgumentException('"average" function expected array, got ' . gettype($arr)); |
|
60
|
8 |
|
return array_sum($arr) / count($arr); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
7 |
|
public static function array_average($arr) |
|
64
|
|
|
{ |
|
65
|
7 |
|
$return = array(); |
|
66
|
7 |
|
foreach ($arr as $key => $val) { |
|
67
|
7 |
|
$return[$key] = self::average($val); |
|
68
|
7 |
|
} |
|
69
|
6 |
|
return $return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
8 |
|
public static function mode($arr) |
|
73
|
|
|
{ |
|
74
|
8 |
|
if (!is_array($arr)) throw new \InvalidArgumentException('"mode" function expected array, got ' . gettype($arr)); |
|
75
|
8 |
|
$vals = array(); |
|
76
|
8 |
|
foreach ($arr as $key => $val) { |
|
77
|
8 |
|
$vals[$val] = self::array_get($vals, $val, 0) + 1; |
|
78
|
8 |
|
} |
|
79
|
8 |
|
arsort($vals); |
|
80
|
8 |
|
return key($vals); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
7 |
|
public static function array_mode($arr) |
|
84
|
|
|
{ |
|
85
|
7 |
|
$return = array(); |
|
86
|
7 |
|
foreach ($arr as $key => $val) { |
|
87
|
7 |
|
$return[$key] = self::mode($val); |
|
88
|
7 |
|
} |
|
89
|
6 |
|
return $return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Uses array_map to apply a callback to each character in a string |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public static function string_map($str, Callable $callable) |
|
96
|
|
|
{ |
|
97
|
1 |
|
return join(array_map($callable, str_split($str))); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|