1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ridibooks\Platform\Common; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* sqlDicts 통해서 얻어진 배열에 대해 자주 사용하는 함수들 |
7
|
|
|
*/ |
8
|
|
|
class DictsUtils |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Dicts 에서 특정 key를 가진 값들을 모두 얻기 |
12
|
|
|
* |
13
|
|
|
* $dicts = $db->sqlDicts('select * from tb_book'); |
14
|
|
|
* $b_ids = DictsUtils::extractValuesByKey($dicts, 'id'); |
15
|
|
|
* |
16
|
|
|
* @param $dicts |
17
|
|
|
* @param $key |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
|
|
public static function extractValuesByKey($dicts, $key) |
21
|
|
|
{ |
22
|
|
|
$return = []; |
23
|
|
|
foreach ($dicts as $dict) { |
24
|
|
|
$return[] = $dict[$key]; |
25
|
|
|
} |
26
|
|
|
return $return; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Dicts 에서 특정 key 를 기준으로 재배치(key 기준으로 하나밖에 없을떄) |
31
|
|
|
* |
32
|
|
|
* $dicts = $db->sqlDicts('select * from cpdp_books'); |
33
|
|
|
* $cpdp_books_by_bid = DictsUtils::alignByKey($dicts, 'b_id'); |
34
|
|
|
* $cpdp_books_111011110 = $tb_book_property_by_bid['111011110']; |
35
|
|
|
* |
36
|
|
|
* $cpdp_books_111011110 => ['id' => '123123', 'b_id'=>'111011110', 'title' => '바람과 함께 사라지다'] |
37
|
|
|
* |
38
|
|
|
* @param $dicts |
39
|
|
|
* @param $key |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public static function alignByKey($dicts, $key) |
43
|
|
|
{ |
44
|
|
|
$return = []; |
45
|
|
|
foreach ($dicts as $dict) { |
46
|
|
|
$return[$dict[$key]] = $dict; |
47
|
|
|
} |
48
|
|
|
return $return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Dicts 에서 특정 key 를 기준으로 재배치(key 기준으로 여러개 있을떄) |
53
|
|
|
* |
54
|
|
|
* $dicts = $db->sqlDicts('select * from tb_book_property'); |
55
|
|
|
* $tb_book_propertys_by_bid = DictsUtils::alignListByKey($dicts, 'b_id'); |
56
|
|
|
* $tb_book_propertys_111011110 = $tb_book_propertys_by_bid['111011110']; |
57
|
|
|
* |
58
|
|
|
* $tb_book_propertys_111011110 => [ |
59
|
|
|
* ['b_id'=>'111011110', 'key' => 'num_pages', 'vaule' => 123123], |
60
|
|
|
* ['b_id'=>'111011110', 'key' => 'type', 'vaule' => 'pdf'], |
61
|
|
|
* ] |
62
|
|
|
* |
63
|
|
|
* @param $dicts |
64
|
|
|
* @param $key |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public static function alignListByKey($dicts, $key) |
68
|
|
|
{ |
69
|
|
|
$return = []; |
70
|
|
|
foreach ($dicts as $dict) { |
71
|
|
|
$return[$dict[$key]][] = $dict; |
72
|
|
|
} |
73
|
|
|
return $return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Dicts 와 Dicts 끼리 join 할때 (SQL 에서 join 하지 않고 PHP에서 join) |
78
|
|
|
* |
79
|
|
|
* $dictA = $db->sqlDicts('select t_id, amount point from tb_point'); |
80
|
|
|
* $dictB = $db->sqlDicts('select t_id, amount cash from tb_cash'); |
81
|
|
|
* $dictNew = DictsUtils::join($dictA, $dictB); |
82
|
|
|
* |
83
|
|
|
* $dictNew => [ |
84
|
|
|
* [t_id => '111', 'point' => 123, 'cash' => 456], |
85
|
|
|
* [t_id => '112', 'point' => 124, 'cash' => 457], |
86
|
|
|
* ] |
87
|
|
|
* |
88
|
|
|
* @param $left_dicts |
89
|
|
|
* @param $right_dicts |
90
|
|
|
* @param int $left_dicts_column_index_to_join |
91
|
|
|
* @param int $right_dicts_column_index_to_join |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public static function join( |
|
|
|
|
95
|
|
|
$left_dicts, |
96
|
|
|
$right_dicts, |
97
|
|
|
$left_dicts_column_index_to_join = 0, |
98
|
|
|
$right_dicts_column_index_to_join = 0 |
99
|
|
|
) { |
100
|
|
|
if (count($left_dicts) == 0) { |
101
|
|
|
return $right_dicts; |
102
|
|
|
} |
103
|
|
|
if (count($right_dicts) == 0) { |
104
|
|
|
return $left_dicts; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$left_keys = array_keys($left_dicts[0]); |
108
|
|
|
$left_key_to_join = $left_keys[$left_dicts_column_index_to_join]; |
109
|
|
|
$right_keys = array_keys($right_dicts[0]); |
110
|
|
|
$right_key_to_join = $right_keys[$right_dicts_column_index_to_join]; |
111
|
|
|
|
112
|
|
|
foreach ($left_dicts as $lk => $lv) { |
113
|
|
|
foreach ($right_dicts as $rk => $rv) { |
114
|
|
|
if ($lv[$left_key_to_join] != $rv[$right_key_to_join]) { |
115
|
|
|
continue; |
116
|
|
|
} |
117
|
|
|
$left_dicts[$lk] = array_merge($lv, $rv); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$keys = []; |
122
|
|
|
foreach ($left_dicts as $dict) { |
123
|
|
|
$keys = array_unique(array_merge($keys, array_keys($dict))); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
foreach ($left_dicts as $dictKey => $dict) { |
127
|
|
|
foreach ($keys as $key) { |
128
|
|
|
if (!isset($left_dicts[$dictKey][$key])) { |
129
|
|
|
$left_dicts[$dictKey][$key] = null; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $left_dicts; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public static function convertDictsToHtmlTable($dicts) |
138
|
|
|
{ |
139
|
|
|
$th = ''; |
140
|
|
|
$tr = ''; |
141
|
|
|
foreach ($dicts as $index => $dict) { |
142
|
|
|
$td = ''; |
143
|
|
|
foreach ($dict as $key => $value) { |
144
|
|
|
if ($index == 0) { |
145
|
|
|
$th = $th . "<th align='left'>" . $key . "</th>\n"; |
146
|
|
|
} |
147
|
|
|
$td = $td . "<td align='left'>" . $value . "</td>\n"; |
148
|
|
|
} |
149
|
|
|
$tr = $tr . "<tr>" . $td . "</tr>\n"; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$html_table = " |
153
|
|
|
<table cellspacing='0' cellpadding='0' style='width: 100%; border-collapse: collapse; color: #333; font-size: 13px; line-height: 1.7em; border-top:1px solid #848484;'>" |
154
|
|
|
. "<thead>" . "<tr>" . $th . "</tr>" . "</thead>" |
155
|
|
|
. "<tbody>" . $tr . "</tbody>" . "</table>"; |
156
|
|
|
|
157
|
|
|
return $html_table; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.