Beispiel #1
0
 public static function boot()
 {
     parent::boot();
     static::creating(function ($object) {
         $object->setJsonAttribute('metadata', 'author', UserProvider::getCurrentUserId());
     });
     static::created(function ($object) {
         $revision = new ObjectRevision();
         $revision->object_id = $object->id;
         $revision->revision_id = 0;
         // Data patch
         $revision->data_patch = json_encode(JsonPatch::diff([], json_decode($object->data, 1)));
         // Metadata patch
         $revision->metadata_patch = json_encode(JsonPatch::diff([], json_decode($object->metadata, 1)));
         $revision->author = UserProvider::getCurrentUserId();
         $revision->created_at = Carbon::now();
         $revision->save();
     });
     static::updating(function ($updatingObject) {
         // Get the object again from DB so we are sure we have up-to-date content to make the diff from
         $object = Object::find($updatingObject->id);
         var_dump("here updating");
         $lastRevision = ObjectRevision::where('object_id', $object->id)->orderBy('revision_id', 'DESC')->firstOrFail();
         $revision = new ObjectRevision();
         $revision->object_id = $object->id;
         $revision->revision_id = $lastRevision->revision_id + 1;
         // Data patch
         $revision->data_patch = json_encode(JsonPatch::diff(json_decode($object->data, 1), json_decode($updatingObject->data, 1)));
         // Metadata patch
         $revision->metadata_patch = json_encode(JsonPatch::diff(json_decode($object->metadata, 1), json_decode($updatingObject->metadata, 1)));
         $revision->author = UserProvider::getCurrentUserId();
         $revision->created_at = Carbon::now();
         $revision->save();
     });
 }
Beispiel #2
0
 public static function diff($src, $dst)
 {
     self::$src = $src;
     self::$dst = $dst;
     $result = [];
     $rows = JsonPatch::diff($src, $dst);
     foreach ($rows as $key => $row) {
         $root = self::getRootName($row['path']);
         if (!isset($result[$root])) {
             $result[$root] = [];
         }
         switch ($root) {
             case 'objects':
                 $result[$root] = AppSettings::mergeArray($result[$root], self::getObjectDiff($row, $key));
                 unset($rows[$key]);
                 break;
             case 'methods':
                 $result[$root] = AppSettings::mergeArray($result[$root], self::getMethodDiff($row));
                 break;
         }
     }
     return $result;
 }
 /**
  * Generates a JSON Patch representation and return its
  *
  * @param mixed $updatedProperties Properties of the resource to update
  * @return String JSON Patch representation for updates
  */
 protected function generateJsonPatch($updatedProperties)
 {
     // Normalize current and updated properties into nested arrays
     $currentProperties = json_decode(json_encode($this->getUpdateablePropertiesAsArray()), true);
     $updatedProperties = json_decode(json_encode($updatedProperties), true);
     // Add any properties that haven't changed to generate the correct patch
     // (otherwise unchanging properties are marked as removed in the patch)
     foreach ($currentProperties as $key => $value) {
         if (!array_key_exists($key, $updatedProperties)) {
             $updatedProperties[$key] = $value;
         }
     }
     // Recursively alias current and updated properties
     $currentProperties = $this->recursivelyAliasPropertyValue($currentProperties);
     $updatedProperties = $this->recursivelyAliasPropertyValue($updatedProperties);
     // Generate JSON Patch representation
     $json = json_encode(JsonPatch::diff($currentProperties, $updatedProperties));
     $this->checkJsonError();
     return $json;
 }
Beispiel #4
0
 public function compare($before, $after)
 {
     //TODO: verify that we're passing arrays
     $diff = JsonPatch::diff($before, $after);
     return $diff;
 }
Beispiel #5
0
function diff_test($test)
{
    // Skip comment-only or test op tests
    if (!(isset($test['doc']) && isset($test['expected']))) {
        return true;
    }
    $result = true;
    try {
        $doc1 = $test['doc'];
        // copy, in case sort/patch alters
        $doc2 = $test['expected'];
        $patch = JsonPatch::diff($doc1, $doc2);
        $patched = JsonPatch::patch($doc1, $patch);
        if (!JsonPatch::considered_equal($patched, $doc2)) {
            print "diff test failed:\n";
            print_test($test);
            print "from:     " . json_encode($doc1) . "\n";
            print "diff:     " . json_encode($patch) . "\n";
            print "found:    " . json_encode($patched) . "\n";
            print "expected: " . json_encode($doc2) . "\n\n";
            $result = false;
        }
        // reverse order
        $doc1 = $test['expected'];
        // copy, in case sort/patch alters
        $doc2 = $test['doc'];
        $patch = JsonPatch::diff($doc1, $doc2);
        $patched = JsonPatch::patch($doc1, $patch);
        if (!JsonPatch::considered_equal($patched, $doc2)) {
            print "reverse diff test failed:\n";
            print_test($test);
            print "from:     " . json_encode($doc1) . "\n";
            print "diff:     " . json_encode($patch) . "\n";
            print "found:    " . json_encode($patched) . "\n";
            print "expected: " . json_encode($doc2) . "\n\n";
            $result = false;
        }
    } catch (Exception $ex) {
        print "caught exception " . $ex->getMessage() . "\n";
        return false;
    }
    return $result;
}
Beispiel #6
0
 public static function boot()
 {
     parent::boot();
     static::creating(function ($page) {
         $slugifier = new Slugify();
         $page->setJsonAttribute('metadata', 'author', UserProvider::getCurrentUserId());
         $page->setJsonAttribute('metadata', 'slug', $slugifier->slugify($page->title));
     });
     static::created(function ($page) {
         $revision = new PageRevision();
         $revision->page_id = $page->id;
         $revision->revision_id = 0;
         $dmp = new DiffMatchPatch();
         // Content patch
         $contentPatches = $dmp->patch_make("", $page->content);
         $revision->content_patch = $dmp->patch_toText($contentPatches);
         // Metadata patch
         $revision->metadata_patch = json_encode(JsonPatch::diff([], json_decode($page->metadata, 1)));
         $revision->author = UserProvider::getCurrentUserId();
         $revision->created_at = Carbon::now();
         $revision->save();
     });
     static::updating(function ($updatingPage) {
         // Get the page again from DB so we are sure we have up-to-date content to make the diff from
         $page = Page::find($updatingPage->id);
         $lastRevision = PageRevision::where('page_id', $page->id)->orderBy('revision_id', 'DESC')->firstOrFail();
         $revision = new PageRevision();
         $revision->page_id = $page->id;
         $revision->revision_id = $lastRevision->revision_id + 1;
         $dmp = new DiffMatchPatch();
         // Content patch
         $contentPatches = $dmp->patch_make($page->getOriginal('content'), $updatingPage->content);
         $revision->content_patch = $dmp->patch_toText($contentPatches);
         // Metadata patch
         $revision->metadata_patch = json_encode(JsonPatch::diff(json_decode($page->metadata, 1), json_decode($updatingPage->metadata, 1)));
         $revision->author = UserProvider::getCurrentUserId();
         $revision->created_at = Carbon::now();
         $revision->save();
     });
 }