/** Constructor for creating a new boolean literal * * If the value is not a string, then it will be converted to 'true' or 'false'. * * @param mixed $value The value of the literal * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'xsd:boolean') */ public function __construct($value, $lang = null, $datatype = null) { if (!is_string($value)) { $value = $value ? 'true' : 'false'; } parent::__construct($value, null, $datatype); }
/** Constructor for creating a new xsd:hexBinary literal * * @param mixed $value The value of the literal (already encoded as hexadecimal) * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'xsd:hexBinary') * * @throws \InvalidArgumentException */ public function __construct($value, $lang = null, $datatype = null) { // Normalise the canonical representation, as specified here: // http://www.w3.org/TR/xmlschema-2/#hexBinary-canonical-repr $value = strtoupper($value); // Validate the data if (preg_match('/[^A-F0-9]/', $value)) { throw new \InvalidArgumentException("Literal of type xsd:hexBinary contains non-hexadecimal characters"); } parent::__construct(strtoupper($value), null, 'xsd:hexBinary'); }
/** Constructor for creating a new date literal * * If the value is a DateTime object, then it will be converted to the xsd:date format. * If no value is given or is is null, then the current date is used. * * @see \DateTime * * @param mixed $value The value of the literal * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'xsd:date') */ public function __construct($value = null, $lang = null, $datatype = null) { // If $value is null, use today's date if (is_null($value)) { $value = new \DateTime('today'); } // Convert DateTime object into string if ($value instanceof \DateTime) { $value = $value->format('Y-m-d'); } parent::__construct($value, null, $datatype); }
/** Constructor for creating a new decimal literal * * @param double|int|string $value The value of the literal * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'xsd:decimal') * * @throws \UnexpectedValueException */ public function __construct($value, $lang = null, $datatype = null) { if (is_string($value)) { self::validate($value); } elseif (is_double($value) or is_int($value)) { $locale_data = localeconv(); $value = str_replace($locale_data['decimal_point'], '.', strval($value)); } else { throw new \UnexpectedValueException('EasyRdf\\Literal\\Decimal expects int/float/string as value'); } $value = self::canonicalise($value); parent::__construct($value, null, $datatype); }
/** Constructor for creating a new date and time literal * * If the value is a DateTime object, then it will be converted to the xsd:dateTime format. * If no value is given or is is null, then the current time is used. * * @see \DateTime * * @param mixed $value The value of the literal * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'xsd:dateTime') */ public function __construct($value = null, $lang = null, $datatype = null) { // If $value is null, use 'now' if (is_null($value)) { $value = new \DateTime('now'); } // Convert DateTime objects into string if ($value instanceof \DateTime) { $atom = $value->format(\DateTime::ATOM); $value = preg_replace('/[\\+\\-]00(\\:?)00$/', 'Z', $atom); } Literal::__construct($value, null, $datatype); }
/** * Proceeds to a copy of resource provided as argument and stores it. * * @param BaseResource $resource * * @return BaseResource */ public function takeSnapshot($resource) { $res = $this->resource($resource->getUri()); $graph = $resource->getGraph(); foreach ($graph->toRdfPhp() as $resource2 => $properties) { if ($resource2 !== $res->getUri()) { continue; } if (!$this->unitOfWork->isManagementBlackListed($resource2)) { foreach ($properties as $property => $values) { foreach ($values as $value) { if ($value['type'] === 'bnode' || $value['type'] === 'uri') { $this->addResource($resource2, $property, $value['value']); } elseif ($value['type'] === 'literal') { $this->addLiteral($resource2, $property, Literal::create($value['value'], isset($value['lang']) ? $value['lang'] : null, isset($value['datatype']) ? $value['datatype'] : null)); } else { } } } } } return $res; }
/** Constructor for creating a new rdf:HTML literal * * @param mixed $value The HTML fragment * @param string $lang Should be null (literals with a datatype can't have a language) * @param string $datatype Optional datatype (default 'rdf:HTML') */ public function __construct($value, $lang = null, $datatype = null) { parent::__construct($value, null, $datatype); }
/** Create a new EasyRdf\Resource or EasyRdf\Literal depending * on the type of data passed in. * * @ignore */ protected function newTerm($data) { switch ($data['type']) { case 'bnode': return new Resource('_:' . $data['value']); case 'uri': return new Resource($data['value']); case 'literal': case 'typed-literal': return Literal::create($data); default: throw new Exception("Failed to parse SPARQL Query Results format, unknown term type: " . $data['type']); } }