Coverage index

src/Service/SequenceService.php

Lines: 96.18%

LineHitsSource
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) {
1422
    if (empty($result)) {
151
      return $result;
16
    }
17
1821
    $row = $result[0];
1921
    foreach (get_object_vars($row) as $key => $value) {
20
      switch ($key) {
21
2221
        case 'visitors_hour':
2321
        case 'visitors_local_hour':
246
          $result = self::hours($result, $key);
256
          break;
26
2721
        case 'visitors_day_of_month':
283
          $result = self::dayOfMonth($result, $key);
293
          break;
30
3121
        case 'visitors_day_of_week':
323
          $result = self::daysOfWeek($result, $key);
333
          break;
34
3521
        case 'visitors_day':
363
          $result = self::days($result, $key);
373
          break;
38
3921
        case 'visitors_week':
403
          $result = self::weeks($result, $key);
413
          break;
42
4321
        case 'visitors_month':
443
          $result = self::months($result, $key);
453
          break;
46
47
        default:
480
          break;
49
      }
50
    }
51
5221
    return $result;
53
  }
54
55
  /**
56
   * Fills the missing hours in the result.
57
   */
58
  protected static function hours($result, $field) {
596
    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
676
    $rows = [];
686
    $exclude_fields = [
696
      'index',
706
      '_entity',
716
      '_relationship_entities',
726
      $field,
736
    ];
746
    $index = 0;
756
    $expected = $result[0]->{$field};
766
    $year = (int) substr($expected, 0, 4);
776
    $month = (int) substr($expected, 4, 2);
786
    foreach ($result as $i => $row) {
796
      while ($expected < $row->{$field}) {
804
        $clone = clone $row;
814
        $clone->{$field} = $expected;
824
        $clone->index = $index;
83
844
        foreach (get_object_vars($clone) as $key => $value) {
854
          if (in_array($key, $exclude_fields)) {
860
            continue;
87
          }
884
          $clone->{$key} = '0';
89
        }
90
914
        $rows[] = $clone;
924
        $index += 1;
934
        $month += 1;
944
        if ($month > $max) {
952
          $month = 1;
962
          $year += 1;
97
        }
984
        $expected = $year . str_pad($month, 2, '0', STR_PAD_LEFT);
99
100
      }
1016
      $row->index = $index;
102
1036
      $index += 1;
1046
      $month += 1;
1056
      if ($month > $max) {
1063
        $month = 1;
1073
        $year += 1;
108
      }
1096
      $expected = $year . str_pad($month, 2, '0', STR_PAD_LEFT);
1106
      $rows[] = $row;
111
    }
112
1136
    return $rows;
114
  }
115
116
  /**
117
   * Fills the missing days in the result.
118
   */
119
  protected static function days($result, $field) {
1203
    $format = 'Y-m-d';
1213
    $increment = '+1 day';
1223
    $rows = [];
1233
    $exclude_fields = [
1243
      'index',
1253
      '_entity',
1263
      '_relationship_entities',
1273
      $field,
1283
    ];
1293
    $index = 0;
1303
    $expected = $result[0]->{$field};
1313
    foreach ($result as $i => $row) {
1323
      while ($expected < $row->{$field}) {
1332
        $clone = clone $row;
1342
        $clone->{$field} = $expected;
1352
        $clone->index = $index;
136
1372
        foreach (get_object_vars($clone) as $key => $value) {
1382
          if (in_array($key, $exclude_fields)) {
1390
            continue;
140
          }
1412
          $clone->{$key} = '0';
142
        }
143
1442
        $rows[] = $clone;
1452
        $expected = date($format, strtotime($increment, strtotime($expected)));
1462
        $index += 1;
147
      }
148
1493
      $expected = date($format, strtotime($increment, strtotime($expected)));
1503
      $rows[] = $row;
151
    }
152
1533
    return $rows;
154
  }
155
156
  /**
157
   * Fills the missing weeks in the result.
158
   */
159
  protected static function weeks($result, $field) {
1603
    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) {
1673
    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) {
1743
    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) {
1813
    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
18912
    $exclude_fields = [
19012
      'index',
19112
      '_entity',
19212
      '_relationship_entities',
19312
      $field,
19412
    ];
195
19612
    $rows = [];
19712
    $expected = $start;
19812
    $index = 0;
19912
    $row = NULL;
20012
    foreach ($result as $i => $row) {
20112
      while ($expected < (int) $row->{$field}) {
20210
        $clone = clone $row;
20310
        $clone->{$field} = str_pad($expected, 2, '0', STR_PAD_LEFT);
20410
        $clone->index = $index;
20510
        foreach (get_object_vars($clone) as $key => $value) {
20610
          if (in_array($key, $exclude_fields)) {
2070
            continue;
208
          }
20910
          $clone->{$key} = '0';
210
        }
211
21210
        $rows[] = $clone;
21310
        $index += 1;
21410
        $expected += 1;
215
      }
21612
      $row->index = $index;
21712
      $row->{$field} = str_pad($row->{$field} ?? '', 2, '0', STR_PAD_LEFT);
21812
      $rows[] = $row;
21912
      $index += 1;
22012
      $expected += 1;
221
    }
222
22312
    while (!is_null($row) && $expected < $end) {
22412
      $clone = clone $row;
22512
      $clone->{$field} = str_pad($expected, 2, '0', STR_PAD_LEFT);
22612
      $clone->index = $index;
22712
      foreach (get_object_vars($clone) as $key => $value) {
22812
        if (in_array($key, $exclude_fields)) {
2290
          continue;
230
        }
23112
        $clone->{$key} = '0';
232
      }
233
23412
      $rows[] = $clone;
23512
      $index += 1;
23612
      $expected += 1;
237
    }
238
23912
    return $rows;
240
  }
241
242
}