Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.
/ input-to-date Public archive

A small PHP library that assists with converting your user input into a Carbon object

License

Notifications You must be signed in to change notification settings

Jono20201/input-to-date

Repository files navigation

InputToDate

This is a really simple library to convert user input to a Carbon object without having to worry about the possible exceptions that maybe thrown with bad or empty input.

If you're using a framework (such as Laravel) you should still validate user input if you want to send them nice errors about invalid input.

Features

  • Parse user input to a Carbon object.
  • Throw an Exception or return a default upon error/empty input.
  • Fluent API

Usage

The following example would return a Carbon object with the correct date.

    $user_input = '01/01/2016 09:30:30'
    $date = InputToDate::create('d/m/Y H:i:s')
                        ->convert($user_input);

The following example has incorrect input, and will return null as we have asked it to by running the setReturnNullOnFailure() method.

    $user_input = '01/01/2016 09:30:30'
    $date = InputToDate::create('d/m/Y')
                        ->setReturnNullOnFailure()
                        ->convert($user_input);

The following example will return this current time as its incorrect, but as have asked for a default of Carbon::now().

    $user_input = '01/01/2016 09:30:30'
    $date = InputToDate::create('d/m/Y')
                        ->setDefault(Carbon::now())
                        ->convert($user_input);

The following will throw an InvalidArgumentException exception as the input is incorrect and we have not set any other default. You can also explicitly add the throwException() method.

    $user_input = '01/01/2016 09:30:30'
    $date = InputToDate::create('d/m/Y')
                        ->convert($user_input);

Contribute

Pull requests are more than welcome.

About

A small PHP library that assists with converting your user input into a Carbon object

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages