public static function encode($model) { $e = new LexDbeDtoEntriesEncoder(); return $e->_encode($model); }
public function testDecode_ExistingModelAndMapOf_DecodeOk() { $multiParagraph = new LexMultiParagraph(); $multiParagraph->inputSystem = 'en'; $paragraph1 = new LexParagraph(); $paragraph1->styleName = 'styleA'; $paragraph1->content = 'This is the first paragraph in <span lang="de">die Deutsche Sprache</span>'; $multiParagraph->paragraphs->append($paragraph1); $paragraph2 = new LexParagraph(); $paragraph2->content = 'This is the second paragraph in <span lang="es">la lingua Espanol</span>'; $multiParagraph->paragraphs->append($paragraph2); $paragraph3 = new LexParagraph(); $paragraph3->styleName = 'styleC'; $multiParagraph->paragraphs->append($paragraph3); $params = json_decode(json_encode(LexDbeDtoEntriesEncoder::encode($multiParagraph)), true); $newMultiParagraph = new LexMultiParagraph($multiParagraph->guid); $key = 'customField_entry_Cust_MultiPara'; $newTestMapOf = new MultiParagraphInMap(); $newTestMapOf->data[$key] = clone $newMultiParagraph; LexEntryDecoder::decode($newMultiParagraph, $params); $this->assertEquals($multiParagraph, $newMultiParagraph); $testMapOf = new MultiParagraphInMap(); $testMapOf->data[$key] = $multiParagraph; $params = json_decode(json_encode(LexDbeDtoEntriesEncoder::encode($testMapOf)), true); LexEntryDecoder::decode($newTestMapOf, $params); $this->assertEquals($testMapOf->data[$key]->guid, $newTestMapOf->data[$key]->guid); $this->assertEquals($testMapOf, $newTestMapOf); }
/** * @param string $projectId * @param string $userId * @param null $lastFetchTime * @param int $offset * @throws \Exception * @return array */ public static function encode($projectId, $userId, $lastFetchTime = null, $offset = 0) { $data = array(); $project = new LexProjectModel($projectId); if ($lastFetchTime) { $entriesModel = new LexEntryListModel($project, $lastFetchTime); $entriesModel->readAsModels(); $commentsModel = new LexCommentListModel($project, $lastFetchTime); $commentsModel->readAsModels(); } else { $entriesModel = new LexEntryListModel($project, null, self::MAX_ENTRIES_PER_REQUEST, $offset); $entriesModel->readAsModels(); $commentsModel = new LexCommentListModel($project, null, self::MAX_ENTRIES_PER_REQUEST, $offset); $commentsModel->readAsModels(); $data['itemTotalCount'] = $entriesModel->totalCount > $commentsModel->totalCount ? $entriesModel->totalCount : $commentsModel->totalCount; $data['itemCount'] = $entriesModel->count > $commentsModel->count ? $entriesModel->count : $commentsModel->count; $data['offset'] = $offset; } $entries = LexDbeDtoEntriesEncoder::encode($entriesModel); $entries = $entries['entries']; $encodedComments = LexDbeDtoCommentsEncoder::encode($commentsModel); $data['comments'] = $encodedComments['entries']; $votes = new UserGenericVoteModel($userId, $projectId, 'lexCommentPlusOne'); $votesDto = array(); foreach ($votes->votes as $vote) { $votesDto[$vote->ref->id] = true; } $data['commentsUserPlusOne'] = $votesDto; if (!is_null($lastFetchTime)) { $deletedEntriesModel = new LexDeletedEntryListModel($project, $lastFetchTime); $deletedEntriesModel->read(); $data['deletedEntryIds'] = array_map(function ($e) { return $e['id']; }, $deletedEntriesModel->entries); $deletedCommentsModel = new LexDeletedCommentListModel($project, $lastFetchTime); $deletedCommentsModel->read(); $data['deletedCommentIds'] = array_map(function ($c) { return $c['id']; }, $deletedCommentsModel->entries); } $lexemeInputSystems = $project->config->entry->fields[LexConfig::LEXEME]->inputSystems; usort($entries, function ($a, $b) use($lexemeInputSystems) { $lexeme1Value = ''; if (array_key_exists(LexConfig::LEXEME, $a)) { $lexeme1 = $a[LexConfig::LEXEME]; foreach ($lexemeInputSystems as $ws) { if (array_key_exists($ws, $lexeme1) && $lexeme1[$ws]['value'] != '') { $lexeme1Value = $lexeme1[$ws]['value']; // '\P{xx} matches all characters without the Unicode property XX. L is the Unicode property "letter". $lexeme1Value = preg_replace('/^\\P{L}+/', '', $lexeme1Value); // Strip non-letter characters from front of word for sorting break; } } } $lexeme2Value = ''; if (array_key_exists(LexConfig::LEXEME, $b)) { $lexeme2 = $b[LexConfig::LEXEME]; foreach ($lexemeInputSystems as $ws) { if (array_key_exists($ws, $lexeme2) && $lexeme2[$ws]['value'] != '') { $lexeme2Value = $lexeme2[$ws]['value']; $lexeme2Value = preg_replace('/^\\P{L}+/', '', $lexeme2Value); // Strip non-letter characters from front of word for sorting break; } } } return strtolower($lexeme1Value) > strtolower($lexeme2Value) ? 1 : -1; }); $data['entries'] = $entries; $data['timeOnServer'] = time(); // for offline syncing if ($project->hasSendReceive()) { $data['sendReceive'] = array(); $data['sendReceive']['status'] = SendReceiveCommands::getProjectStatus($projectId); } return $data; }