* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBDictionary' , 'WBString'); /** * Dictionary: State * * List states of countries * * @version 0.1.0 * @package WB * @subpackage */ class WBDictionary_State extends WBDictionary { /** * Table access * @var WBDatasource_Table */ protected $table; /** * Name of Table, That Stores States */ const TABLE_STATE = 'state'; /** * 2nd constructor * * load county codes and translate names */ public function init() { $this->table = WBClass::create('WBDatasource_Table'); } /** * find entry in dictionary * * Actual look up data in dictionary * * @param array $data * @return array|null */ protected function find($data) { $clause = array(); foreach ($data as $field => $value) { $clause[] = array( 'field' => $field, 'value' => $value ); } $row = $this->table->get(self::TABLE_STATE, null, null, $clause); if (0 == count($row)) { return null; } return $row[0]; } /** * load record by id * * map code to language names etc * * @param string $id */ public function load($id) { if ($this->id == $id) { return; } $data = $this->table->get(self::TABLE_STATE, $id); if (1 != count($data)) { WBClass::load('WBException_Argument'); throw new WBException_Argument('Failed to load dictionary record by id!', 1, __CLASS__); } $data = $data[0]; $this->data = $data; $this->id = $data['id']; $this->word = $data['title']; } /** * Extract dictionary data from word * * Just to satify the interface * * @see include/WB/WBDictionary#explode() * @todo handle country code some how * @param string $word * @return array */ protected function explode($word) { return array('title' => $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 states * * @return array $list */ public function getList() { $data = $this->getExtendedList(); $list = array(); foreach ($data as $d) { $list[$d['id']] = $d['word']; } 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_STATE); foreach ($list as &$l) { $l['word'] = $l['title']; } return $list; } }