Skip to content

Commit

Permalink
W.I.P
Browse files Browse the repository at this point in the history
Deeper JSON structures new accepted.
  • Loading branch information
phptek committed Jun 9, 2016
1 parent 749c7cc commit b421bbf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
34 changes: 30 additions & 4 deletions code/models/fieldtypes/JSONText.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class JSONText extends StringField
private static $allowed_operators = [
'postgres' => [
'getByKey' => '->', // int/str type-check performed at runtime.
'getByVal' => '<-' // int/str type-check performed at runtime.
'getByVal' => '->>' // int/str type-check performed at runtime.
]
];

Expand Down Expand Up @@ -151,6 +151,7 @@ public function getValueAsIterable()
new RecursiveArrayIterator(json_decode($json, true)),
RecursiveIteratorIterator::SELF_FIRST
);

}

/**
Expand Down Expand Up @@ -258,6 +259,7 @@ public function nth($n)
* @param string $operand
* @return mixed null|array
* @throws JSONTextException
* @todo How to increment an interator for each depth using $data->getDepth() and $i ??
*/
public function extract($operator, $operand)
{
Expand All @@ -272,8 +274,9 @@ public function extract($operator, $operand)
throw new JSONTextException($msg);
}

$cleaned = $this->cleanFlattenedArray($data);
$i = 0;
foreach ($data as $key => $val) {
foreach ($cleaned as $key => $val) {
if ($marshalled = $this->marshallQuery($key, $val, $i, func_get_args())) {
return $this->returnAsType($marshalled);
}
Expand All @@ -284,6 +287,29 @@ public function extract($operator, $operand)
return $this->returnAsType([]);
}

/**
* TEMPORARY - doesn't work to the extent expected anyway.
*
* @param $data
* @return array
*/
private function cleanFlattenedArray($data)
{
$flattened = iterator_to_array($data);
$cleaned = [];
foreach ($flattened as $key => $val) {
if (is_int($key)) {
continue;
}

if (!in_array($val, $cleaned)) {
$cleaned[$key] = $val;
}
}

return $cleaned;
}

/**
* Alias of self::extract().
*
Expand Down Expand Up @@ -326,8 +352,8 @@ private function marshallQuery($key, $val, $idx, $args)
$operand
]);

if ($operator === $backendOperator) {
return $backendDBApiInst->$routine();
if ($operator === $backendOperator && $result = $backendDBApiInst->$routine()) {
return $result;
}
}

Expand Down
21 changes: 13 additions & 8 deletions tests/JSONTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,25 @@ public function testNth_AsJson()

public function testExtract_AsArray()
{
// Hashed
$field = JSONText::create('MyJSON');
$field->setValue($this->fixture['hashed']);
$field->setReturnType('array');

// By key

$this->assertEquals(['british' => ['vauxhall', 'morris']], $field->extract('->', 'british'));
$this->assertEquals([0 => 'vauxhall'], $field->extract('->', 6));
$this->assertEquals(['british' => ['vauxhall', 'morris']], $field->extract('->', 2));
$this->assertEquals(['american' => ['buick', 'oldsmobile', 'ford']], $field->extract('->', 1));
$this->assertEquals([], $field->extract('->', '6')); // strict handling

// By value
// TODO: Use rewind() to get he tp-level key > val from the original iterator
$this->assertEquals(['british' => ['vauxhall', 'morris']], $field->extract('<-', 'morris'));
/* $this->assertEquals([0 => 'vauxhall'], $field->extract('<-', 6));
$this->assertEquals([], $field->extract('->', '6')); // strict handling*/
// Nested
$field = JSONText::create('MyJSON');
$field->setValue($this->fixture['nested']);
$field->setReturnType('array');

$this->assertEquals(['planes' => ['russian' => ['antonov', 'mig'], 'french' => 'airbus']], $field->extract('->', 'planes'));
//$this->assertEquals(['planes' => ['russian' => ['antonov', 'mig'], 'french' => 'airbus']], $field->extract('->', 1));
//$this->assertEquals(['american' => ['buick', 'oldsmobile', 'ford']], $field->extract('->', 1));
//$this->assertEquals([], $field->extract('->', '6')); // strict handling*/
}

}

0 comments on commit b421bbf

Please sign in to comment.