-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
96 additions
and
8 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/** | ||
* Array wrapper to both allow for property-based access to Ancestors for | ||
* writing during entity creation as well as enforcing read-only status | ||
* for objects that have already been created that have ancestors. | ||
*/ | ||
namespace Datachore; | ||
use \google\appengine\datastore\v4\Key as GoogleKey; | ||
use \google\appengine\datastore\v4\PathElement as GooglePathElement; | ||
|
||
class Ancestors extends \ArrayObject | ||
{ | ||
protected $read_only = false; | ||
|
||
|
||
public function offsetSet($index, $newval) | ||
{ | ||
if ($this->read_only) | ||
{ | ||
throw new \InvalidArgumentException("Cannot modify ancestors once an entity is already created"); | ||
} | ||
|
||
if ($index && (!is_numeric($index) || ($index != (int)$index))) | ||
{ | ||
throw new \InvalidArgumentException("only numeric indexes are allowed: {$index}"); | ||
} | ||
|
||
if ($newval instanceof Model) | ||
{ | ||
$newval = $newval->key->getPathElement( | ||
$newval->key->getPathElementSize()-1 | ||
); | ||
} | ||
else if (!$newval instanceof GooglePathElement) | ||
{ | ||
throw new \InvalidArgumentException("Illegal value for ancestor"); | ||
} | ||
|
||
parent::offsetSet($index, $newval); | ||
} | ||
|
||
public function __construct(GoogleKey $key = null) | ||
{ | ||
if ($key) | ||
{ | ||
for ($i = 0; $i < $key->getPathElementSize() - 1; $i++) | ||
{ | ||
$this->offsetSet($i, $key->getPathElement($i)); | ||
} | ||
$this->read_only = true; | ||
} | ||
} | ||
} |
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