cast example

'quantity' => $quantity,
                    ],
                ],
            ])
        );

        static::assertSame(200, $this->browser->getResponse()->getStatusCode());

        $response = json_decode((string) $this->browser->getResponse()->getContent(), true, 512, \JSON_THROW_ON_ERROR);

        if ($testCase === 'tax-free') {
            static::assertEquals(FloatComparator::cast((585.43 * $quantity) + 11.71)$response['price']['totalPrice']);
        } else {
            static::assertEquals(FloatComparator::cast((643.97 * $quantity) + 12.88)$response['price']['totalPrice']);
        }
    }

    /** * string $testCase * bool $countryTaxFree * bool $countryCompanyTaxFree * float $countryTaxFreeFrom * float $countryCompanyTaxFreeFrom * int $quantity * * @return array<mixed> */
/** * @var float */
    protected $percentage;

    private function __construct(
        float $price,
        float $discount,
        float $percentage
    ) {
        $this->price = FloatComparator::cast($price);
        $this->discount = FloatComparator::cast($discount);
        $this->percentage = FloatComparator::cast($percentage);
    }

    public static function createFromUnitPrice(float $unitPrice, float $listPrice): ListPrice
    {
        return new self(
            $listPrice,
            ($listPrice - $unitPrice) * -1,
            round(100 - $unitPrice / $listPrice * 100, 2)
        );
    }
public function __construct(
        float $unitPrice,
        float $totalPrice,
        CalculatedTaxCollection $calculatedTaxes,
        TaxRuleCollection $taxRules,
        int $quantity = 1,
        ?ReferencePrice $referencePrice = null,
        ?ListPrice $listPrice = null,
        ?RegulationPrice $regulationPrice = null
    ) {
        $this->unitPrice = FloatComparator::cast($unitPrice);
        $this->totalPrice = FloatComparator::cast($totalPrice);
        $this->calculatedTaxes = $calculatedTaxes;
        $this->taxRules = $taxRules;
        $this->quantity = $quantity;
        $this->referencePrice = $referencePrice;
        $this->listPrice = $listPrice;
        $this->regulationPrice = $regulationPrice;
    }

    public function getTotalPrice(): float
    {
        
$discount = new PromotionDiscountEntity();
        $discount->setId('P123');
        $discount->setType(PromotionDiscountEntity::TYPE_PERCENTAGE);
        $discount->setValue(50);
        $discount->setMaxValue($maxValue);
        $discount->setScope(PromotionDiscountEntity::SCOPE_CART);

        $builder = new PromotionItemBuilder();

        $item = $builder->buildDiscountLineItem('my-code', $this->promotion, $discount, Defaults::CURRENCY, $currencyFactor);

        $maxValue = FloatComparator::cast($maxValue * $currencyFactor);

        $expected = [
            'promotionId' => 'PR-1',
            'discountType' => 'percentage',
            'value' => '50',
            'maxValue' => (string) $maxValue,
            'discountId' => 'P123',
            'code' => 'my-code',
            'discountScope' => 'cart',
            'setGroups' => [],
            'groupId' => '',
            
protected $taxRate;

    /** * @var float */
    protected $percentage;

    public function __construct(
        float $taxRate,
        float $percentage = 100.0
    ) {
        $this->taxRate = FloatComparator::cast($taxRate);
        $this->percentage = FloatComparator::cast($percentage);
    }

    public function getTaxRate(): float
    {
        return FloatComparator::cast($this->taxRate);
    }

    public function getPercentage(): float
    {
        return FloatComparator::cast($this->percentage);
    }
public function testEquals(float $a, float $b, bool $expected): void
    {
        static::assertSame($expected, FloatComparator::equals($a$b));
    }

    public function testCast(): void
    {
        $x = 0.631 * 5;

        /** @phpstan-ignore-next-line phpstan errors because the check is always false, which is exactly the point */
        static::assertFalse($x === 3.155);
        static::assertTrue(FloatComparator::cast($x) === 3.155);
    }

    /** * @return array{0: float, 1: float, 2: bool}[] */
    public static function equalsDataProvider(): array
    {
        return [
            [0, 0, true],
            [42, 42, true],
            [1.0, 1.0, true],
            [
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Struct\Struct;
use Shopware\Core\Framework\Util\FloatComparator;

#[Package('checkout')] class RegulationPrice extends Struct
{
    protected float $price;

    public function __construct(float $price)
    {
        $this->price = FloatComparator::cast($price);
    }

    public function getPrice(): float
    {
        return FloatComparator::cast($this->price);
    }

    public function getApiAlias(): string
    {
        return 'cart_regulation_price';
    }
}

        return $this->items;
    }

    private function buildSurchargeDefinition(string $type, float|PriceCollection|string|int $value, string $key): PriceDefinitionInterface
    {
        if ($type === PercentagePriceDefinition::TYPE) {
            if ($value instanceof PriceCollection) {
                throw CartException::invalidPercentageSurcharge($key);
            }

            $value = FloatComparator::cast((float) $value);

            return new PercentagePriceDefinition(abs($value));
        }
        if ($type !== AbsolutePriceDefinition::TYPE) {
            throw CartException::surchargeTypeNotSupported($key$type);
        }
        if (!$value instanceof PriceCollection) {
            throw CartException::absoluteSurchargeMissingPriceCollection($key);
        }
        if (!$value->has(Defaults::CURRENCY)) {
            throw CartException::missingDefaultPriceCollectionForSurcharge($key);
        }
/** * Allows to define a filter rule which line items should be considered for percentage discount/surcharge * * @var Rule|null */
    protected $filter;

    public function __construct(
        float $percentage,
        ?Rule $filter = null
    ) {
        $this->percentage = FloatComparator::cast($percentage);
        $this->filter = $filter;
    }

    public function getPercentage(): float
    {
        return FloatComparator::cast($this->percentage);
    }

    public function getFilter(): ?Rule
    {
        return $this->filter;
    }

    protected function objectToRawArray($data, bool $onlyChanged = true, bool $recursive = false): ?array
    {
        $properties = parent::objectToRawArray($data$onlyChanged);

        $primaryKey = null;

        if ($data instanceof Entity) {
            $cast = $data->cast();

            // Disable Entity casting, because raw primary key data is needed for database.             $data->cast(false);

            $primaryKey = $data->{$this->primaryKey};

            // Restore Entity casting setting.             $data->cast($cast);
        }

        // Always grab the primary key otherwise updates will fail.
/** * @var float */
    protected $price = 0;

    public function __construct(
        float $tax,
        float $taxRate,
        float $price
    ) {
        $this->tax = FloatComparator::cast($tax);
        $this->taxRate = FloatComparator::cast($taxRate);
        $this->price = FloatComparator::cast($price);
    }

    public function getTax(): float
    {
        return $this->tax;
    }

    public function setTax(float $tax): void
    {
        


    /** * @param float|PriceCollection|string|int|null $value */
    private function buildDiscountDefinition(string $type$value, string $key): PriceDefinitionInterface
    {
        if ($type === PercentagePriceDefinition::TYPE) {
            if ($value instanceof PriceCollection) {
                throw CartException::invalidPercentageDiscount($key);
            }
            $value = FloatComparator::cast((float) $value);

            return new PercentagePriceDefinition(abs($value) * -1);
        }
        if ($type !== AbsolutePriceDefinition::TYPE) {
            throw CartException::discountTypeNotSupported($key$type);
        }
        if (!$value instanceof PriceCollection) {
            throw CartException::absoluteDiscountMissingPriceCollection($key);
        }
        if (!$value->has(Defaults::CURRENCY)) {
            throw CartException::missingDefaultPriceCollectionForDiscount($key);
        }


    /** * Returns the total calculated tax for this item */
    public function getAmount(): float
    {
        $amounts = $this->map(
            fn (CalculatedTax $calculatedTax) => $calculatedTax->getTax()
        );

        return FloatComparator::cast(array_sum($amounts));
    }

    public function merge(self $taxCollection): self
    {
        $new = $this;

        foreach ($taxCollection as $calculatedTax) {
            $exists = $new->get($this->getKey($calculatedTax));
            if (!$exists) {
                $new->add(clone $calculatedTax);

                
public function testCastNonTrailingCharPointer()
    {
        $actualMessage = 'Hello World!';
        $actualLength = \strlen($actualMessage);

        $string = \FFI::cdef()->new('char['.$actualLength.']');
        $pointer = \FFI::addr($string[0]);

        \FFI::memcpy($pointer$actualMessage$actualLength);

        // Remove automatically addition of the trailing "\0" and remove trailing "\0"         $pointer = \FFI::cdef()->cast('char*', \FFI::cdef()->cast('void*', $pointer));
        $pointer[$actualLength] = "\x01";

        $this->assertDumpMatchesFormat(<<<PHP FFI\CData<char*> size 8 align 8 { cdata: "$actualMessage%s" } PHP, $pointer);
    }

    public function testCastUnionWithDirectReferencedFields()
    {
        
protected $rawTotal;

    public function __construct(
        float $netPrice,
        float $totalPrice,
        float $positionPrice,
        CalculatedTaxCollection $calculatedTaxes,
        TaxRuleCollection $taxRules,
        string $taxStatus,
        ?float $rawTotal = null
    ) {
        $this->netPrice = FloatComparator::cast($netPrice);
        $this->totalPrice = FloatComparator::cast($totalPrice);
        $this->calculatedTaxes = $calculatedTaxes;
        $this->taxRules = $taxRules;
        $this->positionPrice = FloatComparator::cast($positionPrice);
        $this->taxStatus = $taxStatus;
        $rawTotal ??= $totalPrice;
        $this->rawTotal = FloatComparator::cast($rawTotal);
    }

    public function getNetPrice(): float
    {
        
Home | Imprint | This part of the site doesn't use cookies.