Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corece an array out of an stdClass? #436

Closed
Ocramius opened this issue Jan 12, 2024 · 3 comments · Fixed by #437
Closed

Corece an array out of an stdClass? #436

Ocramius opened this issue Jan 12, 2024 · 3 comments · Fixed by #437
Assignees
Labels
Type: Enhancement Most issues will probably ask for additions or changes.

Comments

@Ocramius
Copy link
Contributor

Just throwing out ideas, but in the context of JSON -> Domain, the ShapeType could be useful when dealing with JSON structures where the difference between hashmap and array is sometimes to be preserved:

$input = <<<'JSON'
{
  "foo": "hello world!"
}
JSON;

$data = Json\decode($input, false);

echo Type\shape([
    'foo' => Type\non_empty_string(),
])->coerce($data)['foo'];
@Ocramius Ocramius added the Type: Enhancement Most issues will probably ask for additions or changes. label Jan 12, 2024
@veewee
Copy link
Collaborator

veewee commented Jan 13, 2024

This could be a nice addition indeed.

Just thinking out loud here: We could accept any object and cast it to an array.

https://3v4l.org/Hrv0H

<?php

$x = (object) [];
$x->foo = 'bar';


class X {
    public $foo = 'bar';
    private $baz = 'bar';
}



var_dump((array) $x);
var_dump((array) new X);

However:

  • I'm not sure that this will work for all kind of objects. Maybe you know about this?
  • It takes all properties, also private ones.
  • It seems a bit hacky to implement it this way.
    • The alternative is to use reflection (like this) - but this package does not have a reflection component (yet).

@Ocramius
Copy link
Contributor Author

We could accept any object

IMO a bit too risky 😁

Array casts on objects lead to weird null byte keys too.

My endorsement would be to start with stdClass, since its semantics are well known.

Any Traversable is also already handled.

Ocramius added a commit to Ocramius/psl that referenced this issue Jan 16, 2024
Fixes azjezz#436

This change is useful when decoding JSON hashmaps into PHP hashmaps, since PHP keeps
JSON-decoded data as an `stdClass` unless explicitly told to do so.

Also note that preserving JSON structures as `stdClass` is sometimes very much required,
since an empty hashmap is a lossy conversion to `[]` performed by `json_decode()`, when
using `associative: true`. This patch makes preserving JSON data structures a bit easier.

This adds some very minimal overhead to `ShapeType#coerce()`, which should be imperceptible.
@Ocramius
Copy link
Contributor Author

#437

veewee pushed a commit that referenced this issue Jan 17, 2024
Fixes #436

This change is useful when decoding JSON hashmaps into PHP hashmaps, since PHP keeps
JSON-decoded data as an `stdClass` unless explicitly told to do so.

Also note that preserving JSON structures as `stdClass` is sometimes very much required,
since an empty hashmap is a lossy conversion to `[]` performed by `json_decode()`, when
using `associative: true`. This patch makes preserving JSON data structures a bit easier.

This adds some very minimal overhead to `ShapeType#coerce()`, which should be imperceptible.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Most issues will probably ask for additions or changes.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants