-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added integration tests. - Removed redundant JSON fixtures. - Updated docs accordingly.
- Loading branch information
Showing
16 changed files
with
171 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
# Contributing | ||
|
||
Contributions in the form of suggestions, PR's and beer are *always* welcome. | ||
This module is written to PSR-2, follows Semver and uses namespacing. | ||
|
||
Contributions in the form of suggestions and PR's with regard to the above are *always welcome* | ||
as is beer is most formats, apart from that stuff called "craft beer" which is so hoppy, I could be drinking | ||
mushed Daffodils and not know the difference. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
* | ||
* <code> | ||
* static $db = [ | ||
* 'MyJSONStructure' => 'JSONText' | ||
* 'MyJSONStructure' => '\JSONText\Fields\JSONText' | ||
* ]; | ||
* </code> | ||
* | ||
|
@@ -23,7 +23,6 @@ | |
* @package silverstripe-jsontext | ||
* @subpackage fields | ||
* @author Russell Michell <[email protected]> | ||
* @todo Rename query() to getValue() that accepts optional param $expr (for JSONPath queries) | ||
*/ | ||
|
||
namespace JSONText\Fields; | ||
|
@@ -107,8 +106,8 @@ public function requireField() | |
'type' => 'text', | ||
'parts' => $parts | ||
]; | ||
|
||
DB::require_field($this->tableName, $this->name, $values); | ||
\DB::require_field($this->tableName, $this->name, $values); | ||
} | ||
|
||
/** | ||
|
@@ -148,14 +147,6 @@ public function setReturnType($type) | |
return $this; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getReturnType() | ||
{ | ||
return $this->returnType; | ||
} | ||
|
||
/** | ||
* Returns the value of this field as an iterable. | ||
* | ||
|
@@ -164,16 +155,16 @@ public function getReturnType() | |
*/ | ||
public function getJSONStore() | ||
{ | ||
if (!$json = $this->getValue()) { | ||
return new \Peekmo\JsonPath\JsonStore('[]'); | ||
if (!$value = $this->getValue()) { | ||
return new JsonStore('[]'); | ||
} | ||
|
||
if (!$this->isJson($json)) { | ||
if (!$this->isJson($value)) { | ||
$msg = 'DB data is munged.'; | ||
throw new JSONTextException($msg); | ||
} | ||
|
||
$this->jsonStore = new \Peekmo\JsonPath\JsonStore($json); | ||
$this->jsonStore = new JsonStore($value); | ||
|
||
return $this->jsonStore; | ||
} | ||
|
@@ -366,7 +357,7 @@ public function query($operator, $operand = null) | |
} | ||
|
||
$validType = ($isEx ? self::JSONTEXT_QUERY_JSONPATH : self::JSONTEXT_QUERY_OPERATOR); | ||
if ($marshalled = $this->marshallQuery(func_get_args(), $validType)) { | ||
if ($marshalled = $this->marshallQuery(func_get_args(), $validType, $this->getJSONStore())) { | ||
return $this->returnAsType($marshalled); | ||
} | ||
|
||
|
@@ -423,9 +414,6 @@ private function marshallQuery($args, $type = 1) | |
*/ | ||
public function setValue($value, $record = null, $expr = '') | ||
{ | ||
// Deal with standard SS behaviour | ||
parent::setValue($value, $record); | ||
|
||
if (empty($expr)) { | ||
$this->value = $value; | ||
} else { | ||
|
@@ -434,13 +422,16 @@ public function setValue($value, $record = null, $expr = '') | |
throw new JSONTextException($msg); | ||
} | ||
|
||
if (!$this->jsonStore->set($expr, $value)) { | ||
if (!$this->getJSONStore()->set($expr, $value)) { | ||
$msg = 'Failed to properly set custom data to the JSONStore in ' . __FUNCTION__; | ||
throw new JSONTextException($msg); | ||
} | ||
|
||
$this->value = $this->jsonStore->toString(); | ||
} | ||
|
||
// Deal with standard SS behaviour | ||
parent::setValue($this->value, $record); | ||
|
||
return $this; | ||
} | ||
|
@@ -455,7 +446,7 @@ public function setValue($value, $record = null, $expr = '') | |
private function returnAsType($data) | ||
{ | ||
$data = (array) $data; | ||
$type = $this->getReturnType(); | ||
$type = $this->returnType; | ||
if ($type === 'array') { | ||
if (!count($data)) { | ||
return []; | ||
|
@@ -485,18 +476,24 @@ private function returnAsType($data) | |
} | ||
|
||
/** | ||
* Create an instance of {@link JSONBackend} according to the value of JSONText::backend defined by SS config. | ||
* Create an instance of {@link JSONBackend} according to the value of JSONText::backend defined in SS config. | ||
* | ||
* @param string operand | ||
* @return JSONBackend | ||
* @throws JSONTextException | ||
*/ | ||
protected function createBackendInst($operand) | ||
{ | ||
$backend = $this->config()->backend; | ||
$dbBackendClass = ucfirst($backend) . 'JSONBackend'; | ||
$dbBackendClass = '\JSONText\Backends\\' . ucfirst($backend) . 'JSONBackend'; | ||
|
||
if (!class_exists($dbBackendClass)) { | ||
$msg = 'JSONText backend class ' . $dbBackendClass . ' not found.'; | ||
throw new JSONTextException($msg); | ||
} | ||
|
||
return \Injector::inst()->createWithArgs( | ||
'\JSONText\Backends\\' . $dbBackendClass, [ | ||
$dbBackendClass, [ | ||
$operand, | ||
$this | ||
]); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.