/** Constructor * */ public function __construct($value, $lang = null, $datatype = null) { if (EasyRdf_Utils::is_associative_array($value)) { $this->_value = isset($value['value']) ? $value['value'] : null; $this->_lang = isset($value['lang']) ? $value['lang'] : null; $this->_datatype = isset($value['datatype']) ? $value['datatype'] : null; } else { $this->_value = $value; $this->_lang = $lang ? $lang : null; $this->_datatype = $datatype ? $datatype : null; } // Automatic datatype selection if ($this->_datatype == null) { if (is_float($this->_value)) { $this->_datatype = 'xsd:decimal'; } else { if (is_int($this->_value)) { $this->_datatype = 'xsd:integer'; } else { if (is_bool($this->_value)) { $this->_datatype = 'xsd:boolean'; } } } } // Expand shortened URIs (qnames) if ($this->_datatype) { $this->_datatype = EasyRdf_Namespace::expand($this->_datatype); } }
/** Create a new literal object * * PHP values of type bool, int or float, will automatically be converted * to the corresponding datatype and PHP sub-class. * * If a registered datatype is given, then the registered subclass of EasyRdf_Literal * will instantiated. * * Note that literals are not required to have a language or datatype. * Literals cannot have both a language and a datatype. * * @param mixed $value The value of the literal or an associative array * @param string $lang The natural language of the literal or null (e.g. 'en') * @param string $datatype The datatype of the literal or null (e.g. 'xsd:integer') * @return object EasyRdf_Literal (or subclass of EasyRdf_Literal) */ public static function create($value, $lang = null, $datatype = null) { if (EasyRdf_Utils::is_associative_array($value)) { $lang = isset($value['lang']) ? $value['lang'] : null; $datatype = isset($value['datatype']) ? $value['datatype'] : null; $value = isset($value['value']) ? $value['value'] : null; } if ($datatype == null) { if ($lang == null) { // Automatic datatype selection $datatype = self::getDatatypeForValue($value); } } else { // Expand shortened URIs (qnames) $datatype = EasyRdf_Namespace::expand($datatype); } // Work out what class to use for this datatype if (isset(self::$_datatypeMap[$datatype])) { $class = self::$_datatypeMap[$datatype]; } else { $class = 'EasyRdf_Literal'; } return new $class($value, $lang, $datatype); }
/** Add data to the graph * * The resource can either be a resource or the URI of a resource. * * The properties can either be a single property name or an * associate array of property names and values. * * The value can either be a single value or an array of values. * * Examples: * $res = $graph->resource("http://www.example.com"); * $graph->add($res, 'prefix:property', 'value'); * $graph->add($res, 'prefix:property', array('value1',value2')); * $graph->add($res, array('prefix:property' => 'value1')); * $graph->add($res, 'foaf:knows', array( 'foaf:name' => 'Name')); * $graph->add($res, array('foaf:knows' => array( 'foaf:name' => 'Name')); * * @param mixed $resource The resource to add data to * @param mixed $properties The properties or property names * @param mixed $value The new value for the property */ public function add($resource, $properties, $value = null) { if (!is_object($resource)) { $resource = $this->resource($resource); } else { if (!$resource instanceof EasyRdf_Resource) { throw new InvalidArgumentException("\$resource should be an instance of the EasyRdf_Resource class"); } } if (EasyRdf_Utils::is_associative_array($properties)) { foreach ($properties as $property => $value) { $this->add($resource, $property, $value); } return; } else { if (EasyRdf_Utils::is_associative_array($value)) { if (isset($value['rdf:type'])) { $bnode = $this->newBNode($value['rdf:type']); } else { $bnode = $this->newBNode(); } $bnode->add($value); $value = $bnode; } $resource->add($properties, $value); } }
public function testIsAssocIntPreppend() { $arr = array('foo' => 'bar'); array_unshift($arr, 'rat'); $this->assertFalse(EasyRdf_Utils::is_associative_array($arr)); }
/** Add values to an existing property * * The properties can either be a single property name or an * associate array of property names and values. * * The value can either be a single value or an array of values. * * Examples: * $resource->add('prefix:property', 'value'); * $resource->add('prefix:property', array('value1',value2')); * $resource->add(array('prefix:property' => 'value1')); * * @param mixed $resource The resource to add data to * @param mixed $properties The properties or property names * @param mixed $value The new value for the property * @return array Array of all values associated with property. */ public function add($properties, $value = null) { if ($properties == null or $properties == '') { throw new InvalidArgumentException("\$properties cannot be null or empty"); } // Have multiple properties been given? if (is_array($properties)) { if (EasyRdf_Utils::is_associative_array($properties)) { foreach ($properties as $property => $value) { $this->add($property, $value); } return; } else { foreach ($properties as $property) { $this->add($property, $value); } return; } } else { $property = $properties; } // No value given? if ($value == null) { return null; } // Get the existing values for the property if (array_key_exists($property, $this->_properties)) { $values = $this->_properties[$property]; } else { $values = array(); } // Add to array of values, if it isn't already there if (is_array($value)) { foreach ($value as $v) { if (!in_array($v, $values)) { array_push($values, $v); } } } else { if (!in_array($value, $values)) { array_push($values, $value); } } return $this->set($property, $values); }
/** Add values to an existing property * * The properties can either be a single property name or an * associate array of property names and values. * * The value can either be a single value or an array of values. * * Examples: * $resource->add('prefix:property', 'value'); * $resource->add('prefix:property', array('value1',value2')); * $resource->add(array('prefix:property' => 'value1')); * * @param mixed $resource The resource to add data to * @param mixed $properties The properties or property names * @param mixed $value The new value for the property * @return array Array of all values associated with property. */ public function add($properties, $values = null) { if ($properties == null or $properties == '') { throw new InvalidArgumentException("\$properties cannot be null or empty"); } // Have multiple properties been given? if (is_array($properties)) { if (EasyRdf_Utils::is_associative_array($properties)) { foreach ($properties as $property => $value) { $this->add($property, $value); } return; } else { foreach ($properties as $property) { $this->add($property, $values); } return; } } else { $property = $properties; } // No value given? if ($values == null) { return null; } // Create the property if it doesn't already exist $property = EasyRdf_Namespace::expand($property); if (!isset($this->_properties[$property])) { $this->_properties[$property] = array(); } if (!is_array($values)) { $values = array($values); } // Convert literal values into objects $objects = array(); foreach ($values as $value) { if (is_object($value)) { $objects[] = $value; } else { $objects[] = new EasyRdf_Literal($value); } } // Add the objects, if they don't already exist foreach ($objects as $object) { if (!$this->matches($property, $object)) { array_push($this->_properties[$property], $object); if ($object instanceof EasyRdf_Resource) { $object->addInverse($property, $this); } } } return $this->_properties[$property]; }