*/ public function dump( ProductCardViewModel|FallbackCardViewModel $viewModel ): array { if ( $viewModel instanceof ProductCardViewModel ) { return $this->dumpProductCard( $viewModel ); } return $this->dumpFallbackCard( $viewModel ); } /** * Produce the §8.2 ProductCardViewModel array shape. * * @param ProductCardViewModel $vm Source view model. * * @return array */ private function dumpProductCard( ProductCardViewModel $vm ): array { $product = $vm->product(); $price = $vm->price(); $avail = $vm->availability(); $meta = $vm->meta(); return array( 'type' => $vm->type(), 'template' => $vm->template(), 'product' => array( 'id' => $product->id(), 'provider' => $product->provider(), 'provider_label' => $product->providerLabel(), 'provider_item_id' => $product->providerItemId(), 'title' => $product->title(), 'brand' => $product->brand(), 'description' => $product->description(), 'detail_url' => $product->detailUrl(), 'affiliate_url' => $product->affiliateUrl(), ), 'images' => $this->dumpImageSet( $vm->images() ), 'price' => array( 'visible' => $price->visible(), 'label' => $price->label(), 'amount' => $price->amount(), 'currency' => $price->currency(), 'observed_at_label' => $price->observedAtLabel(), 'disclaimer_required' => $price->disclaimerRequired(), ), 'availability' => array( 'visible' => $avail->visible(), 'status' => $avail->status(), 'label' => $avail->label(), ), 'buttons' => $this->dumpButtons( $vm->buttons() ), 'badges' => $this->dumpBadges( $vm->badges() ), 'disclaimers' => array_values( $vm->disclaimers() ), 'meta' => array( 'show_brand' => $meta->showBrand(), 'show_description' => $meta->showDescription(), 'show_price' => $meta->showPrice(), 'show_provider_badge' => $meta->showProviderBadge(), ), 'classes' => $this->dumpClasses( $vm->classes() ), 'is_fallback' => $vm->isFallback(), ); } /** * Produce the §8.4 FallbackCardViewModel array shape. * * @param FallbackCardViewModel $vm Source view model. * * @return array */ private function dumpFallbackCard( FallbackCardViewModel $vm ): array { $primary = $vm->primaryButton(); return array( 'type' => $vm->type(), 'template' => $vm->template(), 'reason' => $vm->reason(), 'title' => $vm->title(), 'primary_button' => $primary === null ? null : $this->dumpButton( $primary ), 'note' => $vm->note(), 'classes' => $this->dumpClasses( $vm->classes() ), 'is_fallback' => $vm->isFallback(), ); } /** * Produce the §8.2 `images` array shape. * * @param ImageSet $images Source image set. * * @return array{primary: array|null, gallery: list>} */ private function dumpImageSet( ImageSet $images ): array { $primary = $images->primary(); $gallery = array(); foreach ( $images->gallery() as $image ) { $gallery[] = $this->dumpImage( $image ); } return array( 'primary' => $primary === null ? null : $this->dumpImage( $primary ), 'gallery' => $gallery, ); } /** * Produce the §8.2 single-image array shape. * * @param ImageView $image Source image view. * * @return array{url: string, alt: string, width: int|null, height: int|null, loading: string} */ private function dumpImage( ImageView $image ): array { return array( 'url' => $image->url(), 'alt' => $image->alt(), 'width' => $image->width(), 'height' => $image->height(), 'loading' => $image->loading(), ); } /** * Produce the §8.2 buttons-list array shape. * * @param list $buttons Source button list. * * @return list> */ private function dumpButtons( array $buttons ): array { $result = array(); foreach ( $buttons as $button ) { $result[] = $this->dumpButton( $button ); } return $result; } /** * Produce the §8.2 single-button array shape. * * @param ButtonView $button Source button view. * * @return array */ private function dumpButton( ButtonView $button ): array { return array( 'provider' => $button->provider(), 'label' => $button->label(), 'url' => $button->url(), 'target' => $button->target(), 'rel' => $button->rel(), 'class_name' => $button->className(), 'aria_label' => $button->ariaLabel(), ); } /** * Produce the §8.2 badges-list array shape. * * @param list $badges Source badge list. * * @return list> */ private function dumpBadges( array $badges ): array { $result = array(); foreach ( $badges as $badge ) { $result[] = array( 'kind' => $badge->kind(), 'label' => $badge->label(), 'class_name' => $badge->className(), ); } return $result; } /** * Produce the §8.2 / §8.4 `classes` array shape. * * @param CssClasses $classes Source classes view. * * @return array{root: string, image: string, body: string, title: string, price: string, buttons: string} */ private function dumpClasses( CssClasses $classes ): array { return array( 'root' => $classes->root(), 'image' => $classes->image(), 'body' => $classes->body(), 'title' => $classes->title(), 'price' => $classes->price(), 'buttons' => $classes->buttons(), ); } }