You are here

private function CssW3Form::testW3C in Advanced CSS/JS Aggregation 8.4

Same name and namespace in other branches
  1. 8.2 advagg_validator/src/Form/CssW3Form.php \Drupal\advagg_validator\Form\CssW3Form::testW3C()
  2. 8.3 advagg_validator/src/Form/CssW3Form.php \Drupal\advagg_validator\Form\CssW3Form::testW3C()

Given a CSS file, test to make sure it is valid CSS.

Parameters

string $filename: The name of the file.

array $validator_options: List of options to pass along to the CSS Validator.

Return value

array Info from the w3c server.

1 call to CssW3Form::testW3C()
CssW3Form::testFiles in advagg_validator/src/Form/CssW3Form.php
Perform server side test(s) on all given files.

File

advagg_validator/src/Form/CssW3Form.php, line 183

Class

CssW3Form
Configure form for W3C validation of CSS files.

Namespace

Drupal\advagg_validator\Form

Code

private function testW3C($filename, array &$validator_options = []) {

  // Get CSS files contents.
  $validator_options['text'] = file_get_contents($filename);
  if (strlen($validator_options['text']) > 50000) {
    unset($validator_options['text']);
    $validator_options['uri'] = $this->requestStack
      ->getCurrentRequest()
      ->getBaseUrl() . $filename;
  }

  // Add in defaults.
  $validator_options += [
    'output' => 'soap12',
    'warning' => '1',
    'profile' => 'css3',
    'usermedium' => 'all',
    'lang' => 'en',
  ];

  // Build request URL.
  // API Documentation http://jigsaw.w3.org/css-validator/api.html
  $request_url = 'http://jigsaw.w3.org/css-validator/validator';
  $query = http_build_query($validator_options, '', '&');
  $url = $request_url . '?' . $query;
  try {
    $data = $this->httpClient
      ->get($url)
      ->getBody();
  } catch (RequestException $e) {
    watchdog_exception('AdvAgg Validator', $e);
  } catch (\Exception $e) {
    watchdog_exception('AdvAgg Validator', $e);
  }
  if (!empty($data)) {

    // Parse XML and return info.
    $return = $this
      ->parseSoapResponse($data);
    $return['filename'] = $filename;
    if (isset($validator_options['text'])) {
      unset($validator_options['text']);
    }
    elseif (isset($validator_options['uri'])) {
      unset($validator_options['uri']);
    }
    $return['options'] = $validator_options;
    return $return;
  }
  return [
    'error' => $this
      ->t('W3C Server did not return a 200 or request data was empty.'),
  ];
}