If the previous pathElement is incomplete (has no name or ID specified),
an InvalidArgumentException will be thrown. Once an incomplete
pathElement is given, the key cannot be extended any further.
Example:
$key->pathElement('Person', 'Jane');
In cases where the identifier type is ambiguous, you can choose the
type to be used.
$key->pathElement('Robots', '1337', [
'identifierType' => Key::TYPE_NAME
]);
public pathElement ( string $kind, string | integer $identifier = null, array $options = [] ) : |
||
$kind | string | The kind. |
$identifier | string | integer | [optional] The name or ID of the object. |
$options | array | { Configuration Options @type string $identifierType [optional] If omitted, the type will be determined internally. Setting this to either `Key::TYPE_ID` or `Key::TYPE_NAME` will force the pathElement identifier type. } |
return |
/** * Create a single Key instance * * @see https://cloud.google.com/datastore/reference/rest/v1/Key Key * @see https://cloud.google.com/datastore/reference/rest/v1/Key#PathElement PathElement * * @param string $kind The kind. * @param string|int $identifier [optional] The ID or name. * @param array $options [optional] { * Configuration Options * * @type string $identifierType If omitted, type will be determined * internally. In cases where any ambiguity can be expected (i.e. * if you want to create keys with `name` but your values may * pass PHP's `is_numeric()` check), this value may be * explicitly set using `Key::TYPE_ID` or `Key::TYPE_NAME`. * } * @return Key */ public function key($kind, $identifier = null, array $options = []) { $options += ['namespaceId' => $this->namespaceId]; $key = new Key($this->projectId, $options); $key->pathElement($kind, $identifier, $options); return $key; }
/** * @expectedException PHPUnit_Framework_Error_Warning */ public function testGcWithException() { $key1 = new Key('projectid'); $key1->pathElement(self::KIND, 'sessionid1'); $key2 = new Key('projectid'); $key2->pathElement(self::KIND, 'sessionid2'); $entity1 = new Entity($key1); $entity2 = new Entity($key2); $query = $this->prophesize(Query::class); $query->kind(self::KIND)->shouldBeCalledTimes(1)->willReturn($query->reveal()); $that = $this; $query->filter(Argument::type('string'), Argument::type('string'), Argument::type('int'))->shouldBeCalledTimes(1)->will(function ($args) use($that, $query) { $that->assertEquals('t', $args[0]); $that->assertEquals('<', $args[1]); $that->assertInternalType('int', $args[2]); $diff = time() - $args[2]; // 2 seconds grace period should be enough $that->assertTrue($diff <= 102); $that->assertTrue($diff >= 100); return $query->reveal(); }); $query->order('t')->shouldBeCalledTimes(1)->willReturn($query->reveal()); $query->keysOnly()->shouldBeCalledTimes(1)->willReturn($query->reveal()); $query->limit(1000)->shouldBeCalledTimes(1)->willReturn($query->reveal()); $this->datastore->transaction()->shouldBeCalledTimes(1)->willReturn($this->transaction->reveal()); $this->datastore->query()->shouldBeCalledTimes(1)->willReturn($query->reveal()); $this->datastore->runQuery(Argument::type(Query::class), Argument::type('array'))->shouldBeCalledTimes(1)->will(function ($args) use($that, $query, $entity1, $entity2) { $that->assertEquals($query->reveal(), $args[0]); $that->assertEquals(['namespaceId' => self::NAMESPACE_ID], $args[1]); return [$entity1, $entity2]; }); $this->datastore->deleteBatch([$key1, $key2])->shouldBeCalledTimes(1)->willThrow(new Exception()); $datastoreSessionHandler = new DatastoreSessionHandler($this->datastore->reveal(), 1000); $datastoreSessionHandler->open(self::NAMESPACE_ID, self::KIND); $ret = $datastoreSessionHandler->gc(100); $this->assertEquals(false, $ret); }
public function testJsonSerialize() { $key = new Key('foo'); $key->pathElement('Robots', '1000', Key::TYPE_NAME); $this->assertEquals($key->jsonSerialize(), $key->keyObject()); }