HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Checkout/Resources/CheckoutInformationResource.php
<?php

namespace App\Checkout\Resources;

use App\Addresses\AddressResource;
use App\Base\FormatsValuesTrait;
use App\Cart\Resources\VatRateTotalResource;
use App\ShippingCosts\Resources\ShippingCostsResource;
use App\Users\SiteUserInterface;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Collection;
use Komma\KMS\Globalization\RegionInfoInterface;

/**
 * Class CheckoutInformationResource
 *
 *
 * @property-write RegionInfoInterface   $regionInfo
 * @property-write int                   $subtotalEx
 * @property-write int                   $subtotalWithDiscountsEx
 * @property-write ShippingCostsResource $shippingCosts
 * @property-write int                   $shippingCostsVatAmount
 * @property-write int                   $subtotalInc
 * @property-write int                   $subtotalWithDiscountsInc
 * @property-write array                 $couponCodes
 * @property-write Collection            $vatRateTotals
 * @property-write array                 $toFormat
 * @property-write JsonResource      $shoppingCartItems
 * @property-write int               $totalItemsCount
 * @property-write string            $cartRoute
 * @property-write string            $startPaymentRoute
 * @property-write string            $startCheckoutRoute
 * @property-write string            $continueShoppingRoute
 * @property-write string            $editDetailsRoute
 * @property-write AddressResource   $shippingAddress
 * @property-write AddressResource   $invoiceAddress
 * @property-write SiteUserInterface $user
 * @property-write array             $discountPriceMutations
 * @package App\Checkout\Resources
 */
class CheckoutInformationResource implements Arrayable
{
    use FormatsValuesTrait;

    private RegionInfoInterface $regionInfo;
    private int $subtotalEx = 0;
    private int $subtotalWithDiscountsEx = 0;
    private ShippingCostsResource $shippingCosts;
    private int $shippingCostsVatAmount = 0;
    private int $subtotalInc = 0;
    private int $subtotalWithDiscountsInc = 0;
    private array $couponCodes = [];
    private Collection $vatRateTotals;
    private JsonResource $shoppingCartItems;
    private int $totalItemsCount = 0;
    private string $cartRoute = '';
    private string $startCheckoutRoute = '';
    private string $continueShoppingRoute = '';
    private string $editDetailsRoute = '';
    private string $startPaymentRoute;
    private ?AddressResource $shippingAddress;
    private ?AddressResource $invoiceAddress;
    private ?SiteUserInterface $user;
    private Collection $discountPriceMutations;

    public function __construct()
    {
        $this->regionInfo = app(RegionInfoInterface::class);
    }

    private array $fieldsToFormat = ['subtotalEx', 'subtotalInc', 'subtotalWithDiscountsInc', 'subtotalWithDiscountsEx'];

    /**
     * Transform the resource into an array.
     *
     * @return array
     */
    public function toArray()
    {
        $baseData = [
            'items' => $this->shoppingCartItems,
            'currencySymbol' => $this->regionInfo->getCurrencySymbol(),
            'totalItemsCount' => $this->totalItemsCount,
            'shippingCosts' => $this->shippingCosts,
            'shippingAddress' => $this->shippingAddress,
            'invoiceAddress' => $this->invoiceAddress,
            'shippingCostsVatAmount' => $this->shippingCostsVatAmount,
            'subtotalInc' => $this->subtotalInc,
            'subtotalWithDiscountsInc' => $this->subtotalWithDiscountsInc,
            'subtotalEx' => $this->subtotalEx,
            'subtotalWithDiscountsEx' => $this->subtotalWithDiscountsEx,
            'vatRateTotals' => VatRateTotalResource::collection($this->vatRateTotals),
            'couponCodes' => $this->couponCodes,
            'total' => $this->getTotalInc(),
            'totalFormatted' => $this->formatValue($this->getTotalInc()),
            'startPaymentRoute' => $this->startPaymentRoute,
            'cartRoute' => $this->cartRoute,
            'continueShoppingRoute' => $this->continueShoppingRoute,
            'editDetailsRoute' => $this->editDetailsRoute,
            'startCheckoutRoute' => $this->startCheckoutRoute,
            'discountPriceMutations' => $this->discountPriceMutations,
            'user' => $this->user
        ];

        return array_merge($baseData, $this->formattedValues($baseData));
    }


    /**
     * @return float
     */
    private function getTotalInc(): float {
        return $this->subtotalWithDiscountsInc + $this->shippingCosts->getPriceInc();
    }

    /**
     * @param $name
     * @param $value
     */
    public function __set($name, $value)
    {
        if(!property_exists($this, $name)) throw new \InvalidArgumentException('There is no property with name "'.$name.'" on '.self::class.'.');
        $this->$name = $value;
    }
}