-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
90% - Prevent null values being added to ExtendedGraph #82
Merged
lordtatty
merged 14 commits into
master
from
do-not-allow-null-values-to-be-added-to-extendedgraph
Jul 23, 2015
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1b8edf8
Prevent null values being added to ExtendedGraph
lordtatty ec3fb44
Added tests
lordtatty 5c89879
Make valid values stricter as per PR comments
lordtatty ac2071e
renamed method as per PR
lordtatty 54fbb4f
simplified conditional from PR
lordtatty cdb99be
Fixed up from PR comments
lordtatty 3a963e4
Improve testing
lordtatty a8cfc26
Better testing
lordtatty 6a96dcb
Fixed up docblock
lordtatty 460f258
Enforce subjects and predicates are also strings
lordtatty 5396f60
Ensure exception is thrown if a blank subject is added to ExtendedGraph
lordtatty 60d0e3c
Fixed up from PR comments
lordtatty a895200
Fixed up formatting
lordtatty c8ef3f6
Renamed Methods as per PR
lordtatty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -141,7 +141,10 @@ public function make_resource_array($resource) { | |
* @return boolean true if the triple was new, false if it already existed in the graph | ||
*/ | ||
public function add_resource_triple($s, $p, $o) { | ||
return $this->_add_triple($s, $p, array('type' => strpos($o, '_:' ) === 0 ? 'bnode' : 'uri', 'value' => $o)); | ||
if($this->isValidResourceValue($o)) { | ||
return $this->_add_triple($s, $p, array('type' => strpos($o, '_:') === 0 ? 'bnode' : 'uri', 'value' => $o)); | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
|
@@ -154,41 +157,83 @@ public function add_resource_triple($s, $p, $o) { | |
* @return boolean true if the triple was new, false if it already existed in the graph | ||
*/ | ||
public function add_literal_triple($s, $p, $o, $lang = null, $dt = null) { | ||
$o_info = array('type' => 'literal', 'value' => $o); | ||
if ( $lang != null ) { | ||
$o_info['lang'] = $lang; | ||
} | ||
if ( $dt != null ) { | ||
$o_info['datatype'] = $dt; | ||
if($this->isValidLiteralValue($o)) { | ||
$o_info = array('type' => 'literal', 'value' => $o); | ||
if ($lang != null) { | ||
$o_info['lang'] = $lang; | ||
} | ||
if ($dt != null) { | ||
$o_info['datatype'] = $dt; | ||
} | ||
return $this->_add_triple($s, $p, $o_info); | ||
} | ||
return $this->_add_triple($s, $p, $o_info); | ||
return false; | ||
} | ||
|
||
/** | ||
* @param string $s | ||
* @param string $p | ||
* @param array $o_info | ||
* @throws Exceptions\Exception | ||
* @return bool | ||
*/ | ||
private function _add_triple($s, $p, Array $o_info) { | ||
// The value $o should already have been validated by this point | ||
// It's validation differs depending on whether it is a literal or resource | ||
// So just check the subject and predicate here... | ||
if(!$this->isValidResourceValue($s)){ | ||
throw new \Tripod\Exceptions\Exception("The subject is invalid"); | ||
} | ||
if(!$this->isValidResourceValue($p)){ | ||
throw new \Tripod\Exceptions\Exception("The predicate is invalid"); | ||
} | ||
if (!isset($this->_index[$s])) { | ||
$this->_index[$s] = array(); | ||
$this->_index[$s][$p] = array( $o_info ); | ||
$this->_index[$s][$p] = array($o_info); | ||
return true; | ||
} | ||
elseif (!isset($this->_index[$s][$p])) { | ||
$this->_index[$s][$p] = array( $o_info); | ||
} elseif (!isset($this->_index[$s][$p])) { | ||
$this->_index[$s][$p] = array($o_info); | ||
return true; | ||
} | ||
else { | ||
if ( ! in_array( $o_info, $this->_index[$s][$p] ) ) { | ||
} else { | ||
if (!in_array($o_info, $this->_index[$s][$p])) { | ||
$this->_index[$s][$p][] = $o_info; | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
||
/** | ||
* Check if a triple value is valid. | ||
* | ||
* Ideally a valid literal value should be a string | ||
* but accepting scalars so we can handle legacy data | ||
* which was not type-checked. | ||
* | ||
* @param string $value | ||
* @return bool | ||
*/ | ||
protected function isValidLiteralValue($value){ | ||
if(!is_scalar($value)){ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Check if a triple value is valid. | ||
* | ||
* @param string $value | ||
* @return bool | ||
*/ | ||
protected function isValidResourceValue($value){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Too verbose, rename is |
||
if(!is_string($value) || empty($value)){ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* @deprecated this is deprecated | ||
*/ | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too verbose, rename
isValidLiteral()