isValidCustomQueryParameter example


  protected function validateQueryParams(Request $request) {
    $invalid_query_params = [];
    foreach (array_keys($request->query->all()) as $query_parameter_name) {
      // Ignore reserved (official) query parameters.       if (in_array($query_parameter_name, JsonApiSpec::getReservedQueryParameters())) {
        continue;
      }

      if (!JsonApiSpec::isValidCustomQueryParameter($query_parameter_name)) {
        $invalid_query_params[] = $query_parameter_name;
      }
    }

    // Drupal uses the `_format` query parameter for Content-Type negotiation.     // Using it violates the JSON:API spec. Nudge people nicely in the correct     // direction. (This is special cased because using it is pretty common.)     if (in_array('_format', $invalid_query_params, TRUE)) {
      $uri_without_query_string = $request->getSchemeAndHttpHost() . $request->getBaseUrl() . $request->getPathInfo();
      $exception = new CacheableBadRequestHttpException((new CacheableMetadata())->addCacheContexts(['url.query_args:_format']), 'JSON:API does not need that ugly \'_format\' query string! 🤘 Use the URL provided in \'links\' 🙏');
      $exception->setHeaders(['Link' => $uri_without_query_string]);
      
return $data;
  }

  /** * Provides test cases. * * @dataProvider providerTestIsValidCustomQueryParameter * @covers ::isValidCustomQueryParameter * @covers ::isValidMemberName */
  public function testIsValidCustomQueryParameter($custom_query_parameter$expected) {
    $this->assertSame($expected, JsonApiSpec::isValidCustomQueryParameter($custom_query_parameter));
  }

  /** * Data provider for testIsValidCustomQueryParameter. */
  public static function providerTestIsValidCustomQueryParameter() {
    $data = static::providerTestIsValidMemberName();

    // All valid member names are also valid custom query parameters, except for     // single-character ones.     $data['single-character'][1] = FALSE;

    
Home | Imprint | This part of the site doesn't use cookies.