Added vendor/ directory for Composer's installed files

This commit is contained in:
Ascendings
2015-08-30 12:33:20 -04:00
parent 45df179c49
commit b66a773ed8
1162 changed files with 112457 additions and 0 deletions

View File

@ -0,0 +1,77 @@
<?php
/**
* Violin example. Extending.
*
* You can extend the Violin class to create your own validator,
* making it easier to keep everything in one place, and more
* importantly, the ability to pass dependencies into the
* class.
*
* This example checks a specfic value isn't already taken
* within a table in your database.
*/
require '../../vendor/autoload.php';
//-- Validator.php
use Violin\Violin;
class Validator extends Violin
{
protected $db;
public function __construct(PDO $db)
{
$this->db = $db;
$this->addRuleMessage('unique', 'That {field} is already taken.');
}
/**
* Check if a value already exists in a database table.
*
* @param mixed $value
* @param array $input
* @param array $args
*
* @return bool
*/
public function validate_unique($value, $input, $args)
{
$table = $args[0];
$column = $args[1];
$value = trim($value);
$exists = $this->db->prepare("
SELECT count(*) as count
FROM {$table}
WHERE {$column} = :value
");
$exists->execute([
'value' => $value
]);
return ! (bool) $exists->fetchObject()->count;
}
}
//-- Any other file
// Some database dependency
$db = new PDO('mysql:dbname=project;host=localhost', 'root', 'root');
$v = new Validator($db);
$v->validate([
'username' => ['alex', 'required|alpha|min(3)|max(20)|unique(users, username)'],
'email' => ['alex@codecourse.com', 'required|email|unique(users, email)']
]);
if ($v->passes()) {
// Passed
} else {
var_dump($v->errors()->all());
}

28
vendor/alexgarrett/violin/examples/basic.php vendored Executable file
View File

@ -0,0 +1,28 @@
<?php
/**
* Violin example. Basic validation.
*
* This shows basic validation, passing in an array to the
* validate method and checking if the validation passes
* with the passes method. Then dumps errors if failed.
*/
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin;
$v->validate([
'username' => ['dale', 'required|alpha|min(3)|max(20)'],
'email' => ['dale@codecourse.com', 'required|email'],
'password' => ['ilovecats', 'required'],
'password_confirm' => ['ilovecats', 'required|matches(password)']
]);
if ($v->passes()) {
// Passed
} else {
var_dump($v->errors()->all());
}

View File

@ -0,0 +1,37 @@
<?php
/**
* Violin example. Basic validation, two arrays.
*
* This shows basic validation using two arrays, one for data,
* and one for the ruleset. This is useful if you'd like to
* extract the rules to a seperate class or variable.
*/
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin;
$data = [
'username' => 'dale',
'email' => 'dale@codecourse.com',
'password' => 'ilovecats',
'password_confirm' => 'ilovecats'
];
$rules = [
'username' => 'required|alpha|min(3)|max(20)',
'email' => 'required|email',
'password' => 'required',
'password_confirm' => 'required|matches(password)'
];
$v->validate($data, $rules);
if ($v->passes()) {
// Passed
} else {
var_dump($v->errors()->all());
}

View File

@ -0,0 +1,21 @@
<?php
/**
* Violin example. All errors.
*
* Simply getting a list of errors that occured while trying
* to validate the data passed in against the rules given.
*/
require '../../vendor/autoload.php';
use Violin\Violin;
$v = new Violin;
$v->validate([
'username' => ['dalegarrett1234567890', 'required|alpha|min(3)|max(20)'],
'email' => ['dale.codecourse.com', 'required|email']
]);
var_dump($v->errors()->all()); // Array of all errors.

View File

@ -0,0 +1,22 @@
<?php
/**
* Violin example. Errors for a specific field.
*
* Simply getting a list of errors for a specific field that
* occured while trying to validate the data passed in
* against the rules given.
*/
require '../../vendor/autoload.php';
use Violin\Violin;
$v = new Violin;
$v->validate([
'username' => ['dalegarrett1234567890', 'required|alpha|min(3)|max(20)'],
'email' => ['dale.codecourse.com', 'required|email']
]);
var_dump($v->errors()->get('username')); // Array of all 'username' errors.

View File

@ -0,0 +1,25 @@
<?php
/**
* Violin example. Checking and getting first error.
*
* This checks a specific field has an error, and then outputs
* the first error that occured for that field. This is most
* likely what you'd use in a real life situation within
* your HTML form.
*/
require '../../vendor/autoload.php';
use Violin\Violin;
$v = new Violin;
$v->validate([
'username' => ['dalegarrett1234567890', 'required|alpha|min(3)|max(20)'],
'email' => ['dale.codecourse.com', 'required|email']
]);
if ($v->errors()->has('email')) { // Check if any errors exist for 'email'.
echo $v->errors()->first('email'); // First 'email' error (string).
}

View File

@ -0,0 +1,29 @@
<?php
/**
* Violin example. Custom field message.
*
* Defining an error message for a particular field, when a
* particular rule fails.
*/
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin;
$v->addFieldMessage('username', 'required', 'We need a username to sign you up.');
$v->validate([
'username' => ['', 'required|alpha|min(3)|max(20)'],
'email' => ['dale@codecourse.com', 'required|email'],
'password' => ['ilovecats', 'required'],
'password_confirm' => ['ilovecats', 'required|matches(password)']
]);
if ($v->passes()) {
// Passed
} else {
var_dump($v->errors()->all());
}

View File

@ -0,0 +1,40 @@
<?php
/**
* Violin example. Custom field messages.
*
* Defining an error message for a particular field, when a
* particular rule fails.
*
* This is the same as addFieldMessage, but allows adding
* of multiple field messages in one go.
*/
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin;
$v->addFieldMessages([
'username' => [
'required' => 'We need a username to sign you up.',
'alpha' => 'Your username can only contain letters.'
],
'email' => [
'email' => 'That email doesn\'t look valid.'
]
]);
$v->validate([
'username' => ['cats4life', 'required|alpha|min(3)|max(20)'],
'email' => ['dale.codecourse.com', 'required|email'],
'password' => ['ilovecats', 'required'],
'password_confirm' => ['ilovecats', 'required|matches(password)']
]);
if ($v->passes()) {
// Passed
} else {
var_dump($v->errors()->all());
}

View File

@ -0,0 +1,28 @@
<?php
/**
* Violin example. Custom rule message.
*
* Defining an error message for when a particular rule fails.
*/
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin;
$v->addRuleMessage('required', 'Hold up, the {field} field is required!');
$v->validate([
'username' => ['', 'required|alpha|min(3)|max(20)'],
'email' => ['dale@codecourse.com', 'required|email'],
'password' => ['ilovecats', 'required'],
'password_confirm' => ['ilovecats', 'required|matches(password)']
]);
if ($v->passes()) {
// Passed
} else {
var_dump($v->errors()->all());
}

View File

@ -0,0 +1,34 @@
<?php
/**
* Violin example. Custom rule message.
*
* Defining an error message for when a particular rule fails.
*
* This is the same as addRuleMessage, but allows adding
* of multiple rule messages in one go.
*/
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin;
$v->addRuleMessages([
'required' => 'Hold up, the {field} field is required!',
'email' => 'That doesn\'t look like a valid email address to me.'
]);
$v->validate([
'username' => ['', 'required|alpha|min(3)|max(20)'],
'email' => ['dale.codecourse.com', 'required|email'],
'password' => ['ilovecats', 'required'],
'password_confirm' => ['ilovecats', 'required|matches(password)']
]);
if ($v->passes()) {
// Passed
} else {
var_dump($v->errors()->all());
}

View File

@ -0,0 +1,26 @@
<?php
/**
* Violin example. Field aliases.
*
* Shows using field aliases to rename fields without having
* to define custom rule or field messages, making it a
* cleaner solution if this will suffice.
*/
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin;
$v->validate([
'username|Username' => ['', 'required'],
'password|Password' => ['', 'required']
]);
if ($v->passes()) {
// Passed
} else {
var_dump($v->errors()->all());
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Violin example. Message placeholders.
*
* Shows the placeholders you can use when defining messages. You can
* output the name of the field, the value given by the user, and
* the arguments that were passed into the rule.
*/
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin;
$v->addRuleMessage('between', 'The {field} must be between {$0} and {$1}, you gave {value}');
$v->validate([
'age' => ['82', 'required|between(18, 35)']
]);
if ($v->passes()) {
// Passed
} else {
var_dump($v->errors()->all());
}

View File

@ -0,0 +1,31 @@
<?php
/**
* Violin example. Message placeholders.
*
* Shows how you can output all arguments as a comma seperated list,
* if you have a variable amount of arguments for a rule.
*/
require '../../../vendor/autoload.php';
use Violin\Violin;
$v = new Violin;
$v->addRuleMessage('isoneof', '{field} must be one of {$*}');
$v->addRule('isoneof', function($value, $input, $args) {
$value = trim($value);
return in_array($value, $args);
});
$v->validate([
'age' => ['sheep', 'required|isoneof(apples, pears)']
]);
if ($v->passes()) {
// Passed
} else {
var_dump($v->errors()->all());
}

View File

@ -0,0 +1,32 @@
<?php
/**
* Violin example. Custom rule.
*
* Creating a custom rule using the addRule method, passing in a
* closure which should return false if the check has failed,
* or true if the check has passed.
*/
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin;
$v->addRuleMessage('startsWithNumber', 'The {field} must start with a number.');
$v->addRule('startsWithNumber', function($value, $input, $args) {
$value = trim($value);
return is_numeric($value[0]);
});
$v->validate([
'username' => ['dale', 'required|min(3)|max(20)|startsWithNumber']
]);
if ($v->passes()) {
// Passed
} else {
var_dump($v->errors()->all());
}

View File

@ -0,0 +1,35 @@
<?php
/**
* Violin example. Custom rule.
*
* Creating a custom rule using the addRule method, passing in a
* closure which should return false if the check has failed,
* or true if the check has passed.
*
* This example shows the use of arguments that can be used
* to make rules that require arguments.
*/
require '../vendor/autoload.php';
use Violin\Violin;
$v = new Violin;
$v->addRuleMessage('startsWith', 'The {field} must start with "{$0}".');
$v->addRule('startsWith', function($value, $input, $args) {
$value = trim($value);
return $value[0] === $args[0];
});
$v->validate([
'username' => ['dale', 'required|min(3)|max(20)|startsWith(a)']
]);
if ($v->passes()) {
// Passed
} else {
var_dump($v->errors()->all());
}