Giter Club home page Giter Club logo

flute's Introduction

Flute

An utterly awesome and easy-to-use, lightweight and highly-extensible fluent validation framework for PHP.

Flute's fluent interface is heavily inspired by FluentValidation for .NET.

Here is a simple example to get you started :

require 'path/to/flute.php';

$validator = new Validator();
$validator->rule_for('first_name')->max_length(100);

$p = new Person('John');
$result = $validator->validate($p);

if ($result->valid()) {
	echo 'Valid!';
}

These validation rules are supported out of the box :

  • NotNull
  • NotEmpty
  • Required
  • MaxLength
  • ... (and many more. You can also add rules yourself)

Creating custom validation rules

It's very easy to create custom rules, and you don't even have to alter the Validator class. Everything is done with magic naming conventions! For example, here is how the NotEmpty rule is built :

// The naming is very important here. More on that later.
class NotEmptyRule extends Rule // We must extend the Rule abstract class
{
	//We override the condition function to run our own validation logic
	public function condition($value)
	{
		// The $value variable contains the value we need to validate.
		// For this particular case, it is considered valid if it is
		// not equal to the empty string.
		return $value !== '';
	}
}

Once your rule is defined, it must be discoverable by any registered auto-loader in order the make it available to the Validator class. Here is how you invoke it :

// Create an instance of the validator
$v = new Validator();

$v->rule_for('first_name')->not_empty();

See what I did there? No need to add the not_empty() function to the validator. Any unknown function invoked on a validator will create a rule named with the following conventions :

  • Every word delimited by underscores is capitalized.
  • The underscores are removed.
  • 'Rule' is appended to the resulting string.

So not_empty becomes NotEmptyRule. The validator will instantiate a NotEmptyRule class and associate it with the property name defined when we called rule_for. Easy, huh?

What if I want to use parameters?

Very simple. Here is a brief look at the MaxLengthRule implementation.

class MaxLengthRule extends Rule
{
	public function condition($value)
	{
		return strlen($value) <= $this->max_length;
		//W-w-w-wait, what!? Where does max_length come from?
	}
}

$v = new Validator();
$v->rule_for('first_name')->max_length(100);

Any parameters passed to the function definition will be registered in the args array in the same order that they were passed to the validator rule. You can also access args with the magic __get method, like I did here with max_length. I you were to call max_length again, it will send back the same value as the first time it was called. When using multiple arguments, they are handed out in the order they were passed, and each name gets its own argument.

Extending existing rules

Your custom rule can extend the behaviour of existing rules. Let's take a look at the RequiredRule implementation :

class RequiredRule extends Rule
{
	public function extend() {
		return array(
			new NotNullRule(),
			new NotEmptyRule()
		);
	}
}

As you can see, the RequiredRule is only a combination of the NotNullRule and the NotEmptyRule. The extend function returns an array of instantiated rules which you want to extend. The condition function will be called on both rules and combined with a logical and. That is, if any of the condition fails, it is considered invalid.

That's it for the moment! Stay tuned for more awesome features!

flute's People

Contributors

marcofiset avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

flute's Issues

Allow to reuse same arguments with magic __get method

For a rule, every time a call is made to __get magic method, I want to remember the name of the invoked property and store it alongside which index of the args array it corresponds. This will allow to call the same property multiple times without incrementing the next_args_index everytime.

Use namespaces

Use namespaces in order to enable a PSR-0 compliant autoloader.

Use Testify as a dependency

Currently, Testify's files are copied to the flute repo. Make it so it uses composer to install it as a dependency.

Error messages for failed validations

Currently, the validate method returns only true of false. We have no idea what rules have failed and why. Think up a method to make the errors available to the user of the validator class. More details here when I come up with a good solution.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.