Lines: 96.18%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | namespace Drupal\visitors\Service; | |
| 4 | ||
| 5 | /** | |
| 6 | * Fills missing sequence in the view results. | |
| 7 | */ | |
| 8 | class SequenceService { | |
| 9 | ||
| 10 | /** | |
| 11 | * Fills the view results missing sequence. | |
| 12 | */ | |
| 13 | public static function fill($result) { | |
| 14 | 22 | if (empty($result)) { |
| 15 | 1 | return $result; |
| 16 | } | |
| 17 | ||
| 18 | 21 | $row = $result[0]; |
| 19 | 21 | foreach (get_object_vars($row) as $key => $value) { |
| 20 | switch ($key) { | |
| 21 | ||
| 22 | 21 | case 'visitors_hour': |
| 23 | 21 | case 'visitors_local_hour': |
| 24 | 6 | $result = self::hours($result, $key); |
| 25 | 6 | break; |
| 26 | ||
| 27 | 21 | case 'visitors_day_of_month': |
| 28 | 3 | $result = self::dayOfMonth($result, $key); |
| 29 | 3 | break; |
| 30 | ||
| 31 | 21 | case 'visitors_day_of_week': |
| 32 | 3 | $result = self::daysOfWeek($result, $key); |
| 33 | 3 | break; |
| 34 | ||
| 35 | 21 | case 'visitors_day': |
| 36 | 3 | $result = self::days($result, $key); |
| 37 | 3 | break; |
| 38 | ||
| 39 | 21 | case 'visitors_week': |
| 40 | 3 | $result = self::weeks($result, $key); |
| 41 | 3 | break; |
| 42 | ||
| 43 | 21 | case 'visitors_month': |
| 44 | 3 | $result = self::months($result, $key); |
| 45 | 3 | break; |
| 46 | ||
| 47 | default: | |
| 48 | 0 | break; |
| 49 | } | |
| 50 | } | |
| 51 | ||
| 52 | 21 | return $result; |
| 53 | } | |
| 54 | ||
| 55 | /** | |
| 56 | * Fills the missing hours in the result. | |
| 57 | */ | |
| 58 | protected static function hours($result, $field) { | |
| 59 | 6 | return self::integer($result, $field, 24); |
| 60 | } | |
| 61 | ||
| 62 | /** | |
| 63 | * Fills the missing date in the result. | |
| 64 | */ | |
| 65 | protected static function date($result, $field, $max) { | |
| 66 | ||
| 67 | 6 | $rows = []; |
| 68 | 6 | $exclude_fields = [ |
| 69 | 6 | 'index', |
| 70 | 6 | '_entity', |
| 71 | 6 | '_relationship_entities', |
| 72 | 6 | $field, |
| 73 | 6 | ]; |
| 74 | 6 | $index = 0; |
| 75 | 6 | $expected = $result[0]->{$field}; |
| 76 | 6 | $year = (int) substr($expected, 0, 4); |
| 77 | 6 | $month = (int) substr($expected, 4, 2); |
| 78 | 6 | foreach ($result as $i => $row) { |
| 79 | 6 | while ($expected < $row->{$field}) { |
| 80 | 4 | $clone = clone $row; |
| 81 | 4 | $clone->{$field} = $expected; |
| 82 | 4 | $clone->index = $index; |
| 83 | ||
| 84 | 4 | foreach (get_object_vars($clone) as $key => $value) { |
| 85 | 4 | if (in_array($key, $exclude_fields)) { |
| 86 | 0 | continue; |
| 87 | } | |
| 88 | 4 | $clone->{$key} = '0'; |
| 89 | } | |
| 90 | ||
| 91 | 4 | $rows[] = $clone; |
| 92 | 4 | $index += 1; |
| 93 | 4 | $month += 1; |
| 94 | 4 | if ($month > $max) { |
| 95 | 2 | $month = 1; |
| 96 | 2 | $year += 1; |
| 97 | } | |
| 98 | 4 | $expected = $year . str_pad($month, 2, '0', STR_PAD_LEFT); |
| 99 | ||
| 100 | } | |
| 101 | 6 | $row->index = $index; |
| 102 | ||
| 103 | 6 | $index += 1; |
| 104 | 6 | $month += 1; |
| 105 | 6 | if ($month > $max) { |
| 106 | 3 | $month = 1; |
| 107 | 3 | $year += 1; |
| 108 | } | |
| 109 | 6 | $expected = $year . str_pad($month, 2, '0', STR_PAD_LEFT); |
| 110 | 6 | $rows[] = $row; |
| 111 | } | |
| 112 | ||
| 113 | 6 | return $rows; |
| 114 | } | |
| 115 | ||
| 116 | /** | |
| 117 | * Fills the missing days in the result. | |
| 118 | */ | |
| 119 | protected static function days($result, $field) { | |
| 120 | 3 | $format = 'Y-m-d'; |
| 121 | 3 | $increment = '+1 day'; |
| 122 | 3 | $rows = []; |
| 123 | 3 | $exclude_fields = [ |
| 124 | 3 | 'index', |
| 125 | 3 | '_entity', |
| 126 | 3 | '_relationship_entities', |
| 127 | 3 | $field, |
| 128 | 3 | ]; |
| 129 | 3 | $index = 0; |
| 130 | 3 | $expected = $result[0]->{$field}; |
| 131 | 3 | foreach ($result as $i => $row) { |
| 132 | 3 | while ($expected < $row->{$field}) { |
| 133 | 2 | $clone = clone $row; |
| 134 | 2 | $clone->{$field} = $expected; |
| 135 | 2 | $clone->index = $index; |
| 136 | ||
| 137 | 2 | foreach (get_object_vars($clone) as $key => $value) { |
| 138 | 2 | if (in_array($key, $exclude_fields)) { |
| 139 | 0 | continue; |
| 140 | } | |
| 141 | 2 | $clone->{$key} = '0'; |
| 142 | } | |
| 143 | ||
| 144 | 2 | $rows[] = $clone; |
| 145 | 2 | $expected = date($format, strtotime($increment, strtotime($expected))); |
| 146 | 2 | $index += 1; |
| 147 | } | |
| 148 | ||
| 149 | 3 | $expected = date($format, strtotime($increment, strtotime($expected))); |
| 150 | 3 | $rows[] = $row; |
| 151 | } | |
| 152 | ||
| 153 | 3 | return $rows; |
| 154 | } | |
| 155 | ||
| 156 | /** | |
| 157 | * Fills the missing weeks in the result. | |
| 158 | */ | |
| 159 | protected static function weeks($result, $field) { | |
| 160 | 3 | return self::date($result, $field, 52); |
| 161 | } | |
| 162 | ||
| 163 | /** | |
| 164 | * Fills the missing months in the result. | |
| 165 | */ | |
| 166 | protected static function months($result, $field) { | |
| 167 | 3 | return self::date($result, $field, 12); |
| 168 | } | |
| 169 | ||
| 170 | /** | |
| 171 | * Fills the missing days of week in the result. | |
| 172 | */ | |
| 173 | protected static function daysOfWeek($result, $field) { | |
| 174 | 3 | return self::integer($result, $field, 7); |
| 175 | } | |
| 176 | ||
| 177 | /** | |
| 178 | * Fills the missing days of month in the result. | |
| 179 | */ | |
| 180 | protected static function dayOfMonth($result, $field) { | |
| 181 | 3 | return self::integer($result, $field, 32, 1); |
| 182 | } | |
| 183 | ||
| 184 | /** | |
| 185 | * Fills the missing integer in the result. | |
| 186 | */ | |
| 187 | public static function integer($result, $field, $end, $start = 0) { | |
| 188 | ||
| 189 | 12 | $exclude_fields = [ |
| 190 | 12 | 'index', |
| 191 | 12 | '_entity', |
| 192 | 12 | '_relationship_entities', |
| 193 | 12 | $field, |
| 194 | 12 | ]; |
| 195 | ||
| 196 | 12 | $rows = []; |
| 197 | 12 | $expected = $start; |
| 198 | 12 | $index = 0; |
| 199 | 12 | $row = NULL; |
| 200 | 12 | foreach ($result as $i => $row) { |
| 201 | 12 | while ($expected < (int) $row->{$field}) { |
| 202 | 10 | $clone = clone $row; |
| 203 | 10 | $clone->{$field} = str_pad($expected, 2, '0', STR_PAD_LEFT); |
| 204 | 10 | $clone->index = $index; |
| 205 | 10 | foreach (get_object_vars($clone) as $key => $value) { |
| 206 | 10 | if (in_array($key, $exclude_fields)) { |
| 207 | 0 | continue; |
| 208 | } | |
| 209 | 10 | $clone->{$key} = '0'; |
| 210 | } | |
| 211 | ||
| 212 | 10 | $rows[] = $clone; |
| 213 | 10 | $index += 1; |
| 214 | 10 | $expected += 1; |
| 215 | } | |
| 216 | 12 | $row->index = $index; |
| 217 | 12 | $row->{$field} = str_pad($row->{$field} ?? '', 2, '0', STR_PAD_LEFT); |
| 218 | 12 | $rows[] = $row; |
| 219 | 12 | $index += 1; |
| 220 | 12 | $expected += 1; |
| 221 | } | |
| 222 | ||
| 223 | 12 | while (!is_null($row) && $expected < $end) { |
| 224 | 12 | $clone = clone $row; |
| 225 | 12 | $clone->{$field} = str_pad($expected, 2, '0', STR_PAD_LEFT); |
| 226 | 12 | $clone->index = $index; |
| 227 | 12 | foreach (get_object_vars($clone) as $key => $value) { |
| 228 | 12 | if (in_array($key, $exclude_fields)) { |
| 229 | 0 | continue; |
| 230 | } | |
| 231 | 12 | $clone->{$key} = '0'; |
| 232 | } | |
| 233 | ||
| 234 | 12 | $rows[] = $clone; |
| 235 | 12 | $index += 1; |
| 236 | 12 | $expected += 1; |
| 237 | } | |
| 238 | ||
| 239 | 12 | return $rows; |
| 240 | } | |
| 241 | ||
| 242 | } |