Download the PHP package thepublicgood/yerp without Composer

On this page you can find all versions of the php package thepublicgood/yerp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package yerp

Run Tests

Yerp

Yerp is an object property validation library for PHP. It was written to provide simply validation for some other libraries, but it may have some value elsewhere.

Yerp uses PHP Attributes to set validation rules on public properties. You can then pass an instance of that object to the validator and get a result. Results are are boolean, so there's no translation or language requirements. You can specify your own messages based on the result.

Installation

Usage

Each validation rule is a different attribute class in the TPG\Yerp\Rules namespace.

Here's a quick example:

There are a number of built-in rules and you can easily add your own.

Note that only public properties can validated.

Running the validator

To validate an object, pass it to the Validator class and call the validate method. This will return an instance of Validated which provides access to the results of each rule on each property.

Getting the results

The Validated class provides a property method which you can use to get to the result of a particular property. The property method returns an instance of Result. You can use the provided passed method to test the result of a given property:

The passed method returns a boolean representing the validation result of that property. There is also a failed method which returns the exact opposite:

If you want to know the result of a specific rule on the property, you can pass the class name of the rule to the property method:

Stopping on the first error

All validation rules will be evaluated in the order they appear. You can tell Yerp to stop validating the rest of the rules if a specific one fails. You can do this by passing true to the last property on any rule:

In the example above, if the Email rule fails, then the Equal rule will not be evaluated. If the Email rule DOES pass, then the Equal rule will be evaluated and will be present in the results. If you end the evaluation by using last, then any rules that come later will not be included in the results set. This is important because if you try to check the outcome of a rule that doesn't exist in the results, you'll get an InvalidRuleException.

Setting messages per rule

Although Yerp is designed to be translation agnostic, it can be handy to set a static message. You can do so by passing a string value to the failure property of any rule:

When this rule evaluates, you'll be able to use the message method on the Result class:

If the rule passes, or no message is set, then the message method will return null. By default, the message is only returned when the rule fails. However, you can specify a success message if you like by passing a string to the success. property:

Available rules

Required

The Required rule is handy to validate a property that must be present. A "required" property must contain a value, cannot be null and cannot be empty. An empty array, or an empty string would fail:

Alpha

The Alpha rule ensures that the value of the property contains only letters.

ArrayKey

The ArrayKey rule simply ensures that the specified key exists in the array:

Boolean

The Boolean rule allows you to validate that the property is either TRUE or FALSE:

Equality

You can ensure that a property is equal to a specific value. Simply pass the required value as the first parameter of the Euqal rule:

There is also an opposing NotEqual rule.

Email

A common validation rule is to ensure that a string is a valid email address:

In

The In rule allows you to test if a value is one of the specified values:

Length

The Length rule allows you to specify a minimum and/or maximum length. If the property type is an array, then length validates the number of array elements:

You don't need to specify both, but you must specify at least one value.

Numeric

The Numeric rule ensures that the property is parsable as a number:

Regex

You can pass a regex string as the first parameter of the Regex rule:

Writing new rules

Yerp only provides a small number of rules. This was mainly because they're the only ones we needed at time. We might add new rules as we need them more often, but it's simple to add your own rules without needing to ask.

Create a new class to contain your rule logic extending the TPG\Yerp\Rules\AbstractRule class and add #[Attribute] to the class definition.

You'll need to implement the required validate method which returns a TPG\Yerp\Result. You can use the getResult method, which takes a boolean value, to return a new Result instance:

Now you can use your new rule in any class:

Your new rule gets the same last, success and failure parameters. If your rule needs to accept a value, simply specify it in a constructor:

Credits

License

The MIT License (MIT). See the [LICENSE.md]() file for more information.


All versions of yerp with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package thepublicgood/yerp contains the following files

Loading the files please wait ....