lithium\g11n\Locale::compose PHP Method

compose() public static method

Composes a locale from locale tags. This is the pendant to Locale::decompose().
public static compose ( array $tags ) : string
$tags array An array as obtained from `Locale::decompose()`.
return string A locale with tags separated by underscores or `null` if none of the passed tags could be used to compose a locale.
    public static function compose($tags)
    {
        $result = array();
        foreach (static::$_tags as $name => $tag) {
            if (isset($tags[$name])) {
                $result[] = $tags[$name];
            }
        }
        if ($result) {
            return implode('_', $result);
        }
    }

Usage Example

Example #1
0
 /**
  * Tests if the ouput of `compose()` can be used as the input for `decompose()`
  * and vice versa.
  *
  * @return void
  */
 public function testComposeDecomposeCompose()
 {
     $data = array('language' => 'en');
     $expected = 'en';
     $result = Locale::compose(Locale::decompose(Locale::compose($data)));
     $this->assertEqual($expected, $result);
     $data = array('language' => 'en', 'territory' => 'US');
     $expected = 'en_US';
     $result = Locale::compose(Locale::decompose(Locale::compose($data)));
     $this->assertEqual($expected, $result);
     $data = array('language' => 'zh', 'script' => 'Hans', 'territory' => 'HK', 'variant' => 'REVISED');
     $expected = 'zh_Hans_HK_REVISED';
     $result = Locale::compose(Locale::decompose(Locale::compose($data)));
     $this->assertEqual($expected, $result);
 }