-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
64 lines (54 loc) · 1.79 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* Return the depth of an array
* Source: https://stackoverflow.com/a/263621
*/
function array_depth(array $array) :int {
$max_indentation = 1;
$array_str = print_r($array, true);
$lines = explode("\n", $array_str);
foreach ($lines as $line) {
$indentation = (strlen($line) - strlen(ltrim($line))) / 4;
if ($indentation > $max_indentation) {
$max_indentation = $indentation;
}
}
return ceil(($max_indentation - 1) / 2) + 1;
}
function processRecipientChoices(array $recipientChoices) :array {
$depth = array_depth($recipientChoices);
$choices = [
'addresses' => [],
'users' => [],
'domains' => []
];
$usersEqual = true;
if ($depth == 1) {
$choices['addresses'] = $recipientChoices;
} elseif ($depth == 2) {
$choices['domains'] = array_keys($recipientChoices);
foreach ($choices['domains'] as $domain) {
foreach ($choices['domains'] as $domain2) {
if ($recipientChoices[$domain] != $recipientChoices[$domain2]) {
$usersEqual = false;
$choices['users'] = [];
break 2;
} else {
$choices['users'] = $recipientChoices[$domain];
}
}
}
foreach ($choices['domains'] as $domain) {
foreach($recipientChoices[$domain] as $user) {
if (!empty($domain)) {
$choices['addresses'][] = $user . '@' . $domain;
} else {
$choices['addresses'][] = $user;
}
}
}
} else {
throw new Exception("There are to many levels for custom recipient choices available. Maximum is 2", 1);
}
return $choices;
}