Request wrappers for Hack

One of the most frustrating parts of using Hack is accessing user-provided data through the request based $_GET and $_POST super-globals. The problem being two-fold:

  • In partial mode, these aren’t typed. This means Hack assumes you know what you’re doing with the values, which can be bad.
  • In strict mode, the super globals aren’t available and result in undefined variable errors in the type checker.

That’s where my new package comes in.

I have released the 1.0 version of my Hack Requests package. Its source code is available from my gitlab instance, which also contains the documentation for it.

There are methods to check that a value is provided and to get values out as any of:

  • string
  • int
  • float
  • number
  • bool
  • mixed

As most of the data coming in is a string (due to how PHP handles HTTP parameters), loss-less conversions happen. If the conversion would result in a loss of data, then it doesn’t happen and either null is returned or an exception is thrown.

Once installed, the package provides wrappers for the $_GET, $_POST, $_REQUEST, $_COOKIE, $_FILES and $_SERVER super globals.

Usage information and examples are included in the documentation.

Give it a try and let me know what you think, either as comments on this post or by opening issues on the repo.

build status

PayPal: simon@simon.geek.nz

 

Leave a Reply