Ejemplo n.º 1
0
 /**
  * Ensure that the graph we want to persist has data with valid cardinality.
  *
  * @param \Tripod\ExtendedGraph $graph
  * @throws \Tripod\Exceptions\CardinalityException
  */
 protected function validateGraphCardinality(\Tripod\ExtendedGraph $graph)
 {
     $config = Config::getInstance();
     $cardinality = $config->getCardinality($this->getStoreName(), $this->getPodName());
     $namespaces = $config->getNamespaces();
     $graphSubjects = $graph->get_subjects();
     if (empty($cardinality) || $graph->is_empty()) {
         return;
     }
     foreach ($cardinality as $qname => $cardinalityValue) {
         list($namespace, $predicateName) = explode(':', $qname);
         if (!array_key_exists($namespace, $namespaces)) {
             //TODO This may be changed to a namespace exception at some point...
             throw new \Tripod\Exceptions\CardinalityException("Namespace '{$namespace}' not defined for qname: {$qname}");
         }
         // NB: The only constraint we currently support is a value of 1 to enforce one triple per subject/predicate.
         if ($cardinalityValue == 1) {
             foreach ($graphSubjects as $subjectUri) {
                 $predicateUri = $namespaces[$namespace] . $predicateName;
                 $predicateValues = $graph->get_subject_property_values($subjectUri, $predicateUri);
                 if (count($predicateValues) > 1) {
                     $v = array();
                     foreach ($predicateValues as $predicateValue) {
                         $v[] = $predicateValue['value'];
                     }
                     throw new \Tripod\Exceptions\CardinalityException("Cardinality failed on {$subjectUri} for '{$qname}' - should only have 1 value and has: " . implode(', ', $v));
                 }
             }
         }
     }
 }