/** Add a literal value as a property of a resource * * The resource can either be a resource or the URI of a resource. * The value can either be a single value or an array of values. * * Example: * $graph->add("http://www.example.com", 'dc:title', 'Title of Page'); * * @param mixed $resource The resource to add data to * @param mixed $property The property name * @param mixed $value The value or values for the property * @param string $lang The language of the literal */ public function addLiteral($resource, $property, $value, $lang = null) { $this->checkResourceParam($resource); $this->checkPropertyParam($property, $inverse); if (is_array($value)) { foreach ($value as $v) { $this->addLiteral($resource, $property, $v, $lang); } return; } else { if ($lang) { $value = array('type' => 'literal', 'value' => $value, 'lang' => $lang); } else { $value = array('type' => 'literal', 'value' => $value, 'datatype' => EasyRdf_Literal::getDatatypeForValue($value)); if (empty($value['datatype'])) { unset($value['datatype']); } } } return $this->add($resource, $property, $value); }
/** Check that a value parameter is valid, and convert it to an associative array if needed * @ignore */ protected function checkValueParam(&$value) { if (isset($value)) { if (is_object($value)) { if (!method_exists($value, 'toRdfPhp')) { // Convert to a literal object $value = EasyRdf_Literal::create($value); } $value = $value->toRdfPhp(); } elseif (is_array($value)) { if (!isset($value['type'])) { throw new InvalidArgumentException("\$value is missing a 'type' key"); } if (!isset($value['value'])) { throw new InvalidArgumentException("\$value is missing a 'value' key"); } // Fix ordering and remove unknown keys $value = array('type' => strval($value['type']), 'value' => strval($value['value']), 'lang' => isset($value['lang']) ? strval($value['lang']) : null, 'datatype' => isset($value['datatype']) ? strval($value['datatype']) : null); } else { $value = array('type' => 'literal', 'value' => strval($value), 'datatype' => EasyRdf_Literal::getDatatypeForValue($value)); } if (!in_array($value['type'], array('uri', 'bnode', 'literal'), true)) { throw new InvalidArgumentException("\$value does not have a valid type (" . $value['type'] . ")"); } if (empty($value['datatype'])) { unset($value['datatype']); } if (empty($value['lang'])) { unset($value['lang']); } if (isset($value['lang']) and isset($value['datatype'])) { throw new InvalidArgumentException("\$value cannot have both and language and a datatype"); } } }