|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2020 Google LLC. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +namespace Google\CloudFunctions; |
| 20 | + |
| 21 | +use JsonSerializable; |
| 22 | + |
| 23 | +class CloudEvent implements JsonSerializable |
| 24 | +{ |
| 25 | + // Required Fields |
| 26 | + private $id; |
| 27 | + private $source; |
| 28 | + private $specversion; |
| 29 | + private $type; |
| 30 | + |
| 31 | + // Optional Fields |
| 32 | + private $datacontenttype; |
| 33 | + private $dataschema; |
| 34 | + private $subject; |
| 35 | + private $time; |
| 36 | + /** |
| 37 | + * @var mixed |
| 38 | + */ |
| 39 | + private $data; |
| 40 | + |
| 41 | + final public function __construct( |
| 42 | + string $id, |
| 43 | + string $source, |
| 44 | + string $specversion, |
| 45 | + string $type, |
| 46 | + ?string $datacontenttype, |
| 47 | + ?string $dataschema, |
| 48 | + ?string $subject, |
| 49 | + ?string $time, |
| 50 | + $data |
| 51 | + ) { |
| 52 | + $this->id = $id; |
| 53 | + $this->source = $source; |
| 54 | + $this->specversion = $specversion; |
| 55 | + $this->type = $type; |
| 56 | + $this->datacontenttype = $datacontenttype; |
| 57 | + $this->dataschema = $dataschema; |
| 58 | + $this->subject = $subject; |
| 59 | + $this->time = $time; |
| 60 | + $this->data = $data; |
| 61 | + } |
| 62 | + |
| 63 | + public function getId(): string |
| 64 | + { |
| 65 | + return $this->id; |
| 66 | + } |
| 67 | + |
| 68 | + public function getSource(): string |
| 69 | + { |
| 70 | + return $this->source; |
| 71 | + } |
| 72 | + |
| 73 | + public function getSpecVersion(): string |
| 74 | + { |
| 75 | + return $this->specversion; |
| 76 | + } |
| 77 | + |
| 78 | + public function getType(): string |
| 79 | + { |
| 80 | + return $this->type; |
| 81 | + } |
| 82 | + |
| 83 | + public function getDataContentType(): ?string |
| 84 | + { |
| 85 | + return $this->datacontenttype; |
| 86 | + } |
| 87 | + |
| 88 | + public function getDataSchema(): ?string |
| 89 | + { |
| 90 | + return $this->dataschema; |
| 91 | + } |
| 92 | + |
| 93 | + public function getSubject(): ?string |
| 94 | + { |
| 95 | + return $this->subject; |
| 96 | + } |
| 97 | + |
| 98 | + public function getTime(): ?string |
| 99 | + { |
| 100 | + return $this->time; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @return mixed |
| 105 | + */ |
| 106 | + public function getData() |
| 107 | + { |
| 108 | + return $this->data; |
| 109 | + } |
| 110 | + |
| 111 | + public static function fromArray(array $arr) |
| 112 | + { |
| 113 | + $args = []; |
| 114 | + $argKeys = [ |
| 115 | + 'id', |
| 116 | + 'source', |
| 117 | + 'specversion', |
| 118 | + 'type', |
| 119 | + 'datacontenttype', |
| 120 | + 'dataschema', |
| 121 | + 'subject', |
| 122 | + 'time', |
| 123 | + 'data', |
| 124 | + ]; |
| 125 | + |
| 126 | + foreach ($argKeys as $key) { |
| 127 | + $args[] = $arr[$key] ?? null; |
| 128 | + } |
| 129 | + |
| 130 | + return new static(...$args); |
| 131 | + } |
| 132 | + |
| 133 | + public function jsonSerialize() |
| 134 | + { |
| 135 | + return [ |
| 136 | + 'id' => $this->id, |
| 137 | + 'source' => $this->source, |
| 138 | + 'specversion' => $this->specversion, |
| 139 | + 'type' => $this->type, |
| 140 | + 'datacontenttype' => $this->datacontenttype, |
| 141 | + 'dataschema' => $this->dataschema, |
| 142 | + 'subject' => $this->subject, |
| 143 | + 'time' => $this->time, |
| 144 | + 'data' => $this->data, |
| 145 | + ]; |
| 146 | + } |
| 147 | + |
| 148 | + public function __toString() |
| 149 | + { |
| 150 | + $output = implode("\n", [ |
| 151 | + 'CLOUDEVENT metadata:', |
| 152 | + "- id: $this->id", |
| 153 | + "- source: $this->source", |
| 154 | + "- specversion: $this->specversion", |
| 155 | + "- type: $this->type", |
| 156 | + "- datacontenttype: $this->datacontenttype", |
| 157 | + "- dataschema: $this->dataschema", |
| 158 | + "- subject: $this->subject", |
| 159 | + "- time: $this->time", |
| 160 | + ]); |
| 161 | + |
| 162 | + return $output; |
| 163 | + } |
| 164 | +} |
0 commit comments