Skip to content

Why/how does the object mapper allow source array but not stdClass? #18

Answered by priyadi
ToshY asked this question in Q&A
Discussion options

You must be logged in to vote
$this->mapper->map(['finished' => true], BookDto::class);

The code works because there is a bug in the implementation that it accepts mixed, not object, unlike the interface. Liskov allows it, so static analysis did not catch the bug. I'll fix the bug later.

Internally, the engine can handle array to object mapping. It just forwards the job to Symfony Serializer. So your code can work.

PHPstan does not like it because it sees $this->mapper as a MapperInterface, not the concrete Mapper. But PHP doesn't care, it just runs the code.

I blacklisted internal classes because it almost always results in an error with an obscure error message. stdClass should be specifically supported, though. I…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ToshY
Comment options

Answer selected by ToshY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants