|
2 | 2 |
|
3 | 3 | namespace FiveamCode\LaravelNotionApi\Entities; |
4 | 4 |
|
| 5 | +use DateTime; |
| 6 | +use Carbon\Carbon; |
5 | 7 | use FiveamCode\LaravelNotionApi\Exceptions\WrapperException; |
6 | 8 | use FiveamCode\LaravelNotionApi\Notion; |
| 9 | +use Illuminate\Support\Arr; |
| 10 | + |
7 | 11 |
|
8 | 12 | class Database extends Entity |
9 | 13 | { |
| 14 | + private string $title = ""; |
| 15 | + private array $rawTitle = []; |
| 16 | + private array $rawProperties = []; |
| 17 | + private DateTime $createdTime; |
| 18 | + private DateTime $lastEditedTime; |
| 19 | + |
10 | 20 | protected function setRaw(array $raw): void |
11 | 21 | { |
12 | 22 | parent::setRaw($raw); |
13 | | - if ($raw['object'] != 'database') throw WrapperException::instance("invalid json-array: the given object is not a database"); |
| 23 | + if ($raw['object'] !== 'database') throw WrapperException::instance("invalid json-array: the given object is not a database"); |
| 24 | + |
| 25 | + if (Arr::exists($raw, 'title') && is_array($raw['title'])) { |
| 26 | + $this->title = Arr::first($raw['title'], null, ['plain_text' => ''])['plain_text']; |
| 27 | + $this->rawTitle = $raw['title']; |
| 28 | + } |
| 29 | + |
| 30 | + if (Arr::exists($raw, 'properties')) { |
| 31 | + $this->rawProperties = $raw['properties']; |
| 32 | + } |
| 33 | + |
| 34 | + if (Arr::exists($raw, 'created_time')) { |
| 35 | + $this->createdTime = new Carbon($raw['created_time']); |
| 36 | + } |
| 37 | + |
| 38 | + if (Arr::exists($raw, 'last_edited_time')) { |
| 39 | + $this->lastEditedTime = new Carbon($raw['last_edited_time']); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public function getTitle(): string |
| 44 | + { |
| 45 | + return $this->title; |
| 46 | + } |
| 47 | + |
| 48 | + public function getProperties() |
| 49 | + { |
| 50 | + //TODO: return collection of property-entities (id, type, title) |
| 51 | + throw new \Exception("not implemented yet"); |
| 52 | + } |
| 53 | + |
| 54 | + public function getRawTitle(): array |
| 55 | + { |
| 56 | + return $this->rawTitle; |
| 57 | + } |
| 58 | + |
| 59 | + public function getRawProperties(): array |
| 60 | + { |
| 61 | + return $this->rawProperties; |
| 62 | + } |
| 63 | + |
| 64 | + public function getPropertyNames(): array |
| 65 | + { |
| 66 | + return array_keys($this->rawProperties); |
| 67 | + } |
| 68 | + |
| 69 | + public function getCreatedTime(): DateTime |
| 70 | + { |
| 71 | + return $this->createdTime; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + public function getLastEditedTime(): DateTime |
| 76 | + { |
| 77 | + return $this->lastEditedTime; |
14 | 78 | } |
15 | 79 | } |
0 commit comments