Skip to content

Validator from class #46

@jtojnar

Description

@jtojnar

The Expect::from is nice, but it does not really work for immutable objects like the following:

<?php

use Money\Money;

final class InvoiceItem {
	public function __construct(
		public readonly string $name,
		public readonly Money $price,
		public readonly int $amount,
	) {
	}
}
Version without readonly
<?php

use Money\Money;

final class InvoiceItem {
	public function __construct(
		private string $name,
		private Money $price,
		private int $amount,
	) {
	}

	public function getName(): string {
		return $this->name;
	}

	public function getPrice(): Money {
		return $this->price;
	}

	public function getAmount(): int {
		return $this->amount;
	}
}

It would be nice to have Expect::fromClass(InvoiceItem::class), that would check the constructor arguments instead of the public properties like Expect::from does.

I can try to implement this, if you think this is a good idea.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions