1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleArrayLibrary\Categories; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use UnexpectedValueException; |
7
|
|
|
|
8
|
|
|
trait Getters |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Extracts a column from an array |
12
|
|
|
* |
13
|
|
|
* @param array $array |
14
|
|
|
* @param array $columns |
15
|
|
|
* @param bool $allRowsMustHaveAllColumns |
16
|
|
|
* |
17
|
|
|
* @return array |
18
|
|
|
* @throws UnexpectedValueException |
19
|
|
|
*/ |
20
|
|
|
public static function getColumns(array $array, array $columns, $allRowsMustHaveAllColumns = false) |
21
|
|
|
{ |
22
|
|
|
// validation |
23
|
|
|
foreach ($array as $key => $row) { |
24
|
|
|
if (!is_array($row)) { |
25
|
|
|
throw new UnexpectedValueException('Array element "' . $key . '" is not an array'); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
foreach ($columns as $key => $column) { |
29
|
|
|
if (!is_string($column) && !is_numeric($column)) { |
30
|
|
|
throw new InvalidArgumentException('Invalid column type in columns array, index "' . $key . '"'); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
if (!is_bool($allRowsMustHaveAllColumns)) { |
34
|
|
|
throw new InvalidArgumentException('allRowsMustHaveAllColumns flag must be boolean'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$return = array_fill_keys($columns, array()); |
38
|
|
|
foreach ($array as $key => $row) { |
39
|
|
|
foreach ($columns as $column) { |
40
|
|
|
if (isset($row[$column]) || array_key_exists($column, $row)) { |
41
|
|
|
$return[$column][$key] = $row[$column]; |
42
|
|
|
} elseif ($allRowsMustHaveAllColumns) { |
43
|
|
|
throw new UnexpectedValueException('Row "' . $key . '" is missing column: "' . $column . '"'); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Checks if an array is rectangular array and returns dimensions or -1 if it's not rectangular |
53
|
|
|
* |
54
|
|
|
* @param array $array |
55
|
|
|
* |
56
|
|
|
* @return int|array |
57
|
|
|
*/ |
58
|
|
|
public static function getRectangularDimensions(array $array) |
59
|
|
|
{ |
60
|
|
|
$return = -1; |
61
|
|
|
$allArrays = array_map('is_array', $array); |
62
|
|
|
// all elements are arrays, iterate through them and call the static function recursively |
63
|
|
|
if (self::allElementsEqual($allArrays, true)) { |
64
|
|
|
$elementsPerArray = array(); |
65
|
|
|
foreach ($array as $row) { |
66
|
|
|
$noElements = self::getRectangularDimensions($row); |
67
|
|
|
if ($noElements == -1) { |
68
|
|
|
return $noElements; |
69
|
|
|
} |
70
|
|
|
$elementsPerArray[] = $noElements; |
71
|
|
|
} |
72
|
|
|
if (!self::allElementsEqual($elementsPerArray)) { |
73
|
|
|
return -1; |
74
|
|
|
} else { |
75
|
|
|
$return = reset($elementsPerArray); |
76
|
|
|
$return[] = count($elementsPerArray); |
77
|
|
|
} |
78
|
|
|
} // none of the elements are arrays, return number of elements of the "bottom" array |
79
|
|
|
elseif (self::allElementsEqual($allArrays, false)) { |
80
|
|
|
$return = array(0 => count($array)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Selects random sub array |
88
|
|
|
* |
89
|
|
|
* @param array $array |
90
|
|
|
* @param int $numberOfRequiredElements |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
* @throws InvalidArgumentException |
94
|
|
|
*/ |
95
|
|
|
public static function selectRandomArrayElements(array $array, $numberOfRequiredElements) |
96
|
|
|
{ |
97
|
|
|
// validation, must be positive int or 0 |
98
|
|
|
if (!self::isLogicallyCastableToInt($numberOfRequiredElements)) { |
99
|
|
|
throw new InvalidArgumentException('Number of requested elements parameter must be a positive integer'); |
100
|
|
|
} |
101
|
|
|
if ($numberOfRequiredElements <= 0) { |
102
|
|
|
throw new InvalidArgumentException('Number of requested elements parameter must be a positive integer'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$selected = $array; |
106
|
|
|
if (count($array) > $numberOfRequiredElements) { |
107
|
|
|
// select required number of random keys |
108
|
|
|
$selectedKeys = array_rand($array, $numberOfRequiredElements); |
109
|
|
|
$selectedKeys = $numberOfRequiredElements == 1 ? [$selectedKeys] : $selectedKeys; |
110
|
|
|
// select only array members with selected random keys |
111
|
|
|
$selected = array_intersect_key($array, array_fill_keys($selectedKeys, null)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $selected; |
115
|
|
|
} |
116
|
|
|
} |