57290813b6698e12527d9330aac763f85b10f0bb
[yaffs-website] / web / core / modules / language / src / Config / LanguageConfigCollectionNameTrait.php
1 <?php
2
3 namespace Drupal\language\Config;
4
5 /**
6  * Provides a common trait for working with language override collection names.
7  */
8 trait LanguageConfigCollectionNameTrait {
9
10   /**
11    * Creates a configuration collection name based on a language code.
12    *
13    * @param string $langcode
14    *   The language code.
15    *
16    * @return string
17    *   The configuration collection name for a language code.
18    */
19   protected function createConfigCollectionName($langcode) {
20     return 'language.' . $langcode;
21   }
22
23   /**
24    * Converts a configuration collection name to a language code.
25    *
26    * @param string $collection
27    *   The configuration collection name.
28    *
29    * @return string
30    *   The language code of the collection.
31    *
32    * @throws \InvalidArgumentException
33    *   Exception thrown if the provided collection name is not in the format
34    *   "language.LANGCODE".
35    *
36    * @see self::createConfigCollectionName()
37    */
38   protected function getLangcodeFromCollectionName($collection) {
39     preg_match('/^language\.(.*)$/', $collection, $matches);
40     if (!isset($matches[1])) {
41       throw new \InvalidArgumentException("'$collection' is not a valid language override collection");
42     }
43     return $matches[1];
44   }
45
46 }