Skip to content

Commit 1832157

Browse files
authored
Merge branch 'master' into master
2 parents 08969c1 + 56329e3 commit 1832157

File tree

20 files changed

+547
-11
lines changed

20 files changed

+547
-11
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
staticanalysis:
1010
runs-on: ubuntu-latest
1111
steps:
12-
- uses: actions/checkout@v2
12+
- uses: actions/checkout@v3
1313
- name: Install PHP
1414
uses: shivammathur/setup-php@v2
1515
with:

analyticsdata/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"google/analytics-data": "^0.7.0"
3+
"google/analytics-data": "^0.8.0"
44
}
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/analytics-data": "^0.7.0",
3+
"google/analytics-data": "^0.8.0",
44
"ext-bcmath": "*"
55
}
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"google/cloud-error-reporting": "^0.18.0"
3+
"google/cloud-error-reporting": "^0.19.0"
44
}
55
}

dialogflow/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-dialogflow": "^0.23",
3+
"google/cloud-dialogflow": "^0.25",
44
"symfony/console": "^5.0"
55
},
66
"autoload": {

error_reporting/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"google/cloud-error-reporting": "^0.18.0"
3+
"google/cloud-error-reporting": "^0.19.0"
44
}
55
}

firestore/src/City.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/firestore/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Firestore;
25+
26+
# [START firestore_data_custom_type_definition]
27+
class City
28+
{
29+
/* var string */
30+
public $name;
31+
/* var string */
32+
public $state;
33+
/* var string */
34+
public $country;
35+
/* var bool */
36+
public $capital;
37+
/* var int */
38+
public $population;
39+
/* var array */
40+
public $regions;
41+
42+
public function __construct(
43+
string $name,
44+
string $state,
45+
string $country,
46+
bool $capital = false,
47+
int $population = 0,
48+
array $regions = []
49+
) {
50+
$this->name = $name;
51+
$this->state = $state;
52+
$this->country = $country;
53+
$this->capital = $capital;
54+
$this->population = $population;
55+
$this->regions = $regions;
56+
}
57+
58+
public static function fromArray(array $source): City
59+
{
60+
// implementation of fromArray is excluded for brevity
61+
# [START_EXCLUDE]
62+
$city = new City(
63+
$source['name'],
64+
$source['state'],
65+
$source['country'],
66+
$source['capital'] ?? false,
67+
$source['population'] ?? 0,
68+
$source['regions'] ?? []
69+
);
70+
71+
return $city;
72+
# [END_EXCLUDE]
73+
}
74+
75+
public function toArray(): array
76+
{
77+
// implementation of toArray is excluded for brevity
78+
# [START_EXCLUDE]
79+
$dest = [
80+
'name' => $this->name,
81+
'state' => $this->state,
82+
'country' => $this->country,
83+
'capital' => $this->capital,
84+
'population' => $this->population,
85+
'regions' => $this->regions,
86+
];
87+
88+
return $dest;
89+
# [END_EXCLUDE]
90+
}
91+
92+
public function __toString()
93+
{
94+
// implementation of __toString is excluded for brevity
95+
# [START_EXCLUDE]
96+
return sprintf(
97+
<<<EOF
98+
Custom Type data(
99+
[name] => %s,
100+
[state] => %s,
101+
[country] => %s,
102+
[capital] => %s,
103+
[population] => %s,
104+
[regions] => %s
105+
)
106+
EOF,
107+
$this->name,
108+
$this->state,
109+
$this->country,
110+
$this->capital ? 'true' : 'false',
111+
$this->population,
112+
implode(', ', $this->regions)
113+
);
114+
# [END_EXCLUDE]
115+
}
116+
}
117+
118+
# [END firestore_data_custom_type_definition]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/firestore/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Firestore;
25+
26+
use Google\Cloud\Firestore\FirestoreClient;
27+
28+
include 'City.php';
29+
30+
/**
31+
* Get a single class object.
32+
*
33+
* @param string $projectId The Google Cloud Project ID
34+
*/
35+
function data_get_as_custom_type(string $projectId): void
36+
{
37+
// Create the Cloud Firestore client
38+
$db = new FirestoreClient([
39+
'projectId' => $projectId,
40+
]);
41+
# [START firestore_data_get_as_custom_type]
42+
$docRef = $db->collection('samples/php/cities')->document('SF');
43+
$snapshot = $docRef->snapshot();
44+
$city = City::fromArray($snapshot->data());
45+
46+
if ($snapshot->exists()) {
47+
printf('Document data:' . PHP_EOL);
48+
print((string) $city);
49+
} else {
50+
printf('Document %s does not exist!' . PHP_EOL, $snapshot->id());
51+
}
52+
# [END firestore_data_get_as_custom_type]
53+
}
54+
55+
// The following 2 lines are only needed to run the samples
56+
require_once __DIR__ . '/../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

firestore/test/firestoreTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,22 @@ public function testRetrieveCreateExamples()
381381
$this->assertStringContainsString('Added example cities data to the cities collection.', $output);
382382
}
383383

384+
/**
385+
* @depends testRetrieveCreateExamples
386+
*/
387+
public function testGetCustomType()
388+
{
389+
$output = $this->runFirestoreSnippet('data_get_as_custom_type');
390+
$this->assertStringContainsString('Document data:', $output);
391+
$this->assertStringContainsString('Custom Type data', $output);
392+
$this->assertStringContainsString('[name] => San Francisco', $output);
393+
$this->assertStringContainsString('[state] => CA', $output);
394+
$this->assertStringContainsString('[country] => USA', $output);
395+
$this->assertStringContainsString('[capital] => false', $output);
396+
$this->assertStringContainsString('[population] => 860000', $output);
397+
$this->assertStringContainsString('[regions] =>', $output);
398+
}
399+
384400
/**
385401
* @depends testRetrieveCreateExamples
386402
*/
@@ -692,7 +708,7 @@ private static function runFirestoreSnippet($snippetName, array $args = null)
692708
{
693709
if ($args === null) {
694710
$args = [
695-
self::$firestoreProjectId
711+
self::$firestoreProjectId,
696712
];
697713
}
698714

File renamed without changes.

0 commit comments

Comments
 (0)