Coverage index

src/Helper/SiteSearchQuery.php

Lines: 81.82%

LineHitsSource
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\visitors\Helper;
6
7
/**
8
 * Query-string helpers for site search detection.
9
 */
10
final class SiteSearchQuery {
11
12
  /**
13
   * Query keys added by Views AJAX, not by the visitor.
14
   */
15
  private const AJAX_PARAMETERS = [
16
    'ajax_form',
17
    'ajax_page_state',
18
    'views_dom_id',
19
    '_drupal_ajax',
20
    '_wrapper_format',
21
  ];
22
23
  /**
24
   * Reads the query string from a URL.
25
   *
26
   * @param string $url
27
   *   A page or referrer URL.
28
   *
29
   * @return array
30
   *   The query parameters. An empty array when there are none.
31
   */
32
  public static function fromUrl(string $url): array {
3311
    if ($url === '') {
343
      return [];
35
    }
3611
    $parts = parse_url($url);
3711
    if (!is_array($parts) || empty($parts['query']) || !is_string($parts['query'])) {
389
      return [];
39
    }
404
    $query = [];
414
    parse_str($parts['query'], $query);
424
    return $query;
43
  }
44
45
  /**
46
   * Path component of a URL.
47
   *
48
   * @param string $url
49
   *   A page or referrer URL.
50
   *
51
   * @return string
52
   *   The path, or an empty string.
53
   */
54
  public static function pathFromUrl(string $url): string {
558
    if ($url === '') {
563
      return '';
57
    }
585
    $parts = parse_url($url);
595
    if (!is_array($parts)) {
600
      return '';
61
    }
625
    return (string) ($parts['path'] ?? '');
63
  }
64
65
  /**
66
   * First non-empty string in a query value.
67
   *
68
   * @param mixed $value
69
   *   A query value, which may be a list.
70
   *
71
   * @return string
72
   *   The trimmed value, or an empty string.
73
   */
74
  public static function scalar(mixed $value): string {
7511
    if (is_array($value)) {
760
      foreach ($value as $item) {
770
        $scalar = self::scalar($item);
780
        if ($scalar !== '') {
790
          return $scalar;
80
        }
81
      }
820
      return '';
83
    }
8411
    if (!is_scalar($value)) {
8511
      return '';
86
    }
875
    return trim((string) $value);
88
  }
89
90
  /**
91
   * Zero-based results page from the page query parameter.
92
   *
93
   * @param array $query
94
   *   The query parameters.
95
   *
96
   * @return int
97
   *   The page. Zero when the parameter is missing or not numeric.
98
   */
99
  public static function page(array $query): int {
1005
    $page = self::scalar($query['page'] ?? NULL);
1015
    if ($page === '') {
1025
      return 0;
103
    }
1042
    $first = explode(',', $page)[0];
1052
    if (!is_numeric($first)) {
1060
      return 0;
107
    }
1082
    return max(0, (int) $first);
109
  }
110
111
  /**
112
   * Canonical filter string.
113
   *
114
   * The keyword parameter, the page, and Views AJAX parameters are removed.
115
   * Remaining keys and list values are sorted so the same filters match in
116
   * any order.
117
   *
118
   * @param array $query
119
   *   The query parameters.
120
   * @param string[] $drop
121
   *   Parameter names to remove before sorting.
122
   *
123
   * @return string
124
   *   The filter string. Empty when no filters remain.
125
   */
126
  public static function canonical(array $query, array $drop): string {
1275
    foreach (array_merge($drop, ['page'], self::AJAX_PARAMETERS) as $key) {
1285
      unset($query[$key]);
129
    }
1305
    $query = self::sort($query);
1315
    if ($query === []) {
1323
      return '';
133
    }
1343
    return http_build_query($query);
135
  }
136
137
  /**
138
   * Sorts query keys and list values.
139
   *
140
   * @param array $query
141
   *   The query parameters.
142
   *
143
   * @return array
144
   *   The sorted parameters.
145
   */
146
  private static function sort(array $query): array {
1475
    foreach ($query as $key => $value) {
1483
      if (is_array($value)) {
1490
        $query[$key] = self::sort($value);
150
      }
151
    }
1525
    if (array_is_list($query)) {
1533
      sort($query);
1543
      return $query;
155
    }
1563
    ksort($query);
1573
    return $query;
158
  }
159
160
}