1919
2020use Google \ApiCore \ArrayTrait ;
2121use Google \Cloud \Spanner \Session \SessionPoolInterface ;
22+ use Google \Cloud \Spanner \V1 \TransactionOptions ;
23+ use Google \Cloud \Spanner \V1 \TransactionOptions \PBReadOnly ;
2224use Google \Protobuf \Duration ;
2325
2426/**
@@ -39,10 +41,10 @@ trait TransactionConfigurationTrait
3941 * @see V1\TransactionSelector
4042 *
4143 * @param array $options call options.
42- * @param array $previousReadOnlyOptions Previously given call options (for single-use snapshots).
44+ * @param PBReadOnly $previousReadOnlyOptions Previously given call options (for single-use snapshots).
4345 * @return array [(array) transaction selector, (string) context]
4446 */
45- private function transactionSelector (array &$ options , array $ previousReadOnlyOptions = [] ): array
47+ private function transactionSelector (array &$ options , ? PBReadOnly $ previousReadOnlyOptions = null ): array
4648 {
4749 $ options += [
4850 'begin ' => false ,
@@ -74,15 +76,15 @@ private function transactionSelector(array &$options, array $previousReadOnlyOpt
7476 *
7577 * @param array $options call options
7678 *
77- * @param array $previousReadOnlyOptions Previously given call options (for single-use snapshots).
79+ * @param PBReadOnly $previousReadOnlyOptions Previously given call options (for single-use snapshots).
7880 * @return array [(array) transaction options, (string) transaction type, (string) context]
7981 */
80- private function transactionOptions (array &$ options , array $ previousReadOnlyOptions = [] ): array
82+ private function transactionOptions (array &$ options , ? PBReadOnly $ previousReadOnlyOptions = null ): array
8183 {
8284 // @TODO: Remove $options being passed by reference
8385
8486 $ type = null ;
85- $ begin = $ options ['begin ' ] ?? false ;
87+ $ begin = $ options ['begin ' ] ?? [] ;
8688 $ context = $ options ['transactionType ' ] ?? SessionPoolInterface::CONTEXT_READWRITE ;
8789 $ id = $ options ['transactionId ' ] ?? null ;
8890
@@ -103,7 +105,8 @@ private function transactionOptions(array &$options, array $previousReadOnlyOpti
103105 $ transactionOptions = $ this ->configureReadOnlyTransactionOptions ($ options , $ previousReadOnlyOptions );
104106 } elseif ($ context === SessionPoolInterface::CONTEXT_READWRITE ) {
105107 $ transactionOptions = $ this ->configureReadWriteTransactionOptions (
106- $ type == 'begin ' && is_array ($ begin ) ? $ begin : []
108+ // TODO: Find out when $begin is a bool and fix it
109+ $ type == 'begin ' && !is_bool ($ begin ) ? $ begin : []
107110 );
108111 } else {
109112 throw new \BadMethodCallException (sprintf (
@@ -140,11 +143,14 @@ private function initReadWriteTransactionOptions(): array
140143 return ['readWrite ' => []];
141144 }
142145
143- private function configureReadWriteTransactionOptions (array $ options = [] ): array
146+ private function configureReadWriteTransactionOptions (array | TransactionOptions $ options ): array
144147 {
145- return array_intersect_key ($ options , array_flip ([
146- 'excludeTxnFromChangeStreams ' ,
147- ])) + $ this ->initReadWriteTransactionOptions ();
148+ $ excludeTxn = $ options instanceof TransactionOptions
149+ ? $ options ->getExcludeTxnFromChangeStreams ()
150+ : $ options ['excludeTxnFromChangeStreams ' ] ?? null ;
151+ return array_filter ([
152+ 'excludeTxnFromChangeStreams ' => $ excludeTxn ,
153+ ]) + $ this ->initReadWriteTransactionOptions ();
148154 }
149155
150156 /**
@@ -180,10 +186,10 @@ private function configureReadWriteTransactionOptions(array $options = []): arra
180186 * "single-use", and may be used for only a single operation.
181187 * **Defaults to** `false`.
182188 * }
183- * @param array $previousReadOnlyOptions Previously given call options (for single-use snapshots).
189+ * @param PBReadOnly $previousReadOnlyOptions Previously given call options (for single-use snapshots).
184190 * @return array
185191 */
186- private function configureReadOnlyTransactionOptions (array $ options , array $ previousReadOnlyOptions = [] ): array
192+ private function configureReadOnlyTransactionOptions (array $ options , ? PBReadOnly $ previousReadOnlyOptions = null ): array
187193 {
188194 // select only the PBReadOnly fields from $options
189195 $ readOnly = array_intersect_key ($ options , array_flip ([
@@ -216,7 +222,8 @@ private function configureReadOnlyTransactionOptions(array $options, array $prev
216222 );
217223 }
218224
219- $ readOnly += $ previousReadOnlyOptions ;
225+ // Removing this for now - not sure why it's here
226+ // $readOnly += $previousReadOnlyOptions;
220227
221228 if (empty ($ readOnly )) {
222229 $ readOnly ['strong ' ] = true ;
0 commit comments