You are here

HiddenLanguageManager.php in Hidden Language 2.x

File

src/HiddenLanguageManager.php
View source
<?php

namespace Drupal\hidden_language;

use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Path\PathValidator;

/**
 * Class HiddenLanguageManager.
 *
 * @package Drupal\hidden_language
 */
class HiddenLanguageManager {

  /**
   * Drupal\Core\Config\ConfigFactory.
   *
   * @var Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * Drupal\Core\Path\PathValidator.
   *
   * @var Drupal\Core\Path\PathValidator
   */
  protected $pathValidator;

  /**
   * The hidden_language.settings.
   */
  protected $hiddenLanguageSettings;

  /**
   * Constructor.
   */
  public function __construct(ConfigFactory $config_factory, PathValidator $path_validator) {
    $this->configFactory = $config_factory;
    $this->pathValidator = $path_validator;
    $this->hiddenLanguageSettings = $this->configFactory
      ->getEditable('hidden_language.settings');
  }

  /**
   * Get array of route name exception configuration.
   *
   * @return array
   *   Array of configurated routes.
   */
  public function getRoutesException() {
    return json_decode($this->hiddenLanguageSettings
      ->get('route_name'));
  }

  /**
   * Get array of path url exception configuration.
   *
   * @return array
   *   Array of configurated paths.
   */
  public function getPathsException() {
    return json_decode($this->hiddenLanguageSettings
      ->get('path'));
  }

  /**
   * Check if url route name existis in the exception list.
   *
   * @param string $url
   *   Url to be tested.
   *
   * @return bool
   *   True if url route name existis in exception list and False if not.
   */
  public function isRouteExcecption($url) {
    if (!is_array($this
      ->getRoutesException())) {
      return FALSE;
    }
    $url_object = $this->pathValidator
      ->getUrlIfValidWithoutAccessCheck($url);
    if (!$url_object) {
      return FALSE;
    }
    $route_name = $url_object
      ->getRouteName();
    return array_key_exists($route_name, array_flip($this
      ->getRoutesException()));
  }

  /**
   * Check if url exists in the exception list.
   *
   * @param string $url
   *   Url to be tested.
   *
   * @return bool
   *   True if Url is in exception list and False if not.
   */
  public function isPathException($url) {
    if (!is_array($this
      ->getPathsException())) {
      return FALSE;
    }
    return array_key_exists($url, array_flip($this
      ->getPathsException()));
  }

  /**
   * Verify if $url is an exception on the hidden language config.
   *
   * @param string $url
   *   Url to be tested.
   *
   * @return bool
   *   True if Url is in exception list and False if not.
   */
  public function isUrlException($url) {
    if ($this
      ->isRouteExcecption($url) || $this
      ->isPathException($url)) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Returns json encode array.
   *
   * @param string $config
   *   The name of the form element.
   *
   * @return array
   *   The states array.
   */
  public function encodeArray(string $config = NULL) {
    $array = explode(",", str_replace(" ", "", $config));
    $array = json_encode($array);
    return $array;
  }

}

Classes

Namesort descending Description
HiddenLanguageManager Class HiddenLanguageManager.