LocaleFileList.php 1.62 KB
Newer Older
Pham Huy committed
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
<?php
/**
 * File list indexed by locale codes
 */
class Loco_fs_LocaleFileList extends Loco_fs_FileList {
    
    /**
     * Look up locale entries by their tag
     * @var array
     */
    private $index = array();
    
    
    /**
     * @return Loco_fs_LocaleFileList
     */
    public function addLocalized( Loco_fs_LocaleFile $file ){
        $i = count($this);
        $this->add( $file );
        if( count($this) !== $i ){
            if( $key = $file->getSuffix() ){
                $this->index[$key][] = $i;
            }
        }
        
        return $this;
    }
    


    /**
     * Get a new list containing just files for a given locale (exactly)
     * @return Loco_fs_LocaleFileList
     */
    public function filter( $tag ){
        $list = new Loco_fs_LocaleFileList;
        if( isset($this->index[$tag]) ){
            foreach( $this->index[$tag] as $i ){
                $list->addLocalized( $this[$i] );
            }
        }
        return $list;
    }    



    /**
     * Get a unique list of valid locales for which there are files
     * @return array<Loco_Locale>
     */
    public function getLocales(){
        $list = array();
        foreach( array_keys($this->index) as $tag ){
            $locale = Loco_Locale::parse($tag);
            if( $locale->isValid() ){
                $list[$tag] = $locale;
            }
        }
        return $list;
    }



    /**
     * {@inheritdoc}
     * @return Loco_fs_LocaleFileList
     */
    public function augment( Loco_fs_FileList $list ){
        foreach( $list as $file ){
            $this->addLocalized( $file );
        }
        return $this;
    }
    
}