1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
<?php
/*
* (c) Markus Lanthaler <mail@markus-lanthaler.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ML\JsonLD;
use stdClass as JsonObject;
/**
* A typed value represents a value with an associated type.
*
* @author Markus Lanthaler <mail@markus-lanthaler.com>
*/
final class TypedValue extends Value
{
/**
* The type of the value in the form of an IRI.
*
* @var string
*/
private $type;
/**
* Constructor
*
* @param string $value The value.
* @param string $type The type.
*/
public function __construct($value, $type)
{
$this->setValue($value);
$this->setType($type);
}
/**
* Set the type
*
* For the sake of simplicity, the type is currently just a Node
* identifier in the form of a string and not a Node reference.
* This might be changed in the future.
*
* @param string $type The type.
*
* @return self
*
* @throws \InvalidArgumentException If the type is not a string. No
* further checks are currently done.
*/
public function setType($type)
{
if (!is_string($type)) {
throw new \InvalidArgumentException('type must be a string.');
}
$this->type = $type;
return $this;
}
/**
* Get the type
*
* For the sake of simplicity, the type is currently just a Node
* identifier in the form of a string and not a Node reference.
* This might be changed in the future.
*
* @return string The type.
*/
public function getType()
{
return $this->type;
}
/**
* {@inheritdoc}
*/
public function toJsonLd($useNativeTypes = true)
{
$result = new JsonObject();
if (RdfConstants::XSD_STRING === $this->type) {
$result->{'@value'} = $this->value;
return $result;
}
if (true === $useNativeTypes) {
if (RdfConstants::XSD_BOOLEAN === $this->type) {
if ('true' === $this->value) {
$result->{'@value'} = true;
return $result;
} elseif ('false' === $this->value) {
$result->{'@value'} = false;
return $result;
}
} elseif (RdfConstants::XSD_INTEGER === $this->type) {
if (preg_match('/^[\+|-]?\d+$/', trim($this->value))) {
$result->{'@value'} = intval($this->value);
return $result;
}
} elseif (RdfConstants::XSD_DOUBLE === $this->type) {
// TODO Need to handle +/-INF and NaN as well?
if (preg_match('/^[\+|-]?\d+(?:\.\d*)?(?:[eE][\+|-]?\d+)?$/', trim($this->value))) {
$result->{'@value'} = floatval($this->value);
return $result;
}
// TODO Need to preserve doubles without fraction for round-tripping??
}
}
$result->{'@value'} = $this->value;
$result->{'@type'} = $this->type;
return $result;
}
/**
* {@inheritdoc}
*/
public function equals($other)
{
if (get_class($this) !== get_class($other)) {
return false;
}
return ($this->value === $other->value) && ($this->type === $other->type);
}
}
|