Overview

Namespaces

  • Deimos
    • I18n
  • PHP

Classes

  • ArrayObject
  • Deimos\I18n\I18n

Interfaces

  • ArrayAccess
  • Countable
  • IteratorAggregate
  • Serializable
  • Traversable
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: namespace Deimos\I18n;
  4: 
  5: abstract class I18n extends \ArrayObject
  6: {
  7: 
  8:     /**
  9:      * @var string
 10:      */
 11:     protected $prefix;
 12: 
 13:     /**
 14:      * @var bool
 15:      */
 16:     protected $loaded;
 17: 
 18:     /**
 19:      * @var bool
 20:      */
 21:     protected $isSave;
 22: 
 23:     /**
 24:      * i18n constructor.
 25:      *
 26:      * @param string $prefix
 27:      */
 28:     public function __construct($prefix = null)
 29:     {
 30:         $this->prefix = $prefix;
 31:         parent::__construct();
 32:     }
 33: 
 34:     /**
 35:      * @param string $key
 36:      * @param bool   $addPrefix
 37:      *
 38:      * @return string
 39:      */
 40:     protected function prefix($key, $addPrefix = true)
 41:     {
 42:         if (!$addPrefix)
 43:         {
 44:             return $key;
 45:         }
 46: 
 47:         return ($this->prefix ? $this->prefix . '.' : '') . $key;
 48:     }
 49: 
 50:     /**
 51:      * @param $key
 52:      *
 53:      * @return bool
 54:      */
 55:     protected function valid($key)
 56:     {
 57:         return isset($this[$key]);
 58:     }
 59: 
 60:     /**
 61:      * load data
 62:      */
 63:     private function load()
 64:     {
 65:         if (!$this->loaded)
 66:         {
 67:             foreach ($this->init() as $key => $value)
 68:             {
 69:                 $this[$this->prefix($key)] = $value;
 70:             }
 71:             $this->loaded = true;
 72:         }
 73:     }
 74: 
 75:     /**
 76:      * @param array $storage
 77:      * @param bool  $addPrefix
 78:      */
 79:     public final function stream(array $storage, $addPrefix = true)
 80:     {
 81:         foreach ($storage as $key => $item)
 82:         {
 83:             $this[$this->prefix($key, $addPrefix)] = $item;
 84:         }
 85:     }
 86: 
 87:     /**
 88:      * load data
 89:      *
 90:      * @return array
 91:      */
 92:     abstract protected function init();
 93: 
 94:     /**
 95:      * @param string $key
 96:      * @param string $value
 97:      */
 98:     abstract protected function register($key, $value);
 99: 
100:     /**
101:      * @param      $key
102:      * @param null $default
103:      * @param bool $addPrefix
104:      *
105:      * @return string
106:      */
107:     public function t($key, $default = null, $addPrefix = true)
108:     {
109:         $this->load();
110:         $itemKey = $this->prefix($key, $addPrefix);
111:         if (!$this->valid($itemKey))
112:         {
113:             $this[$itemKey] = $default;
114:             if ($this->isSave)
115:             {
116:                 $this->register($key, $default);
117:             }
118:         }
119: 
120:         return $this[$itemKey];
121:     }
122: 
123: }
API documentation generated by ApiGen