phpgt / input
Encapsulated user input.
Fund package maintenance!
Requires
- php: >=8.4
- phpgt/http: ^2.0
- phpgt/json: ^2.3
Requires (Dev)
- phpmd/phpmd: ^2.13
- phpstan/phpstan: ^2.2
- phpunit/phpunit: ^12.5
- squizlabs/php_codesniffer: ^4.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
By default, PHP stores all user input in global arrays ($_GET, $_POST, and $_FILES) available for reading and modification in any code, including third party libraries.
This library wraps user input in objects that promote encapsulation, allowing functions to be passed only the user input they require, rather than having unmitigated read/write access to everything.
Type-safe functions allow more predictable functionality, such as $input->getFileUpload("photo"), $input->getDateTime("date-of-birth"), and $input->getMultipleString("pizza-topping").
getDateTime() returns a mutable DateTime, or null for an empty or missing value. Pass a format as the second argument to parse a specific date format, for example $input->getDateTime("date-of-birth", "!d/m/Y"). Invalid values throw DataNotCompatibleFormatException.
Example usage
<form method="post"> <h1>User Profile</h1> <label> <span>Your name</span> <input name="name" placeholder="e.g. Eugene Kaspersky" required /> </label> <label> <span>Age</span> <input type="number" name="age" /> </label> <label> <span>Interests</span> <select name="interest[]" multiple> <option>Mathematics</option> <option>Cryptography</option> <option>Information Security</option> <option>Cyberwarfare</option> </select> </label> <label> <span>Photo</span> <input name="photo" type="file" /> </label> <button name="do" value="save">Save profile</button> </form>
<?php $profile->update( $profileId, // Use type-safe getters to help write maintainable code. $input->getString("name"), $input->getInt("age"), ); // Handle multiple values with type safety. foreach($input->getMultipleString("interest") as $interest) { $profile->addInterest($interest); } // Handle file uploads with a FileUpload object. $photoUpload = $input->getFile("photo"); if($photoUpload instanceof FailedFileUpload) { // Handle a failed upload here. } $photoUpload->moveTo("data/upload/$profileId.jpg");
When PHP rejects an upload, getFile() returns a FailedFileUpload. Check its getError() or getErrorMessage() before moving it. Calling moveTo() on a failed upload throws UploadedFileMoveException with PHP's upload error code and a readable reason, including upload_max_filesize or the form's MAX_FILE_SIZE limit when applicable.
Features at a glance
- Type-safe getters, implementing the TypeSafeGetter interface.
- Typed
multiplegetters, for working with checkboxes, multi-select elements or multiple file uploads. - "do" callback functions - hook up callbacks to button presses (implemented automatically in WebEngine applications).
- "when" triggers - execute callbacks when certain user input is present.
FileUploadInputDataclass for easy file uploads, including functions such asmoveTo(),getOriginalName(), etc.- Coming soon: working with huge files by streaming them to PHP, efficiently managing memory in the process.