|
| 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] |
0 commit comments