Skip to content

Commit 688a592

Browse files
committed
Modify classes to make use of parameter promotion
1 parent 103ba8d commit 688a592

4 files changed

Lines changed: 68 additions & 145 deletions

File tree

Datastore/src/EntityMapper.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,6 @@ class EntityMapper
3333
const DATE_FORMAT = 'Y-m-d\TH:i:s.uP';
3434
const DATE_FORMAT_NO_MS = 'Y-m-d\TH:i:sP';
3535

36-
/**
37-
* @var string
38-
*/
39-
private $projectId;
40-
41-
/**
42-
* @var bool
43-
*/
44-
private $encode;
45-
46-
/**
47-
* @var bool
48-
*/
49-
private $returnInt64AsObject;
50-
5136
/**
5237
* Create an Entity Mapper
5338
*
@@ -58,14 +43,10 @@ class EntityMapper
5843
* platform compatibility.
5944
*/
6045
public function __construct(
61-
$projectId,
62-
$encode,
63-
$returnInt64AsObject
64-
) {
65-
$this->projectId = $projectId;
66-
$this->encode = $encode;
67-
$this->returnInt64AsObject = $returnInt64AsObject;
68-
}
46+
private string $projectId,
47+
private bool $encode,
48+
private bool $returnInt64AsObject
49+
) {}
6950

7051
/**
7152
* Convert an entity response to properties, excludes and meanings.

Datastore/src/Operation.php

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,6 @@ class Operation
6565
use TimestampTrait;
6666
use ApiHelperTrait;
6767

68-
/**
69-
* @var DatastoreClient
70-
* @internal
71-
*/
72-
protected DatastoreClient $gapicClient;
73-
74-
/**
75-
* @var string
76-
*/
77-
private $projectId;
78-
79-
/**
80-
* @var string
81-
*/
82-
private $namespaceId;
83-
84-
/**
85-
* @var string
86-
*/
87-
private $databaseId;
88-
89-
/**
90-
* @var EntityMapper
91-
*/
92-
private $entityMapper;
93-
9468
/**
9569
* @var Serializer
9670
*/
@@ -111,17 +85,12 @@ class Operation
11185
* @param string $databaseId ID of the database to which the entities belong.
11286
*/
11387
public function __construct(
114-
DatastoreClient $gapicClient,
115-
$projectId,
116-
$namespaceId,
117-
EntityMapper $entityMapper,
118-
$databaseId = ''
88+
private DatastoreClient $gapicClient,
89+
private string $projectId,
90+
private ?string $namespaceId,
91+
private EntityMapper $entityMapper,
92+
private ?string $databaseId = null
11993
) {
120-
$this->gapicClient = $gapicClient;
121-
$this->projectId = $projectId;
122-
$this->namespaceId = $namespaceId;
123-
$this->databaseId = $databaseId;
124-
$this->entityMapper = $entityMapper;
12594
$this->serializer = new Serializer();
12695
$this->optionsValidator = new OptionsValidator($this->serializer);
12796
}
@@ -303,7 +272,7 @@ public function entity($key = null, array $entity = [], array $options = [])
303272
*
304273
* string $databaseId The ID of the database against which to make the request.
305274
* }
306-
* @return string
275+
* @return string The base64-encoded transaction ID.
307276
*/
308277
public function beginTransaction($transactionOptions, array $options = [])
309278
{
@@ -454,13 +423,8 @@ public function lookup(array $keys, array $options = [])
454423
$options['keys'] = $serviceKeys;
455424

456425
$readOptions = $this->createReadOptions($this->pluckArray(
457-
[
458-
'readConsistency',
459-
'transaction',
460-
'readTime'
461-
],
426+
['readConsistency', 'transaction', 'readTime'],
462427
$options,
463-
false
464428
));
465429

466430
/**
@@ -541,7 +505,7 @@ public function lookup(array $keys, array $options = [])
541505
*/
542506
public function runQuery(QueryInterface $query, array $options = [])
543507
{
544-
$className = $this->pluck('className', $options, false) ?? Entity::class;
508+
$className = $this->pluck('className', $options, false) ?? Entity::class;
545509
$options += [
546510
'namespaceId' => $this->namespaceId,
547511
'databaseId' => $this->databaseId,
@@ -586,11 +550,7 @@ public function runQuery(QueryInterface $query, array $options = [])
586550
}
587551

588552
$readOptions = $this->createReadOptions($this->pluckArray(
589-
[
590-
'readConsistency',
591-
'transaction',
592-
'readTime'
593-
],
553+
['readConsistency', 'transaction', 'readTime'],
594554
$options
595555
));
596556

@@ -671,7 +631,7 @@ function (array $entityResult) use ($className) {
671631
/**
672632
* Run an aggregation query and return aggregated results.
673633
*
674-
* @param AggregationQuery $query The Aggregation Query object.
634+
* @param AggregationQuery $runQueryObj The Aggregation Query object.
675635
* @param array $options [optional] {
676636
* Configuration Options
677637
*
@@ -699,11 +659,7 @@ public function runAggregationQuery(AggregationQuery $runQueryObj, array $option
699659
] + $runQueryObj->queryObject();
700660

701661
$pluckedReadOptions = $this->pluckArray(
702-
[
703-
'readConsistency',
704-
'transaction',
705-
'readTime'
706-
],
662+
['readConsistency', 'transaction', 'readTime'],
707663
$options
708664
);
709665

Datastore/src/Serializer.php

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -43,52 +43,57 @@ class Serializer extends ApiCoreSerializer
4343

4444
public function __construct()
4545
{
46-
parent::__construct(
47-
[
48-
'end_cursor' => function ($v) {
49-
return base64_encode($v);
50-
},
51-
'start_cursor' => function ($v) {
52-
return base64_encode($v);
53-
},
54-
'cursor' => function ($v) {
55-
return base64_encode($v);
56-
},
57-
],
58-
[
59-
'google.protobuf.Duration' => function ($v) {
60-
return $this->formatDurationFromApi($v);
46+
$fieldTransformers = [
47+
'end_cursor' => function ($v) {
48+
return base64_encode($v);
49+
},
50+
'start_cursor' => function ($v) {
51+
return base64_encode($v);
52+
},
53+
'cursor' => function ($v) {
54+
return base64_encode($v);
55+
},
56+
];
57+
$messageTypeTransformers = [
58+
'google.protobuf.Duration' => function ($v) {
59+
return $this->formatDurationFromApi($v);
60+
}
61+
];
62+
$decodeFieldTransformers = [
63+
'transaction' => function ($v) {
64+
return base64_decode($v);
65+
},
66+
'previous_transaction' => function ($v) {
67+
return base64_decode($v);
68+
},
69+
'end_cursor' => function ($v) {
70+
return base64_decode($v);
71+
},
72+
'start_cursor' => function ($v) {
73+
return base64_decode($v);
74+
},
75+
'cursor' => function ($v) {
76+
return base64_decode($v);
77+
},
78+
'timestamp_value' => function ($v) {
79+
return $this->formatTimestampForApi($v);
80+
},
81+
];
82+
$decodeMessageTypeTransformers = [
83+
'google.protobuf.Timestamp' => function ($v) {
84+
if ($v instanceof Timestamp) {
85+
return $v->formatForApi();
6186
}
62-
],
63-
[
64-
'transaction' => function ($v) {
65-
return base64_decode($v);
66-
},
67-
'previous_transaction' => function ($v) {
68-
return base64_decode($v);
69-
},
70-
'end_cursor' => function ($v) {
71-
return base64_decode($v);
72-
},
73-
'start_cursor' => function ($v) {
74-
return base64_decode($v);
75-
},
76-
'cursor' => function ($v) {
77-
return base64_decode($v);
78-
},
79-
'timestamp_value' => function ($v) {
80-
return $this->formatTimestampForApi($v);
81-
},
82-
],
83-
[
84-
'google.protobuf.Timestamp' => function ($v) {
85-
if ($v instanceof Timestamp) {
86-
return $v->formatForApi();
87-
}
8887

89-
return $v;
90-
}
91-
]
88+
return $v;
89+
}
90+
];
91+
92+
parent::__construct(
93+
$fieldTransformers,
94+
$messageTypeTransformers,
95+
$decodeFieldTransformers,
96+
$decodeMessageTypeTransformers
9297
);
9398
}
9499

Datastore/src/TransactionTrait.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,6 @@
2626
*/
2727
trait TransactionTrait
2828
{
29-
/**
30-
* @var Operation
31-
*/
32-
private $operation;
33-
34-
/**
35-
* @var string
36-
*/
37-
private $projectId;
38-
39-
/**
40-
* @var string
41-
*/
42-
private $transactionId;
43-
4429
/**
4530
* Create a Transaction
4631
*
@@ -49,14 +34,10 @@ trait TransactionTrait
4934
* @param string $transactionId The transaction to run mutations in.
5035
*/
5136
public function __construct(
52-
Operation $operation,
53-
$projectId,
54-
$transactionId
55-
) {
56-
$this->operation = $operation;
57-
$this->projectId = $projectId;
58-
$this->transactionId = $transactionId;
59-
}
37+
private Operation $operation,
38+
private string $projectId,
39+
private string $transactionId
40+
) { }
6041

6142
/**
6243
* Retrieve an entity from the datastore inside a transaction

0 commit comments

Comments
 (0)