On Wed, 26 Feb 2020 at 16:42, Paul M. Jones <pmjo...@pmjones.io> wrote:

> Your presumption is correct! And your point on trying for better names is
> well-taken -- though I think these are "expected" names, based on my
> research into existing implementations. The most-common names are ...
>
> - the word "files" for unparsed or unmodified $_FILES values, leading me
> to think $files is well-understood
>
> - the words "upload(s)", "fileUpload(s)", or "uploadedFile(s)" for parsed,
> transformed, or restructured $_FILES values, leading me to think $uploads
> is well-understood
>


That's a reasonable justification. Just to check, are there other
implementations that have both of these names side by side, or do most
implementations have one or the other, but using this naming?

The main confusion I can see is having to remember which two of these is an
error without having to read the docs each time:

isset( $request->files['attachment']['name'][0] );
isset( $request->files['attachment'][0]['name'] );
isset( $request->uploads['attachment']['name'][0] );
isset( $request->uploads['attachment'][0]['name'] );




> Having said that, I am open to suggestion here. What names do you think
> would be better than the ones presented, contra pre-existing work from
> other authors?
>


Looking at the examples, the difference is rather similar to the
PREG_PATTERN_ORDER and PREG_SET_ORDER options to preg_match_all.

If getUploads was a method, it could take a similar behaviour switch -
GROUP_BY_ITEM vs GROUP_BY_ATTRIBUTE or similar. For a property, that would
have to be part of the name, like $uploadsByItem and $uploadsByAttribute,
which are a bit ugly.

Alternatively, you could lean more heavily on the legacy aspect, and have
$uploads and $uploadsLegacyFormat or something like that.



> Incidentally, the current documentation doesn't describe the differences
> particularly well, just saying one is "more like $_POST". Some details of
> the structure, or examples comparing the two arrays, would be useful.
>
> Good call! On your suggestion, I have added details at <
> https://github.com/pmjones/ext-request#the-uploads-array>.  Suggestions
> for improvement are welcome.
>


Thanks, that makes it a lot clearer. :)




> - "There is no function which currently takes a string and returns an
> array for this scenario." -- True, though (and not to undermine my own
> case) there is json_decode(), which can return an array. But then the
> trouble is how to decide when to apply it, and with what parameters, and
> how to trap & report errors, etc., all of which adds complexity to what I
> think ought to be a simple object. And then once special treatment is given
> to JSON, what about XML? Etc., etc. Given all that, it strikes me that the
> cases not already served by PHP for parsing the body content into $_POST
> are best left to consumers of ServerRequest.
>


Again, any mention of JSON or XML is drifting away from what I'm asking
for. What I'm asking for (or rather, suggesting would be a useful and
consistent addition) is a way to do *exactly what PHP does right now*, but
based on a given input string, rather than the initial request. I am
totally happy with users needing to add any support for JSON, XML, etc,
just like they would have to populate $_POST manually.




> I agree that the default $content value is an exception to everything else
> in ServerRequest. While it stays read-only, it does get read from
> php://input each time you refer to it.
>
> The alternative is to read from php://input once at construction time
> (when the construct $content argument is null) and retain that value in
> $content from the start. But in that case, when you have very large content
> bodies (as when there are PUT file uploads or other large payloads), then
> ServerRequest takes up a lot of memory, when it might not ever be used
> directly. Yes, "it might never be used" could be true of every element on
> ServerRequest, but the maximum memory use for those other elements tends to
> be rather smaller.
>


That's a reasonable justification. I guess that's why PSR-7 exposes it as a
stream, even though that causes a whole load of pain.

To play devil's advocate, does the property belong in the object at all? If
it doesn't interact with any of the other properties (e.g. populating $post
and $uploads based on form data), could "better syntax for getting raw
request for global state" be a separate feature. Then again, maybe the
ability to over-ride it in the constructor is enough to make it useful.


Regards,
-- 
Rowan Tommins
[IMSoP]

Reply via email to