| 
									
										
										
										
											2022-01-05 01:31:57 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-30 00:40:11 +08:00
										 |  |  | namespace BookStack\Sorting; | 
					
						
							| 
									
										
										
										
											2022-01-05 01:31:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class BookSortMap | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var BookSortMapItem[] | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $mapData = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function addItem(BookSortMapItem $mapItem): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->mapData[] = $mapItem; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @return BookSortMapItem[] | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function all(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->mapData; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function fromJson(string $json): self | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-01-07 21:04:49 +08:00
										 |  |  |         $map = new BookSortMap(); | 
					
						
							| 
									
										
										
										
											2022-01-05 01:31:57 +08:00
										 |  |  |         $mapData = json_decode($json); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($mapData as $mapDataItem) { | 
					
						
							|  |  |  |             $item = new BookSortMapItem( | 
					
						
							|  |  |  |                 intval($mapDataItem->id), | 
					
						
							|  |  |  |                 intval($mapDataItem->sort), | 
					
						
							|  |  |  |                 $mapDataItem->parentChapter ? intval($mapDataItem->parentChapter) : null, | 
					
						
							|  |  |  |                 $mapDataItem->type, | 
					
						
							|  |  |  |                 intval($mapDataItem->book) | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $map->addItem($item); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $map; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-06 20:18:11 +08:00
										 |  |  | } |