Passed
Push — master ( bb74df...01777c )
by Jakub
02:24
created

src/Resolvers/HeaderLocaleResolver.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Translation\Resolvers;
5
6
use Nexendrie\Translation\Loaders\ILoader,
7
    Nette\Http\IRequest,
8
    Nexendrie\Translation\LoaderNotSetException,
9
    Nette\Http\RequestFactory;
10
11
/**
12
 * HeaderLocaleResolver
13
 *
14
 * @author Jakub Konečný
15
 */
16 1
class HeaderLocaleResolver implements ILoaderAwareLocaleResolver {
17 1
  use \Nette\SmartObject;
18
  
19
  /** @var ILoader|NULL */
20
  protected $loader = NULL;
21
  /** @var IRequest */
22
  protected $request;
23
  
24
  function __construct(IRequest $request = NULL) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25 1
    if(is_null($request)) {
26 1
      $request = (new RequestFactory)->createHttpRequest();
27
    }
28 1
    $this->request = $request;
29 1
  }
30
  
31
  function setLoader(ILoader $loader): void {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
32 1
    $this->loader = $loader;
33 1
  }
34
  
35
  /**
36
   * Resolve language
37
   *
38
   * Taken from Nette\Http\Request::detectLanguage()
39
   * @author David Grudl
40
   */
41
  function resolve(): ?string {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Expected 1 space after closing parenthesis; found 0
Loading history...
Inline shorthand IF statement must be declared on a single line
Loading history...
42 1
    if(is_null($this->loader)) {
43 1
      throw new LoaderNotSetException("Loader is not available, cannot detect possible languages.");
44
    }
45 1
    $header = $this->request->getHeader("Accept-Language");
46 1
    $langs = $this->loader->getAvailableLanguages();
47 1
    if(is_null($header)) {
48 1
      return NULL;
49
    }
50 1
    $s = strtolower($header);  // case insensitive
51 1
    $s = strtr($s, '_', '-');  // cs_CZ means cs-CZ
52 1
    rsort($langs);             // first more specific
53 1
    $pattern = ')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#';
54 1
    preg_match_all('#(' . implode('|', $langs) . $pattern, $s, $matches);
55 1
    if(!$matches[0]) {
56 1
      return NULL;
57
    }
58 1
    $max = 0;
59 1
    $lang = NULL;
60 1
    foreach($matches[1] as $key => $value) {
61 1
      $q = ($matches[2][$key] === '') ? 1.0 : (float) $matches[2][$key];
62 1
      if($q > $max) {
63 1
        $max = $q;
64 1
        $lang = $value;
65
      }
66
    }
67 1
    return $lang;
68
  }
69
}
70
?>