-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
HalResource::validateElementName() with numbered array #7
Comments
I've confirmed this with a similar structure if an array looks like the following, and fed to fromArray(); Array
(
[0] => Array
(
[id] => 1
[name] => Bank #1
[phone] => 555-555-5555
[zone_id] => 18
)
[1] => Array
(
[id] => 2
[name] => Bank #2
[phone] => 555-555-5555
[zone_id] => 18
)
[2] => Array
(
[id] => 7
[name] => Test Company
[phone] => 555-555-5555
[zone_id] => 18
)
) Seems the Originally posted by @adamculp at zendframework/zend-expressive-hal#42 (comment) |
I've been able to create a reproduce case finally, from what @grizzm0 originally wrote: use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Expressive\Hal\HalResource;
use Zend\Expressive\Hal\LinkGenerator;
use Zend\Expressive\Hal\Metadata\MetadataMap;
use Zend\Expressive\Hal\ResourceGenerator;
$generator = new ResourceGenerator(
new MetadataMap(),
new class implements ContainerInterface {
public function has($name) : bool
{
return false;
}
public function get($name)
{
return new $name();
}
},
new LinkGenerator(new class implements LinkGenerator\UrlGeneratorInterface {
public function generate(ServerRequestInterface $request, string $routeName, array $routeParams = [], array $queryParams = []) : string
{
return 'https://not-a-url.localdomain/foo/bar';
}
})
);
$resource = new HalResource();
$array = [
['foo' => 'bar'],
];
$resource->embed('foobar', $generator->fromArray($array)); I can start debugging from here. Originally posted by @weierophinney at zendframework/zend-expressive-hal#42 (comment) |
Based on the examples provided, you have arrays of associative arrays, and you want to embed these as a collection in another resource. The way to do it with current code is as follows: $collection = array_map(function ($item) use ($generator) {
return $generator->fromArray($item);
}, $array);
$resource = $resource->embed('foobar', $collection); I've just made those changes to the reproduce case I posted earlier, and it works perfectly. So, either we (a) need more documentation, or (b) need a new method in the If we were to go route (b), I'd argue for a Do those approaches work for either of you? If not, can you provide a full use case demonstrating how you're trying to use the functionality and what you expect to happen, please? Originally posted by @weierophinney at zendframework/zend-expressive-hal#42 (comment) |
I would say by the very needs, that many times a common use case is where an associative array is required. This would facilitate the return of multiple records from a database, for instance, where an associative array is a proper way to utilize the results. So, at a minimum, documentation with how to pass an associative array in (as shown in @weierophinney example above). However, the fromArray() method should also be able to handle both an associative array as well as a single level array for one record. Originally posted by @adamculp at zendframework/zend-expressive-hal#42 (comment) |
@adamculp and I had a skype discussion, and the typical use case here is getting an array of records back from a data source. The problem is that, even when using metadata, we have no way to know how to map an associative array to a resource for purposes of generating links, which means that, at best, you end up with vanilla JSON objects. When it comes to The assumption I'm seeing in this issue, then, is that using the generator should allow you to bypass those restrictions. But it doesn't.
So, how do you create a You have several options:
What it comes down to is:
I'll make a note to write up documentation to make this more clear, as well as explain why the limitations exist. Originally posted by @weierophinney at zendframework/zend-expressive-hal#42 (comment) |
While trying to create a resource from a numbered array an exception is thrown due to
empty($name)
check on zero index on this line.Code to reproduce the issue
Expected results
The resource should be generated just fine.
Actual results
Exception is thrown:
$name provided to Zend\Expressive\Hal\HalResource cannot be empty
Originally posted by @grizzm0 at zendframework/zend-expressive-hal#42
The text was updated successfully, but these errors were encountered: