-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCompare.php
More file actions
133 lines (117 loc) · 4.2 KB
/
Copy pathCompare.php
File metadata and controls
133 lines (117 loc) · 4.2 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
declare(strict_types=1);
/*
* UserFrosting Framework (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/framework
* @copyright Copyright (c) 2013-2024 Alexander Weissman, Louis Charette, Jordan Mele
* @license https://github.com/userfrosting/framework/blob/master/LICENSE.md (MIT License)
*/
namespace UserFrosting\I18n;
use Illuminate\Support\Arr;
/**
* Helper class to compare two dictionaries.
* Can be used to determine which keys/values are missing from a dictionary (right) compared to a base directory (left).
*/
class Compare
{
/**
* Compares keys and values from left dictionary against right dictionary and returns the difference.
*
* @param DictionaryInterface $leftDictionary
* @param DictionaryInterface $rightDictionary
* @param bool $undot If true, return the dot notation associative array. If false, returns the normal multidimensional associative array [Default: true]
*
* @return mixed[]
*/
public static function dictionaries(
DictionaryInterface $leftDictionary,
DictionaryInterface $rightDictionary,
bool $undot = false
): array {
$diff = array_diff_assoc(
$leftDictionary->getFlattenDictionary(),
$rightDictionary->getFlattenDictionary()
);
if ($undot) {
return self::undot($diff);
}
return $diff;
}
/**
* Compares keys from left dictionary against right dictionary.
* Returns a list of keys present in the left dictionary, but not found in the right one.
* Can be used to sync both dictionary content.
*
* @param DictionaryInterface $leftDictionary
* @param DictionaryInterface $rightDictionary
*
* @return string[] List of keys
*/
public static function dictionariesKeys(
DictionaryInterface $leftDictionary,
DictionaryInterface $rightDictionary
): array {
$diff = array_diff_key(
$leftDictionary->getFlattenDictionary(),
$rightDictionary->getFlattenDictionary()
);
return array_keys($diff);
}
/**
* Compares values from left dictionary against right dictionary to find same values.
* Returns a list of values which are the same in both dictionaries.
* Can be used to find all values in the right directory that might not have been translated compared to the left one.
* For example, when comparing french and english dictionaries, this can be used to return all English values present in the French dictionary.
*
* @param DictionaryInterface $leftDictionary
* @param DictionaryInterface $rightDictionary
* @param bool $undot If true, return the dot notation associative array. If false, returns the normal multidimensional associative array [Default: false]
*
* @return string[]
*/
public static function dictionariesValues(
DictionaryInterface $leftDictionary,
DictionaryInterface $rightDictionary,
bool $undot = false
): array {
$diff = array_intersect_assoc(
$leftDictionary->getFlattenDictionary(),
$rightDictionary->getFlattenDictionary()
);
if ($undot) {
return self::undot($diff);
}
return $diff;
}
/**
* Returns all keys for which the value is empty.
* Can be used to find all keys that needs to be translated in the dictionary.
*
* @param DictionaryInterface $dictionary
*
* @return string[] List of keys
*/
public static function dictionariesEmptyValues(DictionaryInterface $dictionary): array
{
$diff = array_filter($dictionary->getFlattenDictionary(), function ($value) {
return $value == '';
});
return array_keys($diff);
}
/**
* Transfer dot notation associative array back into multidimensional associative array.
*
* @param string[] $array
*
* @return string[]
*/
protected static function undot(array $array): array
{
$result = [];
foreach ($array as $key => $value) {
Arr::set($result, $key, $value);
}
return $result;
}
}