<?php

namespace SCF\Core;


class Languages
{

    /**
     * @var Database
     */
    private $_db;

    /**
     * @var
     */
    private $language;

    /**
     * @var array
     */
    private $phrases = [];

    /**
     * Languages constructor.
     * @param Database $db
     * @param $langCode
     */
    public function __construct(Database $db, $langCode)
    {
        $this->_db = $db;
        $this->language = $langCode;
        $this->setLanguage();
    }

    /**
     * Create Language Array for the current language
     */
    private function setLanguage()
    {
        $this->_db->query("SELECT * FROM system_localization ORDER BY locaId ASC");


        $result = $this->_db->fetchArray();

        foreach ($result as $lang) {
            $this->phrases[$lang['locaVarname']] = $lang['locaString_'.$this->language];
        }
    }

    /**
     * @return array
     */
    public function postArray()
    {
        return $this->phrases;
    }

    /**
     * @param $phraseName
     * @return mixed
     */
    public function getPhrase($phraseName)
    {
        if (isset($this->phrases[$phraseName])) {
            return $this->phrases[$phraseName];
        } else {
            return $phraseName;
        }
    }

}