From 809a4d31ce78d92cfcdd78344fdc7289a83262dc Mon Sep 17 00:00:00 2001 From: Dave Longley Date: Mon, 16 Jan 2012 15:53:49 -0500 Subject: [PATCH] Remove duplicate objects when normalizing. --- jsonld.php | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/jsonld.php b/jsonld.php index 99d23b8..d726bd4 100644 --- a/jsonld.php +++ b/jsonld.php @@ -997,19 +997,18 @@ function _collectSubjects($input, $subjects, $bnodes) } /** - * Filters duplicate IRIs. + * Filters duplicate objects. */ -class DuplicateIriFilter +class DuplicateFilter { - public function __construct($iri) + public function __construct($obj) { - $this->iri = $iri; + $this->obj = $obj; } public function filter($e) { - return (is_object($e) and property_exists($e, '@id') and - $e->{'@id'} === $this->iri); + return (_compareObjects($e, $this->obj) === 0); } } @@ -1126,13 +1125,9 @@ function _flatten($parent, $parentProperty, $value, $subjects) if($parent instanceof ArrayObject) { // do not add duplicate IRIs for the same property - $duplicate = false; - if(is_object($flattened) and property_exists($flattened, '@id')) - { - $duplicate = count(array_filter( - (array)$parent, array( - new DuplicateIriFilter($flattened->{'@id'}), 'filter'))) > 0; - } + $duplicate = count(array_filter( + (array)$parent, array( + new DuplicateFilter($flattened), 'filter'))) > 0; if(!$duplicate) { $parent[] = $flattened;