|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Resend; |
| 4 | + |
| 5 | +use BadMethodCallException; |
| 6 | +use Resend\Contracts\Resource as ResourceContract; |
| 7 | +use Resend\Exceptions\MissingAttributeException; |
| 8 | + |
| 9 | +class Resource implements ResourceContract |
| 10 | +{ |
| 11 | + /** |
| 12 | + * The resources's attributes. |
| 13 | + */ |
| 14 | + protected $attributes = []; |
| 15 | + |
| 16 | + /** |
| 17 | + * Create a new Resource instance. |
| 18 | + */ |
| 19 | + public function __construct(array $attributes = []) |
| 20 | + { |
| 21 | + $this->fill($attributes); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Create a new resource from the given attributes. |
| 26 | + */ |
| 27 | + public static function from(array $attributes): static |
| 28 | + { |
| 29 | + return new static($attributes); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Fill the resource with an array of attributes. |
| 34 | + */ |
| 35 | + protected function fill(array $attributes): void |
| 36 | + { |
| 37 | + $fillable = $attributes; |
| 38 | + |
| 39 | + foreach ($fillable as $key => $value) { |
| 40 | + $this->attributes[$key] = $value; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Get an attribute by name. |
| 46 | + */ |
| 47 | + public function getAttribute($name) |
| 48 | + { |
| 49 | + if (! $name) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if (array_key_exists($name, $this->attributes)) { |
| 54 | + return $this->getAttributes()[$name] ?? null; |
| 55 | + } |
| 56 | + |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Get all attributes for the resource. |
| 62 | + */ |
| 63 | + public function getAttributes(): array |
| 64 | + { |
| 65 | + return $this->attributes; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Convert the resource instance into an array. |
| 70 | + */ |
| 71 | + public function toArray(): array |
| 72 | + { |
| 73 | + return $this->getAttributes(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Convert the resource instance into JSON. |
| 78 | + */ |
| 79 | + public function toJson(int $options = 0): string |
| 80 | + { |
| 81 | + $json = json_encode($this->jsonSerialize(), $options); |
| 82 | + |
| 83 | + return $json; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Convert the object into something JSON serializable. |
| 88 | + */ |
| 89 | + public function jsonSerialize(): mixed |
| 90 | + { |
| 91 | + return $this->toArray(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Dynamically retrieve attributes on the resource. |
| 96 | + */ |
| 97 | + public function __get($name) |
| 98 | + { |
| 99 | + return $this->getAttribute($name); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Get all the attributes when dumping the resource. |
| 104 | + */ |
| 105 | + public function __debugInfo(): array |
| 106 | + { |
| 107 | + return $this->getAttributes(); |
| 108 | + } |
| 109 | + |
| 110 | + public function offsetExists(mixed $offset): bool |
| 111 | + { |
| 112 | + try { |
| 113 | + return ! is_null($this->getAttribute($offset)); |
| 114 | + } catch (MissingAttributeException) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public function offsetGet(mixed $offset): mixed |
| 120 | + { |
| 121 | + return $this->getAttribute($offset); |
| 122 | + } |
| 123 | + |
| 124 | + public function offsetSet(mixed $offset, mixed $value): void |
| 125 | + { |
| 126 | + throw new BadMethodCallException('Cannot set resource attributes.'); |
| 127 | + } |
| 128 | + |
| 129 | + public function offsetUnset(mixed $offset): void |
| 130 | + { |
| 131 | + throw new BadMethodCallException('Cannot unset resource attributes.'); |
| 132 | + } |
| 133 | +} |
0 commit comments