JsonFFS.php
Go to the documentation of this file.00001 <?php
00019 class JsonFFS extends SimpleFFS {
00024 public static function isValid( $data ) {
00025 return is_array( FormatJSON::decode( $data, true ) );
00026 }
00027
00028 public function getFileExtensions() {
00029 return array( '.json' );
00030 }
00031
00036 public function readFromVariable( $data ) {
00037 $messages = (array) FormatJSON::decode( $data, true );
00038 $authors = array();
00039 $metadata = array();
00040
00041 if ( isset( $messages['@metadata']['authors'] ) ) {
00042 $authors = (array)$messages['@metadata']['authors'];
00043 unset( $messages['@metadata']['authors'] );
00044 }
00045
00046 if ( isset( $messages['@metadata'] ) ) {
00047 $metadata = $messages['@metadata'];
00048 }
00049
00050 unset( $messages['@metadata'] );
00051
00052 $messages = $this->group->getMangler()->mangle( $messages );
00053
00054 return array(
00055 'MESSAGES' => $messages,
00056 'AUTHORS' => $authors,
00057 'METADATA' => $metadata,
00058 );
00059 }
00060
00065 protected function writeReal( MessageCollection $collection ) {
00066 $messages = array();
00067 $template = $this->read( $collection->getLanguage() );
00068
00069 if ( isset( $template['METADATA'] ) ) {
00070 $messages['@metadata'] = $template['METADATA'];
00071 }
00072
00073 $authors = $collection->getAuthors();
00074 $authors = $this->filterAuthors( $authors, $collection->code );
00075
00076 if ( $authors !== array() ) {
00077 $messages['@metadata']['authors'] = $authors;
00078 }
00079
00080 $mangler = $this->group->getMangler();
00081
00085 foreach ( $collection as $key => $m ) {
00086 $value = $m->translation();
00087 if ( $value === null ) {
00088 continue;
00089 }
00090
00091 if ( $m->hasTag( 'fuzzy' ) ) {
00092 $value = str_replace( TRANSLATE_FUZZY, '', $value );
00093 }
00094
00095 $key = $mangler->unmangle( $key );
00096 $messages[$key] = $value;
00097 }
00098
00099
00100 if ( !count( $messages ) ) {
00101 return '';
00102 }
00103
00104 return FormatJSON::encode( $messages, true );
00105 }
00106 }