summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/language/src/Config/LanguageConfigCollectionNameTrait.php
blob: 57290813b6698e12527d9330aac763f85b10f0bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

namespace Drupal\language\Config;

/**
 * Provides a common trait for working with language override collection names.
 */
trait LanguageConfigCollectionNameTrait {

  /**
   * Creates a configuration collection name based on a language code.
   *
   * @param string $langcode
   *   The language code.
   *
   * @return string
   *   The configuration collection name for a language code.
   */
  protected function createConfigCollectionName($langcode) {
    return 'language.' . $langcode;
  }

  /**
   * Converts a configuration collection name to a language code.
   *
   * @param string $collection
   *   The configuration collection name.
   *
   * @return string
   *   The language code of the collection.
   *
   * @throws \InvalidArgumentException
   *   Exception thrown if the provided collection name is not in the format
   *   "language.LANGCODE".
   *
   * @see self::createConfigCollectionName()
   */
  protected function getLangcodeFromCollectionName($collection) {
    preg_match('/^language\.(.*)$/', $collection, $matches);
    if (!isset($matches[1])) {
      throw new \InvalidArgumentException("'$collection' is not a valid language override collection");
    }
    return $matches[1];
  }

}