44
55use DateTime ;
66use FiveamCode \LaravelNotionApi \Entities \PropertyItems \RichDate ;
7+ use FiveamCode \LaravelNotionApi \Exceptions \HandlingException ;
78use Illuminate \Support \Collection ;
9+ use Illuminate \Support \Str ;
810
911/**
1012 * Class Rollup
@@ -15,26 +17,26 @@ class Rollup extends Property
1517 protected string $ rollupType ;
1618
1719 /**
18- *
20+ * @throws HandlingException
1921 */
2022 protected function fillFromRaw (): void
2123 {
2224 parent ::fillFromRaw ();
2325
2426 $ this ->rollupType = $ this ->rawContent ['type ' ];
2527
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- }
28+ switch ($ this ->rollupType ) {
29+ case ' number ' :
30+ $ this ->setRollupContentNumber ();
31+ break ;
32+ case ' array ' :
33+ $ this ->setRollupContentArray ( );
34+ break ;
35+ case ' date ' :
36+ $ this ->setRollupContentDate ();
37+ break ;
38+ default :
39+ throw new HandlingException ( " Unexpected rollupType { $ this -> rollupType }" );
3840 }
3941 }
4042
@@ -55,17 +57,56 @@ public function getRollupType(): string
5557 }
5658
5759 /**
58- * @return string
60+ * @return string|null
5961 */
60- public function getRollupContentType (): string
62+ public function getRollupContentType (): ? string
6163 {
62- if ($ this ->getContent () instanceof Collection){
64+ if ($ this ->getContent () instanceof Collection) {
6365 $ firstItem = $ this ->getContent ()->first ();
64- if ($ firstItem == null ) return "null " ;
66+
67+ # if rollup is empty, there is no type
68+ if ($ firstItem == null ) return null ;
69+
6570 return $ firstItem ->getType ();
66- }
67- else {
71+ } else {
6872 return $ this ->getRollupType ();
6973 }
7074 }
75+
76+
77+ private function setRollupContentNumber ()
78+ {
79+ $ this ->content = $ this ->rawContent [$ this ->rollupType ];
80+ }
81+
82+ private function setRollupContentArray ()
83+ {
84+ $ this ->content = new Collection ();
85+
86+ foreach ($ this ->rawContent [$ this ->rollupType ] as $ rollupPropertyItem ) {
87+ // TODO
88+ $ rollupPropertyItem ['id ' ] = 'undefined ' ;
89+
90+ $ this ->content ->add (
91+ Property::fromResponse ("" , $ rollupPropertyItem )
92+ );
93+ }
94+ }
95+
96+ private function setRollupContentDate ()
97+ {
98+ $ this ->content = new RichDate ();
99+
100+ if (isset ($ this ->rawContent [$ this ->rollupType ]['start ' ])) {
101+ $ this ->content ->setStart (
102+ new DateTime ($ this ->rawContent [$ this ->rollupType ]['start ' ])
103+ );
104+ }
105+
106+ if (isset ($ this ->rawContent [$ this ->rollupType ]['end ' ])) {
107+ $ this ->content ->setEnd (
108+ new DateTime ($ this ->rawContent [$ this ->rollupType ]['end ' ])
109+ );
110+ }
111+ }
71112}
0 commit comments