* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBDictionary' , 'WBString'); /** * Dictionary: Country * * Manage two letter country codes * * @version 0.2.0 * @package WB * @subpackage */ class WBDictionary_Country extends WBDictionary { /** * Table access * @var WBDatasource_Table */ protected $table; /** * Name of Table, That Stores Country Information */ const TABLE_COUNTRY = 'country'; /** * 2nd constructor * * load county codes and translate names */ public function init() { $this->table = WBClass::create('WBDatasource_Table'); } /** * Find Entry in Dictionary * * In country code dictionary, this is just the reverse mapping from * localized country name to code - seldomly used * * @param array $data * @return array|null */ protected function find($data) { $clause = array(); if (!empty($data['name'])) { $clause[] = array( 'field' => 'country', 'value' => trim($data['name']) ); } if (empty($clause)) { return null; } $row = $this->table->get(self::TABLE_COUNTRY, null, null, $clause); if (0 == count($row)) { return null; } return $row[0]; } /** * load record by id * * Map code to name * * @param string $id */ public function load($id) { $id = strtolower($id); if ('c' == $id) { $id = 'us'; } if ($this->id == $id) { return; } $clause = array(); $clause[] = array( 'field' => 'code', 'value' => $id ); $row = $this->table->get(self::TABLE_COUNTRY, null, null, $clause); if (1 != count($row)) { WBClass::load('WBException_Argument'); throw new WBException_Argument('Invalid country code: "'.$id.'"!', 1, __CLASS__); } $row = $row[0]; $this->id = $id; $this->word = $row['country']; $this->data = array( 'populated' => true, 'id' => $id, 'countrycode' => $id, 'countryname' => $this->word, 'phone' => $row['phone'], 'emoji' => $row['emoji'] ); } /** * extract dictionary data from word * * Just to satify the interface * * @see include/WB/WBDictionary#explode() * @param string $word * @return array */ protected function explode($word) { return array('name' => $word); } /** * add record to dictionary * * This is just a fake. Insert is not allowed * * @see include/WB/WBDictionary#insert() * @param array record to save * @return string record id */ protected function insert($data) { return null; } /** * save additional data to dictionary record * * This is just a fake - merge is not required * * @see populate() * @see $mergeFields * @param array $data * @return array */ protected function merge($save) { return $save; } /** * automatical populate function * * Surf to URL and extract title, description as well as * * @param string $id * @see include/WB/WBDictionary#autoPolulate() */ public function autoPopulate($id = null) { } /** * Get List of Dictionary Records * * Get all countries * * @return array $list */ public function getList() { $list = array(); $data = $this->table->get(self::TABLE_COUNTRY); foreach ($data as $d) { $list[$d['code']] = $d['country']; } return $list; } /** * Get List of Dictionary Records Like Database Records * * @throws WBException_Call if there is no list * @return array $list */ public function getExtendedList() { $list = $this->table->get(self::TABLE_COUNTRY); foreach ($list as &$l) { $l['word'] = $l['country']; $l['id'] = $l['code']; } return $list; } }