diff --git a/jsonld.php b/jsonld.php index cbab03c..20f9b98 100644 --- a/jsonld.php +++ b/jsonld.php @@ -1071,7 +1071,7 @@ class JsonLdProcessor { } // 2. equal @values - if(self::_isValue($v1) || self::_isValue($v2)) { + if(self::_isValue($v1) && self::_isValue($v2)) { return ( self::_compareKeyValues($v1, $v2, '@value') && self::_compareKeyValues($v1, $v2, '@type') && @@ -1538,9 +1538,9 @@ class JsonLdProcessor { if(property_exists($active_ctx->mappings, $active_property)) { $mapping = $active_ctx->mappings->{$active_property}; if($mapping && $mapping->propertyGenerator) { - $this->_findAndRemovePropertyGeneratorDuplicates( + $this->_findPropertyGeneratorDuplicates( $active_ctx, $element, $expanded_property, $expanded_item, - $active_property); + $active_property, true); } } @@ -3647,14 +3647,8 @@ class JsonLdProcessor { // see if a property generator matches if(is_object($parent)) { foreach($term_info->propertyGenerators as $property_generator) { - $iris = $active_ctx->mappings->{$property_generator}->{'@id'}; - $match = true; - foreach($iris as $iri_) { - if(!property_exists($parent, $iri_)) { - $match = false; - break; - } - } + $match = $this->_findPropertyGeneratorDuplicates( + $active_ctx, $parent, $iri, $value, $property_generator, false); if($match) { $term = $property_generator; break; @@ -3952,8 +3946,8 @@ class JsonLdProcessor { } /** - * Finds and removes any duplicate values that were presumably generated by - * a property generator in the given element. + * Finds and, if specified, removes any duplicate values that were + * presumably generated by a property generator in the given element. * * @param stdClass $active_ctx the active context. * @param stdClass $element the element to remove duplicates from. @@ -3961,30 +3955,64 @@ class JsonLdProcessor { * generator. * @param mixed $value the value to compare against when duplicate checking. * @param string $active_property the property generator term. + * @param bool $remove true to remove the duplicates found, false not to. + * + * @return bool true if duplicates were found for every IRI. */ - protected function _findAndRemovePropertyGeneratorDuplicates( - $active_ctx, $element, $expanded_property, $value, $active_property) { + protected function _findPropertyGeneratorDuplicates( + $active_ctx, $element, $expanded_property, $value, $active_property, + $remove) { + $rval = true; + // get property generator IRIs $iris = $active_ctx->mappings->{$active_property}->{'@id'}; // for each IRI that isn't 'expandedProperty', remove a single duplicate // from element, if found foreach($iris as $iri) { + if($rval === false) { + break; + } + if($iri === $expanded_property) { continue; } + + $rval = false; + if(!property_exists($element, $iri)) { + break; + } + $length = count($element->{$iri}); + + // handle empty array case + if($length === 0 && is_array($value) && count($value) === 0) { + $rval = true; + if($remove) { + unset($element->{$iri}); + } + continue; + } + + // handle other cases for($pi = 0; $pi < $length; ++$pi) { if(self::compareValues($element->{$iri}[$pi], $value)) { - // duplicate found, remove it in place - array_splice($element->{$iri}, $pi, 1); - if(count($element->{$iri}) === 0) { - unset($element->{$iri}); + // duplicate found + $rval = true; + + if($remove) { + // remove it in place + array_splice($element->{$iri}, $pi, 1); + if(count($element->{$iri}) === 0) { + unset($element->{$iri}); + } } break; } } } + + return $rval; } /**