Download the PHP package ryanwhitman/php-values without Composer

On this page you can find all versions of the php package ryanwhitman/php-values. 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 php-values

PHP Values

Latest Version on Packagist PHP from Packagist

PHP Values is a tool for creating immutable value objects in PHP. A value object intakes a raw value, transforms it, validates it, and can be used consistently and dependably across your application.

For instance, suppose you need an email address when creating a user. You can write it more traditionally like this:

But it's more optimal to write it like this:

Install

You should install the package via composer:

Example

Start by creating a Value class. For instance, a Value class for an email address:

Now, you're ready to use the value:

Usage

To create a new Value class, extend the RyanWhitman\PhpValues\Value class. From there, define a transform method (optional) and a validate method (mandatory). Upon instantiation, the transform method receives the raw input and transforms it, as needed. Then, the validate method receives the transformed value and returns true or false. If validation passes, the object is ready for use. If validation passes, InvalidValueException is thrown. Note: 2 try static methods exist that catch the exception and return null.

transform(mixed $value): mixed

The transform method is an optional method called during instantiation. It receives the input value and, when defined, should return a sanitized/transformed version of the value. The transform method is not defined in the base abstract Value class to allow for proper typing in sub-classes.

validate(mixed $value): bool

The validate method is called during instantiation. It receives the transformed value and should return true or false. The validate method is not defined in the base abstract Value class to allow for proper typing in sub-classes.

Base Values

Suppose you're creating a Value class for a person's name. You'll likely want to remove all superfluous whitespace. You could, of course, simply call another Value class within your transform method, but you can also define a $baseValues property to automatically run other Value classes:

Static Methods

from(mixed $value): Value

The from static method will return a Value instance when validation passes and will throw an exception when validation fails.

getFrom(mixed $value): mixed

The getFrom static method is a shortcut for ::from($value)->get().

tryFrom(mixed $value): ?Value

The tryFrom static method will return a Value instance when validation passes and null when validation fails.

tryGetFrom(mixed $value): mixed

The tryGetFrom static method is a shortcut for ::tryFrom($value)->get().

isValid(mixed $value): bool

The isValid static method will return true or false.

Instance Methods

getOrigValue(): mixed

The getOrigValue method returns the original input value (before transformation).

get(): mixed

The get method returns the transformed and validated value.

Shortcut Methods

As mentioned above, the getFrom and tryGetFrom static methods are shortcuts for ::from($value)->get() and ::tryFrom($value)->get(), respectively. You may add the ShortcutMethod annotation/attribute to your custom get methods to add the same shortcut capabilities. Shortcut methods must be defined using camelCase and start with get (e.g. getFormatted).

Using a doctrine annotation in PHP 7.4+:

Using an attribute in PHP 8.0+:

After adding the ShortcutMethod annotation/attribute to the getFormatted method, for example, the following will work:

Traits

RyanWhitman\PhpValues\Concerns\Stringable

The Stringable trait simply defines the __toString() magic method with (string) $this->get().

Exceptions

PHP Values will throw 1 of 2 exceptions:

RyanWhitman\PhpValues\Exceptions\InvalidValueException will be thrown when either a TypeError occurs (e.g. an array is needed but a string is provided) or when validation fails. This exception is useful as it indicates the raw input is invalid. RyanWhitman\PhpValues\Exceptions\Exception is thrown when something else goes wrong (e.g. a validate method is not defined). Note: The try methods only catch InvalidValueException.

Pre-Built Values

Testing

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please have a look at License File for more information.


All versions of php-values with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4,<8.3
doctrine/annotations Version ^1|^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 ryanwhitman/php-values contains the following files

Loading the files please wait ....