Skip to content

Commit

Permalink
API: Moved $allowed_operators config static to JSONBackend.
Browse files Browse the repository at this point in the history
Updated comments.
  • Loading branch information
phptek committed Jun 30, 2016
1 parent bae9c50 commit 5844b86
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion code/backends/JSONBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use JSONText\Exceptions\JSONTextException;
use JSONText\Fields\JSONText;

abstract class JSONBackend
abstract class JSONBackend extends \Object
{

/**
Expand Down
18 changes: 14 additions & 4 deletions code/backends/PostgresJSONBackend.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<?php

/**
* DB backend for use with a Postgres RDBMS like JSON querying syntax.
* JSONText database backend that encapsulates a Postgres-like syntax for JSON querying.
*
* @package silverstripe-jsontext
* @subpackage models
* @author Russell Michell <[email protected]>
* @see {@link JSONBackend}
* @see https://github.com/Peekmo/JsonPath/blob/master/tests/JsonStoreTest.php
* @see http://goessner.net/articles/JsonPath/
* @see https://www.postgresql.org/docs/9.6/static/functions-json.html
*/

namespace JSONText\Backends;
Expand All @@ -17,6 +15,18 @@

class PostgresJSONBackend extends JSONBackend
{
/**
* An array of acceptable operators for this backend.
*
* @var array
* @config
*/
private static $allowed_operators = [
'matchOnInt' => '->',
'matchOnStr' => '->>',
'matchOnPath' => '#>'
];

/**
* @inheritdoc
*/
Expand Down
34 changes: 12 additions & 22 deletions code/models/fieldtypes/JSONText.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,11 @@ class JSONText extends \StringField
* @config
*/
private static $backend = 'postgres';

/**
* @var array
* @config
*
* [<backend>] => [
* [<method> => <operator>]
* ]; // For use in query() method.
* @var boolean
*/
private static $allowed_operators = [
'postgres' => [
'matchOnInt' => '->',
'matchOnStr' => '->>',
'matchOnPath' => '#>'
]
];
protected $nullifyEmpty = false;

/**
* Legitimate query return types.
Expand All @@ -83,6 +72,8 @@ class JSONText extends \StringField
protected $returnType = 'json';

/**
* A representation of this field's data as a {@link JSONStore} object.
*
* @var \Peekmo\JsonPath\JsonStore
*/
protected $jsonStore;
Expand Down Expand Up @@ -335,11 +326,11 @@ public function query($operator, $operand = null)
}

$isOperator = !is_null($operand) && $this->isValidOperator($operator);
$isExpresssion = is_null($operand) && $this->isValidExpression($operator);
$isExpression = is_null($operand) && $this->isValidExpression($operator);

if ($isOperator) {
$type = self::JSONTEXT_QUERY_OPERATOR;
} else if ($isExpresssion) {
} else if ($isExpression) {
$type = self::JSONTEXT_QUERY_JSONPATH;
} else {
$msg = 'JSON expression: ' . $operator . ' is invalid.';
Expand All @@ -363,15 +354,14 @@ public function query($operator, $operand = null)
*/
private function marshallQuery($args, $type = 1)
{
$backend = $this->config()->backend;
$operator = $expression = $args[0];
$operand = isset($args[1]) ? $args[1] : null;
$operators = $this->config()->allowed_operators[$backend];
$operatorParamIsValid = $type === self::JSONTEXT_QUERY_OPERATOR;
$expressionParamIsValid = $type === self::JSONTEXT_QUERY_JSONPATH;

if ($operatorParamIsValid) {
$dbBackendInst = $this->createBackendInst($operand);
$operators = $dbBackendInst->config()->allowed_operators;
foreach ($operators as $routine => $backendOperator) {
if ($operator === $backendOperator && $result = $dbBackendInst->$routine()) {
return $result;
Expand Down Expand Up @@ -475,7 +465,7 @@ private function returnAsType($data)
* @return JSONBackend
* @throws JSONTextException
*/
protected function createBackendInst($operand)
protected function createBackendInst($operand = '')
{
$backend = $this->config()->backend;
$dbBackendClass = '\JSONText\Backends\\' . ucfirst($backend) . 'JSONBackend';
Expand Down Expand Up @@ -535,11 +525,11 @@ public function isValidDBValue($value) {
*/
public function isValidOperator($operator)
{
$backend = $this->config()->backend;
$dbBackendInst = $this->createBackendInst();

return $operator && in_array(
$operator,
$this->config()->allowed_operators[$backend],
$operator,
$dbBackendInst->config()->allowed_operators,
true
);
}
Expand Down

0 comments on commit 5844b86

Please sign in to comment.