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 | class Dictionary { // ... function asArray() { return $this->translations; } function getType() { return $this->type; } function export() { $this->dictio->export( $this ); } function import() { $this->dictio->import( $this ); } } class DictionaryIO { function path( Dictionary $dictionary, $ext ) { $path = Dictionary::getSaveDirectory(); $path .= DIRECTORY_SEPARATOR; $path .= $dictionary->getType().".$ext"; return $path; } function export( Dictionary $dictionary ) { $translations = $dictionary->asArray(); file_put_contents( $this->path( $dictionary, 'serial'), serialize( $translations ) ); } function import( Dictionary $dictionary ) { $path = $this->path( $dictionary, 'serial' ); if ( ! is_file( $path ) ) return false; $translations = unserialize( file_get_contents( $path ) ); foreach ( $translations as $term => $trans ) { $dictionary->set( $term, $trans ); } } } $dict = new Dictionary( "En", new DictionaryIO() ); $dict->set( "TREE", "tree" ); $dict->export(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function export( Dictionary $dictionary ) { if ( $this->type == DictionaryIO::SERIAL ) { // write serialized data } else if ( $this->type == DictionaryIO::XML ) { // write xml data } } function import( Dictionary $dictionary ) { if ( $this->type == DictionaryIO::SERIAL ) { // read serialized data } else if ( $this->type == DictionaryIO::XML ) { // read xml data } } |
1 2 | XmlDictionaryIO extends DictionaryIO { } |
1 2 3 4 5 6 7 | $dictio = new XmlDictionaryIO(); if ( $dictio instanceof XmlDictionaryIO ) { print "object is an instance of XmlDictionaryIO\n"; } if ( $dictio instanceof DictionaryIO ) { print "object is an instance of DictionaryIO\n"; } |
1 2 | object is an instance of XmlDictionaryIO object is an instance of DictionaryIO |
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 | class XmlDictionaryIO extends DictionaryIO { function export( Dictionary $dictionary ) { $translations = $dictionary->asArray(); $doc = new DOMDocument("1.0"); $dic_el = $doc->createElement( "dictionary" ); $doc->appendChild( $dic_el ); foreach ( $translations as $key => $val ) { $term_el = $doc->createElement( "term" ); $dic_el->appendChild( $term_el ); $key_el = $doc->createElement("key", $key ); $val_el = $doc->createElement( "value", $val ); $term_el->appendChild( $key_el ); $term_el->appendChild( $val_el ); } file_put_contents( $this->path( $dictionary, 'xml'), $doc->saveXML() ); } function import( Dictionary $dictionary ) { $path = $this->path( $dictionary, 'xml'); if ( ! is_file( $path ) ) return false; $doc = DOMDocument::loadXML( file_get_contents( $path ) ); $termlist = $doc ->getElementsByTagName( "term" ); foreach ( $termlist as $term ) { $key = $term->getElementsByTagName( "key" ) ->item( 0 )->nodeValue; $val = $term ->getElementsByTagName( "value" ) ->item( 0 )->nodeValue; $dictionary->set( $key, $val ); } } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |