|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace FiveamCode\LaravelNotionApi\Entities\Properties; |
| 4 | + |
| 5 | +use DateTime; |
| 6 | +use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate; |
| 7 | +use Illuminate\Support\Collection; |
| 8 | + |
| 9 | +/** |
| 10 | + * Class Rollup |
| 11 | + * @package FiveamCode\LaravelNotionApi\Entities\Properties |
| 12 | + */ |
| 13 | +class Rollup extends Property |
| 14 | +{ |
| 15 | + protected string $rollupType; |
| 16 | + |
| 17 | + /** |
| 18 | + * |
| 19 | + */ |
| 20 | + protected function fillFromRaw(): void |
| 21 | + { |
| 22 | + parent::fillFromRaw(); |
| 23 | + |
| 24 | + $this->rollupType = $this->rawContent['type']; |
| 25 | + |
| 26 | + if ($this->rollupType == 'number') { |
| 27 | + $this->content = $this->rawContent[$this->rollupType]; |
| 28 | + } else if ($this->rollupType == 'date') { |
| 29 | + $this->content = new RichDate(); |
| 30 | + if (isset($this->rawContent[$this->rollupType]['start'])) $this->content->setStart(new DateTime($this->rawContent[$this->rollupType]['start'])); |
| 31 | + if (isset($this->rawContent[$this->rollupType]['end'])) $this->content->setEnd(new DateTime($this->rawContent[$this->rollupType]['end'])); |
| 32 | + } else if ($this->rollupType == 'array') { |
| 33 | + $this->content = new Collection(); |
| 34 | + foreach ($this->rawContent[$this->rollupType] as $rollupPropertyItem) { |
| 35 | + $rollupPropertyItem['id'] = 'undefined'; |
| 36 | + $this->content->add(Property::fromResponse("", $rollupPropertyItem)); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @return mixed |
| 43 | + */ |
| 44 | + public function getContent() |
| 45 | + { |
| 46 | + return $this->content; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return string |
| 51 | + */ |
| 52 | + public function getRollupType(): string |
| 53 | + { |
| 54 | + return $this->rollupType; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return string |
| 59 | + */ |
| 60 | + public function getRollupContentType(): string |
| 61 | + { |
| 62 | + if($this->getContent() instanceof Collection){ |
| 63 | + $firstItem = $this->getContent()->first(); |
| 64 | + if($firstItem == null) return "null"; |
| 65 | + return $firstItem->getType(); |
| 66 | + } |
| 67 | + else{ |
| 68 | + return $this->getRollupType(); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments