Skip to content

Commit

Permalink
Add optional types parameter to jsonLDSerialize
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Aug 25, 2017
1 parent 542cc8d commit 251e3ba
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -5,6 +5,7 @@ This changelog tracks features and fixes of jskos PHP library.
## NEXT

* Fix method Set->findURI to actually return an index
* Add optional types parameter to jsonLDSerialize

## 0.3.2

Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php" colors="true">
<testsuites name="JSKOS-PHP test suite">
<testsuites name="test suite">
<directory>tests</directory>
</testsuites>
<filter>
Expand Down
5 changes: 3 additions & 2 deletions src/ConceptBundle.php
Expand Up @@ -37,12 +37,13 @@ class ConceptBundle extends PrettyJsonSerializable
/**
* Returns data which should be serialized to JSON.
* @param string $context
* @param bool $types
*/
public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT)
public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT, bool $types = null)
{
$members = [];
foreach ($this->members as $m) {
$members[] = $m->jsonLDSerialize('');
$members[] = $m->jsonLDSerialize('', $types);
}
$json = [];
if ($context) {
Expand Down
9 changes: 3 additions & 6 deletions src/Container.php
Expand Up @@ -167,13 +167,10 @@ public function getIterator()

# extends PrettyJsonSerializable:

/**
* Return a data structure to serialize this container as JSON.
*/
public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT)
public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT, bool $types = null)
{
$set = array_map(function ($m) {
return is_object($m) ? $m->jsonLDSerialize('') : $m;
$set = array_map(function ($m) use ($types) {
return is_object($m) ? $m->jsonLDSerialize('', $types) : $m;
}, $this->members);

if (!$this->closed) {
Expand Down
10 changes: 5 additions & 5 deletions src/DataType.php
Expand Up @@ -31,7 +31,7 @@ abstract class DataType extends PrettyJsonSerializable
/**
* Get field definition from FIELDS, including parent classes.
*/
protected static function fieldType(string $field, bool $strict=false)
protected static function fieldType(string $field, bool $strict = false)
{
$class = get_called_class();
while ($class && $class != self::class) {
Expand All @@ -51,11 +51,11 @@ protected static function fieldType(string $field, bool $strict=false)
private function fieldException($field, $message)
{
return new InvalidArgumentException(
get_called_class()."->$field must $message"
get_called_class() . "->$field must $message"
);
}

protected function setField(string $field, $value, bool $strict=true)
protected function setField(string $field, $value, bool $strict = true)
{
if ($field == '@context') {
return;
Expand All @@ -72,7 +72,7 @@ protected function setField(string $field, $value, bool $strict=true)
if ($type[0] == 'Set') {
if (!($value instanceof Set)) {
if (is_array($value)) {
$class = 'JSKOS\\'.$type[1];
$class = 'JSKOS\\' . $type[1];
$value = new Set(
array_map(function ($m) use ($class) {
if (is_null($m)) {
Expand Down Expand Up @@ -135,7 +135,7 @@ public function &__get($field)
}
} else {
trigger_error(
"Undefined property: ".get_called_class()."::$$field",
"Undefined property: " . get_called_class() . "::$$field",
\E_USER_NOTICE
);
}
Expand Down
7 changes: 2 additions & 5 deletions src/LanguageMapOfLists.php
Expand Up @@ -35,14 +35,11 @@ public function contains($member): bool
return false;
}

/**
* Return a data structure to serialize this container as JSON.
*/
public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT)
public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT, bool $types = null)
{
$map = new \stdClass();
foreach ($this->members as $lang => $list) {
$map->$lang = $list->jsonLDSerialize('');
$map->$lang = $list->jsonLDSerialize('', $types);
}
return $map;
}
Expand Down
5 changes: 1 addition & 4 deletions src/LanguageMapOfStrings.php
Expand Up @@ -11,10 +11,7 @@ class LanguageMapOfStrings extends LanguageMap
{
use StringContainer;

/**
* Return a data structure to serialize this container as JSON.
*/
public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT)
public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT, bool $types = null)
{
return (object)$this->members;
}
Expand Down
15 changes: 10 additions & 5 deletions src/PrettyJsonSerializable.php
Expand Up @@ -26,20 +26,21 @@ public function jsonSerialize()
* Keys are sorted by Unicode codepoint for stable output.
*
* @param string $context optional JSON-LD context URL. Use empty string to omit.
* @param bool $types include default type URIs.
*/
public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT)
public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT, bool $types = null)
{
$json = [];

foreach ($this as $key => $value) {
if (isset($value)) {
if ($value instanceof PrettyJsonSerializable) {
$value = $value->jsonLDSerialize('');
$value = $value->jsonLDSerialize('', false);
} elseif (is_array($value) and !count(array_filter(array_keys($value), 'is_string'))) {
$a = [];
foreach ($value as $m) {
if ($m instanceof PrettyJsonSerializable) {
$m = $m->jsonLDSerialize('');
$m = $m->jsonLDSerialize('', false);
}
$a[] = $m;
}
Expand All @@ -49,10 +50,14 @@ public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT)
}
}

# don't serialize implicitly deriveable types for brevity
if ($context) {
$json['@context'] = $context;
} elseif (count($json['type'] ?? []) == 1) {
# don't serialize implicitly deriveable types for brevity
}
if ($types === null) {
$types = (bool)$context;
}
if (!$types && count($json['type'] ?? []) == 1) {
if ($json['type'][0] == static::TYPES[0]) {
unset($json['type']);
}
Expand Down
42 changes: 26 additions & 16 deletions tests/ConceptTest.php
Expand Up @@ -33,25 +33,35 @@ public function testClosed()

public function testJson()
{
$concept = new Concept(['uri'=>'x:1']);
$concept->prefLabel = ['en' => 'test'];
$fields = ['uri'=>'x:1'];

$concept = new Concept($fields);
$this->assertEquals($fields, $concept->jsonLDSerialize(''));
$this->assertEquals($fields, $concept->jsonLDSerialize('', false));

$fields['@context'] = 'e:x';
$this->assertEquals($fields, $concept->jsonLDSerialize('e:x', false));

$fields['type'] = [Concept::TYPES[0]];
$this->assertEquals($fields, $concept->jsonLDSerialize('e:x'));
$this->assertEquals($fields, $concept->jsonLDSerialize('e:x', true));

unset($fields['@context']);
$this->assertEquals($fields, $concept->jsonLDSerialize('', true));

$fields['@context'] = 'https://gbv.github.io/jskos/context.json';
$this->assertEquals($fields, $concept->jsonLDSerialize());


$concept->prefLabel = ($fields['prefLabel'] = ['en' => 'test']);
$concept->narrower = [];
$concept->narrower[] = new Concept(['uri'=>'x:2']);
$concept->broader = [null];
$concept->identifier = [null, 'y:1'];

$expect = [
'@context' => 'https://gbv.github.io/jskos/context.json',
'type' => [Concept::TYPES[0]],
'uri' => 'x:1',
'prefLabel' => [ 'en' => 'test' ],
'narrower' => [ [ 'uri' => 'x:2' ] ],
'broader' => [ null ],
'identifier' => [ 'y:1', null ]
];
ksort($expect);
$this->assertEquals(json_encode($expect), json_encode($concept));
$fields['narrower'] = [ [ 'uri' => 'x:2' ] ];
$concept->broader = ($fields['broader'] = [null]);
$concept->identifier = [null, 'y:1', 'y:1'];
$fields['identifier'] = ['y:1', null];
ksort($fields);
$this->assertEquals(json_encode($fields), json_encode($concept));
}

public function testNested()
Expand Down

0 comments on commit 251e3ba

Please sign in to comment.