Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
syncEnrollments
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Yeray Santana Hualde
syncEnrollments
Commits
c3b8524e
Commit
c3b8524e
authored
Jul 13, 2011
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rewrote validation library.
parent
bbc45f92
Changes
22
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
22 changed files
with
494 additions
and
1222 deletions
+494
-1222
validation.php
application/lang/en/validation.php
+20
-45
eloquent.php
system/db/eloquent.php
+11
-0
input.php
system/input.php
+12
-0
lang.php
system/lang.php
+10
-8
str.php
system/str.php
+4
-4
errors.php
system/validation/errors.php
+2
-12
message.php
system/validation/message.php
+0
-155
nullable_rule.php
system/validation/nullable_rule.php
+0
-94
rangable_rule.php
system/validation/rangable_rule.php
+0
-145
rule.php
system/validation/rule.php
+0
-72
acceptance_of.php
system/validation/rules/acceptance_of.php
+0
-39
confirmation_of.php
system/validation/rules/confirmation_of.php
+0
-25
exclusion_of.php
system/validation/rules/exclusion_of.php
+0
-43
format_of.php
system/validation/rules/format_of.php
+0
-43
inclusion_of.php
system/validation/rules/inclusion_of.php
+0
-43
length_of.php
system/validation/rules/length_of.php
+0
-49
numericality_of.php
system/validation/rules/numericality_of.php
+0
-116
presence_of.php
system/validation/rules/presence_of.php
+0
-29
uniqueness_of.php
system/validation/rules/uniqueness_of.php
+0
-62
upload_of.php
system/validation/rules/upload_of.php
+0
-160
with_callback.php
system/validation/rules/with_callback.php
+0
-48
validator.php
system/validator.php
+435
-30
No files found.
application/lang/en/validation.php
View file @
c3b8524e
...
...
@@ -2,50 +2,25 @@
return
array
(
/*
|--------------------------------------------------------------------------
| General Validation Messages
|--------------------------------------------------------------------------
*/
"acceptance_of"
=>
"The :attribute must be accepted."
,
"confirmation_of"
=>
"The :attribute confirmation does not match."
,
"exclusion_of"
=>
"The :attribute value is invalid."
,
"format_of"
=>
"The :attribute format is invalid."
,
"inclusion_of"
=>
"The :attribute value is invalid."
,
"presence_of"
=>
"The :attribute can't be empty."
,
"uniqueness_of"
=>
"The :attribute has already been taken."
,
"with_callback"
=>
"The :attribute is invalid."
,
/*
|--------------------------------------------------------------------------
| Numericality_Of Validation Messages
|--------------------------------------------------------------------------
*/
"number_not_valid"
=>
"The :attribute must be a number."
,
"number_not_integer"
=>
"The :attribute must be an integer."
,
"number_wrong_size"
=>
"The :attribute must be :size."
,
"number_too_big"
=>
"The :attribute must be no more than :max."
,
"number_too_small"
=>
"The :attribute must be at least :min."
,
/*
|--------------------------------------------------------------------------
| Length_Of Validation Messages
|--------------------------------------------------------------------------
*/
"string_wrong_size"
=>
"The :attribute must be :size characters."
,
"string_too_big"
=>
"The :attribute must be no more than :max characters."
,
"string_too_small"
=>
"The :attribute must be at least :min characters."
,
/*
|--------------------------------------------------------------------------
| Upload_Of Validation Messages
|--------------------------------------------------------------------------
*/
"file_wrong_type"
=>
"The :attribute must be a file of type: :types."
,
"file_too_big"
=>
"The :attribute exceeds size limit of :maxkb."
,
"accepted"
=>
"The :attribute must be accepted."
,
"active_url"
=>
"The :attribute does not exist."
,
"alpha"
=>
"The :attribute may only contain letters."
,
"alpha_dash"
=>
"The :attribute may only contain letters, numbers, dashes, and underscores."
,
"alpha_num"
=>
"The :attribute may only contain letters and numbers."
,
"between"
=>
"The :attribute must be between :min - :max."
,
"confirmed"
=>
"The :attribute confirmation does not match."
,
"email"
=>
"The :attribute format is invalid."
,
"image"
=>
"The :attribute must be an image."
,
"in"
=>
"The selected :attribute is invalid."
,
"integer"
=>
"The :attribute must be an integer."
,
"max"
=>
"The :attribute must be less than :max."
,
"mimes"
=>
"The :attribute must be a file of type: :values."
,
"min"
=>
"The :attribute must be at least :min."
,
"not_in"
=>
"The selected :attribute is invalid."
,
"numeric"
=>
"The :attribute must be a number."
,
"required"
=>
"The :attribute field is required."
,
"size"
=>
"The :attribute must be :size."
,
"unique"
=>
"The :attribute has already been taken."
,
"url"
=>
"The :attribute format is invalid."
,
);
\ No newline at end of file
system/db/eloquent.php
View file @
c3b8524e
...
...
@@ -83,6 +83,17 @@ abstract class Eloquent {
* @return void
*/
public
function
__construct
(
$attributes
=
array
())
{
$this
->
fill
(
$attributes
);
}
/**
* Set the attributes of the model using an array.
*
* @param array $attributes
* @return void
*/
public
function
fill
(
$attributes
)
{
foreach
(
$attributes
as
$key
=>
$value
)
{
...
...
system/input.php
View file @
c3b8524e
...
...
@@ -9,6 +9,18 @@ class Input {
*/
public
static
$input
;
/**
* Get all of the input data for the request.
*
* This method returns a merged array containing Input::get and Input::file.
*
* @return array
*/
public
static
function
all
()
{
return
array_merge
(
static
::
get
(),
static
::
file
());
}
/**
* Determine if the input data contains an item.
*
...
...
system/lang.php
View file @
c3b8524e
...
...
@@ -54,26 +54,28 @@ class Lang {
/**
* Get the language line.
*
* @param string $language
* @param mixed $default
* @return string
*/
public
function
get
(
$default
=
null
)
public
function
get
(
$
language
=
null
,
$
default
=
null
)
{
$language
=
Config
::
get
(
'application.language'
);
if
(
is_null
(
$language
))
{
$language
=
Config
::
get
(
'application.language'
);
}
list
(
$file
,
$line
)
=
$this
->
parse
(
$this
->
key
);
$this
->
load
(
$file
,
$language
);
if
(
!
array_key_exists
(
$language
.
$file
,
static
::
$lines
))
if
(
!
isset
(
static
::
$lines
[
$language
.
$file
][
$line
]
))
{
$line
=
is_callable
(
$default
)
?
call_user_func
(
$default
)
:
$default
;
}
else
{
$line
=
Arr
::
get
(
static
::
$lines
[
$language
.
$file
],
$line
,
$default
);
return
is_callable
(
$default
)
?
call_user_func
(
$default
)
:
$default
;
}
$line
=
static
::
$lines
[
$language
.
$file
][
$line
];
foreach
(
$this
->
replacements
as
$key
=>
$value
)
{
$line
=
str_replace
(
':'
.
$key
,
$value
,
$line
);
...
...
system/str.php
View file @
c3b8524e
...
...
@@ -60,13 +60,13 @@ class Str {
/**
* Generate a random alpha or alpha-numeric string.
*
* Supported types: 'alnum' and 'alpha'.
* Supported types: 'al
pha_
num' and 'alpha'.
*
* @param int $length
* @param string $type
* @return string
*/
public
static
function
random
(
$length
=
16
,
$type
=
'alnum'
)
public
static
function
random
(
$length
=
16
,
$type
=
'al
pha_
num'
)
{
$value
=
''
;
...
...
@@ -86,11 +86,11 @@ class Str {
* @param string $type
* @return string
*/
private
static
function
pool
(
$type
=
'alnum'
)
private
static
function
pool
(
$type
=
'al
pha_
num'
)
{
switch
(
$type
)
{
case
'alnum'
:
case
'al
pha_
num'
:
return
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
;
default
:
...
...
system/validation/error
_collector
.php
→
system/validation/error
s
.php
View file @
c3b8524e
<?php
namespace
System\Validation
;
class
Error
_Collector
{
class
Error
s
{
/**
* All of the error messages.
...
...
@@ -10,7 +10,7 @@ class Error_Collector {
public
$messages
;
/**
* Create a new Error
Collector
instance.
* Create a new Error
s
instance.
*
* @return void
*/
...
...
@@ -30,13 +30,7 @@ class Error_Collector {
*/
public
function
add
(
$attribute
,
$message
)
{
// -------------------------------------------------------------
// Make sure the error message is not duplicated.
//
// For example, the Nullable rules can add a "required" message.
// If the same message has already been added we don't want to
// add it again.
// -------------------------------------------------------------
if
(
!
array_key_exists
(
$attribute
,
$this
->
messages
)
or
!
is_array
(
$this
->
messages
[
$attribute
])
or
!
in_array
(
$message
,
$this
->
messages
[
$attribute
]))
{
$this
->
messages
[
$attribute
][]
=
$message
;
...
...
@@ -94,10 +88,6 @@ class Error_Collector {
{
$all
=
array
();
// ---------------------------------------------------------
// Add each error message to the array of messages. Each
// messages will have the specified format applied to it.
// ---------------------------------------------------------
foreach
(
$this
->
messages
as
$messages
)
{
$all
=
array_merge
(
$all
,
$this
->
format
(
$messages
,
$format
));
...
...
system/validation/message.php
deleted
100644 → 0
View file @
bbc45f92
<?php
namespace
System\Validation
;
use
System\Str
;
use
System\Lang
;
class
Message
{
/**
* Get the appropriate validation message for a rule attribute.
*
* @param Rule $rule
* @param string $attribute
* @return string
*/
public
static
function
get
(
$rule
,
$attribute
)
{
if
(
$rule
instanceof
Rangable_Rule
)
{
$message
=
static
::
get_rangable_message
(
$rule
);
}
elseif
(
$rule
instanceof
Rules\Upload_of
)
{
$message
=
static
::
get_upload_of_message
(
$rule
);
}
else
{
$message
=
static
::
get_message
(
$rule
);
}
return
static
::
prepare
(
$rule
,
$attribute
,
$message
);
}
/**
* Get the error message for a typical validation rule.
*
* @param Rule $rule
* @return string
*/
private
static
function
get_message
(
$rule
)
{
// ---------------------------------------------------------
// The built-in error messages are stored in the language
// directory and are keyed by the class name of the rule
// they are associated with.
// ---------------------------------------------------------
if
(
is_null
(
$rule
->
error
))
{
$class
=
explode
(
'\\'
,
get_class
(
$rule
));
$rule
->
error
=
strtolower
(
end
(
$class
));
}
return
(
is_null
(
$rule
->
message
))
?
Lang
::
line
(
'validation.'
.
$rule
->
error
)
->
get
()
:
$rule
->
message
;
}
/**
* Get the error message for a Rangable rule.
*
* @param Rule $rule
* @return string
*/
private
static
function
get_rangable_message
(
$rule
)
{
// ---------------------------------------------------------
// Rangable rules sometimes set a "presence_of" error.
//
// This occurs when an attribute is null and the option to
// allow null values has not been set.
// ---------------------------------------------------------
if
(
$rule
->
error
==
'presence_of'
)
{
return
static
::
get_message
(
$rule
);
}
// ---------------------------------------------------------
// Slice "number_" or "string_" off of the error type.
// ---------------------------------------------------------
$error_type
=
substr
(
$rule
->
error
,
7
);
return
(
is_null
(
$rule
->
$error_type
))
?
Lang
::
line
(
'validation.'
.
$rule
->
error
)
->
get
()
:
$rule
->
$error_type
;
}
/**
* Get the error message for an Upload_Of rule.
*
* @param Rule $rule
* @return string
*/
private
static
function
get_upload_of_message
(
$rule
)
{
// ---------------------------------------------------------
// Upload_Of rules sometimes set a "presence_of" error.
//
// This occurs when the uploaded file didn't exist and the
// "not_required" method was not called.
// ---------------------------------------------------------
if
(
$rule
->
error
==
'presence_of'
)
{
return
static
::
get_message
(
$rule
);
}
// ---------------------------------------------------------
// Slice "file_" off of the error type.
// ---------------------------------------------------------
$error_type
=
substr
(
$rule
->
error
,
5
);
return
(
is_null
(
$rule
->
$error_type
))
?
Lang
::
line
(
'validation.'
.
$rule
->
error
)
->
get
()
:
$rule
->
$error_type
;
}
/**
* Prepare an error message for display. All place-holders will be replaced
* with their actual values.
*
* @param Rule $rule
* @param string $attribute
* @param string $message
* @return string
*/
private
static
function
prepare
(
$rule
,
$attribute
,
$message
)
{
// ---------------------------------------------------------
// The rangable rule messages have three place-holders that
// must be replaced.
//
// :max = The maximum size of the attribute.
// :min = The minimum size of the attribute.
// :size = The exact size the attribute must be.
// ---------------------------------------------------------
if
(
$rule
instanceof
Rangable_Rule
)
{
$message
=
str_replace
(
':max'
,
$rule
->
maximum
,
$message
);
$message
=
str_replace
(
':min'
,
$rule
->
minimum
,
$message
);
$message
=
str_replace
(
':size'
,
$rule
->
size
,
$message
);
}
// ---------------------------------------------------------
// The Upload_Of rule message have two place-holders taht
// must be replaced.
//
// :max = The maximum file size of the upload (kilobytes).
// :types = The allowed file types for the upload.
// ---------------------------------------------------------
elseif
(
$rule
instanceof
Rules\Upload_Of
)
{
$message
=
str_replace
(
':max'
,
$rule
->
maximum
,
$message
);
if
(
is_array
(
$rule
->
types
))
{
$message
=
str_replace
(
':types'
,
implode
(
', '
,
$rule
->
types
),
$message
);
}
}
return
str_replace
(
':attribute'
,
Lang
::
line
(
'attributes.'
.
$attribute
)
->
get
(
str_replace
(
'_'
,
' '
,
$attribute
)),
$message
);
}
}
\ No newline at end of file
system/validation/nullable_rule.php
deleted
100644 → 0
View file @
bbc45f92
<?php
namespace
System\Validation
;
use
System\Str
;
abstract
class
Nullable_Rule
extends
Rule
{
/**
* Indicates an empty value should be considered valid.
*
* @var bool
*/
public
$allow_empty
=
false
;
/**
* Indicates null should be considered valid.
*
* @var bool
*/
public
$allow_null
=
false
;
/**
* Evaluate the validity of an attribute.
*
* If this method returns a value, the child class will return it
* as the result of the validation. Otherwise, the child class will
* continue validating as normal.
*
* @param string $attribute
* @param array $attributes
* @return mixed
*/
public
function
check
(
$attribute
,
$attributes
)
{
// -------------------------------------------------------------
// If the attribute doesn't exist, the child's validation
// check will be be halted, and a presence_of error will be
// raised if null is not allowed.
// -------------------------------------------------------------
if
(
!
array_key_exists
(
$attribute
,
$attributes
))
{
if
(
!
$this
->
allow_null
)
{
$this
->
error
=
'presence_of'
;
}
return
is_null
(
$this
->
error
);
}
// -------------------------------------------------------------
// Make sure the attribute is not an empty string. An error
// will be raised if the attribute is empty and empty strings
// are not allowed, halting the child's validation.
// -------------------------------------------------------------
elseif
(
Str
::
length
((
string
)
$attributes
[
$attribute
])
==
0
and
!
$this
->
allow_empty
)
{
$this
->
error
=
'presence_of'
;
return
false
;
}
}
/**
* Allow a empty and null to be considered valid.
*
* @return Nullable_Rule
*/
public
function
not_required
()
{
return
$this
->
allow_empty
()
->
allow_null
();
}
/**
* Allow empty to be considered valid.
*
* @return Nullable_Rule
*/
public
function
allow_empty
()
{
$this
->
allow_empty
=
true
;
return
$this
;
}
/**
* Allow null to be considered valid.
*
* @return Nullable_Rule
*/
public
function
allow_null
()
{
$this
->
allow_null
=
true
;
return
$this
;
}
}
\ No newline at end of file
system/validation/rangable_rule.php
deleted
100644 → 0
View file @
bbc45f92
<?php
namespace
System\Validation
;
abstract
class
Rangable_Rule
extends
Nullable_Rule
{
/**
* The exact size the attribute must be.
*
* @var int
*/
public
$size
;
/**
* The maximum size of the attribute.
*
* @var int
*/
public
$maximum
;
/**
* The minimum size of the attribute.
*
* @var int
*/
public
$minimum
;
/**
* The "wrong size" error message.
*
* @var string
*/
public
$wrong_size
;
/**
* The "too big" error message.
*
* @var string
*/
public
$too_big
;
/**
* The "too small" error message.
*
* @var string
*/
public
$too_small
;
/**
* Set the exact size the attribute must be.
*
* @param int $size
* @return Rangable_Rule
*/
public
function
is
(
$size
)
{
$this
->
size
=
$size
;
return
$this
;
}
/**
* Set the minimum and maximum size of the attribute.
*
* @param int $minimum
* @param int $maximum
* @return Rangable_Rule
*/
public
function
between
(
$minimum
,
$maximum
)
{
$this
->
minimum
=
$minimum
;
$this
->
maximum
=
$maximum
;
return
$this
;
}
/**
* Set the minimum size the attribute.
*
* @param int $minimum
* @return Rangable_Rule
*/
public
function
minimum
(
$minimum
)
{
$this
->
minimum
=
$minimum
;
return
$this
;
}
/**
* Set the maximum size the attribute.
*
* @param int $maximum
* @return Rangable_Rule
*/
public
function
maximum
(
$maximum
)
{
$this
->
maximum
=
$maximum
;
return
$this
;
}
/**
* Set the validation error message.
*
* @param string $message
* @return Rangable_Rule
*/
public
function
message
(
$message
)
{
return
$this
->
wrong_size
(
$message
)
->
too_big
(
$message
)
->
too_small
(
$message
);
}
/**
* Set the "wrong size" error message.
*
* @param string $message
* @return Rangable_Rule
*/
public
function
wrong_size
(
$message
)
{
$this
->
wrong_size
=
$message
;
return
$this
;
}
/**
* Set the "too big" error message.
*
* @param string $message
* @return Rangable_Rule
*/
public
function
too_big
(
$message
)
{
$this
->
too_big
=
$message
;
return
$this
;
}
/**
* Set the "too small" error message.
*
* @param string $message
* @return Rangable_Rule
*/
public
function
too_small
(
$message
)
{
$this
->
too_small
=
$message
;
return
$this
;
}
}
\ No newline at end of file
system/validation/rule.php
deleted
100644 → 0
View file @
bbc45f92
<?php
namespace
System\Validation
;
use
System\Lang
;
abstract
class
Rule
{
/**
* The attributes being validated by the rule.
*
* @var array
*/
public
$attributes
;
/**
* The validation error message.
*
* @var string
*/
public
$message
;
/**
* The error type. This is used for rules that have more than
* one type of error such as Size_Of and Upload_Of.
*
* @var string
*/
public
$error
;
/**
* Create a new validation Rule instance.
*
* @param array $attributes
* @return void
*/
public
function
__construct
(
$attributes
)
{
$this
->
attributes
=
$attributes
;
}
/**
* Run the validation rule.
*
* @param array $attributes
* @param Error_Collector $errors
* @return void
*/
public
function
validate
(
$attributes
,
$errors
)
{
foreach
(
$this
->
attributes
as
$attribute
)
{
$this
->
error
=
null
;
if
(
!
$this
->
check
(
$attribute
,
$attributes
))
{
$errors
->
add
(
$attribute
,
Message
::
get
(
$this
,
$attribute
));
}
}
}
/**
* Set the validation error message.
*
* @param string $message
* @return Rule
*/
public
function
message
(
$message
)
{
$this
->
message
=
$message
;
return
$this
;
}
}
\ No newline at end of file
system/validation/rules/acceptance_of.php
deleted
100644 → 0
View file @
bbc45f92
<?php
namespace
System\Validation\Rules
;
use
System\Input
;
use
System\Validation\Rule
;
class
Acceptance_Of
extends
Rule
{
/**
* The value is that is considered accepted.
*
* @var string
*/
public
$accepts
=
'1'
;
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return bool
*/
public
function
check
(
$attribute
,
$attributes
)
{
return
Input
::
has
(
$attribute
)
and
(
string
)
Input
::
get
(
$attribute
)
===
$this
->
accepts
;
}
/**
* Set the accepted value.
*
* @param string $value
* @return Acceptance_Of
*/
public
function
accepts
(
$value
)
{
$this
->
accepts
=
$value
;
return
$this
;
}
}
\ No newline at end of file
system/validation/rules/confirmation_of.php
deleted
100644 → 0
View file @
bbc45f92
<?php
namespace
System\Validation\Rules
;
use
System\Input
;
use
System\Validation\Rule
;
class
Confirmation_Of
extends
Rule
{
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return bool
*/
public
function
check
(
$attribute
,
$attributes
)
{
if
(
!
array_key_exists
(
$attribute
,
$attributes
))
{
return
true
;
}
return
Input
::
has
(
$attribute
.
'_confirmation'
)
and
$attributes
[
$attribute
]
===
Input
::
get
(
$attribute
.
'_confirmation'
);
}
}
\ No newline at end of file
system/validation/rules/exclusion_of.php
deleted
100644 → 0
View file @
bbc45f92
<?php
namespace
System\Validation\Rules
;
use
System\Validation\Nullable_Rule
;
class
Exclusion_Of
extends
Nullable_Rule
{
/**
* The reserved values for the attribute.
*
* @var string
*/
public
$reserved
;
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return bool
*/
public
function
check
(
$attribute
,
$attributes
)
{
if
(
!
is_null
(
$nullable
=
parent
::
check
(
$attribute
,
$attributes
)))
{
return
$nullable
;
}
return
!
in_array
(
$attributes
[
$attribute
],
$this
->
reserved
);
}
/**
* Set the reserved values for the attribute
*
* @param array $reserved
* @return Exclusion_Of
*/
public
function
from
(
$reserved
)
{
$this
->
reserved
=
$reserved
;
return
$this
;
}
}
\ No newline at end of file
system/validation/rules/format_of.php
deleted
100644 → 0
View file @
bbc45f92
<?php
namespace
System\Validation\Rules
;
use
System\Validation\Nullable_Rule
;
class
Format_Of
extends
Nullable_Rule
{
/**
* The regular expression that will be used to validate the attribute.
*
* @var string
*/
public
$expression
;
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return bool
*/
public
function
check
(
$attribute
,
$attributes
)
{
if
(
!
is_null
(
$nullable
=
parent
::
check
(
$attribute
,
$attributes
)))
{
return
$nullable
;
}
return
preg_match
(
$this
->
expression
,
$attributes
[
$attribute
]);
}
/**
* Set the regular expression.
*
* @param string $expression
* @return Format_Of
*/
public
function
using
(
$expression
)
{
$this
->
expression
=
$expression
;
return
$this
;
}
}
\ No newline at end of file
system/validation/rules/inclusion_of.php
deleted
100644 → 0
View file @
bbc45f92
<?php
namespace
System\Validation\Rules
;
use
System\Validation\Nullable_Rule
;
class
Inclusion_Of
extends
Nullable_Rule
{
/**
* The accepted values for the attribute.
*
* @var string
*/
public
$accepted
;
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return bool
*/
public
function
check
(
$attribute
,
$attributes
)
{
if
(
!
is_null
(
$nullable
=
parent
::
check
(
$attribute
,
$attributes
)))
{
return
$nullable
;
}
return
in_array
(
$attributes
[
$attribute
],
$this
->
accepted
);
}
/**
* Set the accepted values for the attribute.
*
* @param array $accepted
* @return Inclusion_Of
*/
public
function
in
(
$accepted
)
{
$this
->
accepted
=
$accepted
;
return
$this
;
}
}
\ No newline at end of file
system/validation/rules/length_of.php
deleted
100644 → 0
View file @
bbc45f92
<?php
namespace
System\Validation\Rules
;
use
System\Str
;
use
System\Validation\Rangable_Rule
;
class
Length_Of
extends
Rangable_Rule
{
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return bool
*/
public
function
check
(
$attribute
,
$attributes
)
{
if
(
!
is_null
(
$nullable
=
parent
::
check
(
$attribute
,
$attributes
)))
{
return
$nullable
;
}
$value
=
trim
((
string
)
$attributes
[
$attribute
]);
// ---------------------------------------------------------
// Validate the exact length of the attribute.
// ---------------------------------------------------------
if
(
!
is_null
(
$this
->
size
)
and
Str
::
length
(
$value
)
!==
$this
->
size
)
{
$this
->
error
=
'string_wrong_size'
;
}
// ---------------------------------------------------------
// Validate the maximum length of the attribute.
// ---------------------------------------------------------
elseif
(
!
is_null
(
$this
->
maximum
)
and
Str
::
length
(
$value
)
>
$this
->
maximum
)
{
$this
->
error
=
'string_too_big'
;
}
// ---------------------------------------------------------
// Validate the minimum length of the attribute.
// ---------------------------------------------------------
elseif
(
!
is_null
(
$this
->
minimum
)
and
Str
::
length
(
$value
)
<
$this
->
minimum
)
{
$this
->
error
=
'string_too_small'
;
}
return
is_null
(
$this
->
error
);
}
}
\ No newline at end of file
system/validation/rules/numericality_of.php
deleted
100644 → 0
View file @
bbc45f92
<?php
namespace
System\Validation\Rules
;
use
System\Validation\Rangable_Rule
;
class
Numericality_Of
extends
Rangable_Rule
{
/**
* Indicates that the attribute must be an integer.
*
* @var bool
*/
public
$only_integer
=
false
;
/**
* The "not valid" error message.
*
* @var string
*/
public
$not_valid
;
/**
* The "not integer" error message.
*
* @var string
*/
public
$not_integer
;
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return bool
*/
public
function
check
(
$attribute
,
$attributes
)
{
if
(
!
is_null
(
$nullable
=
parent
::
check
(
$attribute
,
$attributes
)))
{
return
$nullable
;
}
// ---------------------------------------------------------
// Validate the attribute is a number.
// ---------------------------------------------------------
if
(
!
is_numeric
(
$attributes
[
$attribute
]))
{
$this
->
error
=
'number_not_valid'
;
}
// ---------------------------------------------------------
// Validate the attribute is an integer.
// ---------------------------------------------------------
elseif
(
$this
->
only_integer
and
filter_var
(
$attributes
[
$attribute
],
FILTER_VALIDATE_INT
)
===
false
)
{
$this
->
error
=
'number_not_integer'
;
}
// ---------------------------------------------------------
// Validate the exact size of the attribute.
// ---------------------------------------------------------
elseif
(
!
is_null
(
$this
->
size
)
and
$attributes
[
$attribute
]
!=
$this
->
size
)
{
$this
->
error
=
'number_wrong_size'
;
}
// ---------------------------------------------------------
// Validate the maximum size of the attribute.
// ---------------------------------------------------------
elseif
(
!
is_null
(
$this
->
maximum
)
and
$attributes
[
$attribute
]
>
$this
->
maximum
)
{
$this
->
error
=
'number_too_big'
;
}
// ---------------------------------------------------------
// Validate the minimum size of the attribute.
// ---------------------------------------------------------
elseif
(
!
is_null
(
$this
->
minimum
)
and
$attributes
[
$attribute
]
<
$this
->
minimum
)
{
$this
->
error
=
'number_too_small'
;
}
return
is_null
(
$this
->
error
);
}
/**
* Specify that the attribute must be an integer.
*
* @return Numericality_Of
*/
public
function
only_integer
()
{
$this
->
only_integer
=
true
;
return
$this
;
}
/**
* Set the "not valid" error message.
*
* @param string $message
* @return Numericality_Of
*/
public
function
not_valid
(
$message
)
{
$this
->
not_valid
=
$message
;
return
$this
;
}
/**
* Set the "not integer" error message.
*
* @param string $message
* @return Numericality_Of
*/
public
function
not_integer
(
$message
)
{
$this
->
not_integer
=
$message
;
return
$this
;
}
}
\ No newline at end of file
system/validation/rules/presence_of.php
deleted
100644 → 0
View file @
bbc45f92
<?php
namespace
System\Validation\Rules
;
use
System\Validation\Nullable_Rule
;
class
Presence_Of
extends
Nullable_Rule
{
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return bool
*/
public
function
check
(
$attribute
,
$attributes
)
{
if
(
!
is_null
(
$nullable
=
parent
::
check
(
$attribute
,
$attributes
)))
{
return
$nullable
;
}
// ---------------------------------------------------------
// The Nullable_Rule check method essentially is a check for
// the presence of an attribute, so there is no further
// checking that needs to be done.
// ---------------------------------------------------------
return
true
;
}
}
\ No newline at end of file
system/validation/rules/uniqueness_of.php
deleted
100644 → 0
View file @
bbc45f92
<?php
namespace
System\Validation\Rules
;
use
System\DB
;
use
System\Validation\Nullable_Rule
;
class
Uniqueness_Of
extends
Nullable_Rule
{
/**
* The database table that should be checked.
*
* @var string
*/
public
$table
;
/**
* The database column that should be checked.
*
* @var string
*/
public
$column
;
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return bool
*/
public
function
check
(
$attribute
,
$attributes
)
{
if
(
!
is_null
(
$nullable
=
parent
::
check
(
$attribute
,
$attributes
)))
{
return
$nullable
;
}
if
(
is_null
(
$this
->
column
))
{
$this
->
column
=
$attribute
;
}
return
DB
::
table
(
$this
->
table
)
->
where
(
$this
->
column
,
'='
,
$attributes
[
$attribute
])
->
count
()
==
0
;
}
/**
* Set the database table and column.
*
* The attribute name will be used as the column name if no other
* column name is specified.
*
* @param string $table
* @param string $column
* @return Uniqueness_Of
*/
public
function
on
(
$table
,
$column
=
null
)
{
$this
->
table
=
$table
;
$this
->
column
=
$column
;
return
$this
;
}
}
\ No newline at end of file
system/validation/rules/upload_of.php
deleted
100644 → 0
View file @
bbc45f92
<?php
namespace
System\Validation\Rules
;
use
System\File
;
use
System\Input
;
use
System\Validation\Nullable_Rule
;
class
Upload_Of
extends
Nullable_Rule
{
/**
* The acceptable file types.
*
* @var array
*/
public
$types
=
array
();
/**
* The maximum file size in bytes.
*
* @var int
*/
public
$maximum
;
/**
* The "wrong type" error message.
*
* @var string
*/
public
$wrong_type
;
/**
* The "too big" error message.
*
* @var string
*/
public
$too_big
;
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return bool
*/
public
function
check
(
$attribute
,
$attributes
)
{
// -----------------------------------------------------
// Check the presence of the upload. If the upload does
// not exist and the upload is required, a presence_of
// error will be raised.
//
// Otherwise no error will be raised.
// -----------------------------------------------------
if
(
!
array_key_exists
(
$attribute
,
Input
::
file
()))
{
if
(
!
$this
->
allow_null
)
{
$this
->
error
=
'presence_of'
;
}
return
is_null
(
$this
->
error
);
}
// -----------------------------------------------------
// Uploaded files are stored in the $_FILES array, so
// we use that array instead of the $attributes.
// -----------------------------------------------------
$file
=
Input
::
file
(
$attribute
);
if
(
!
is_null
(
$this
->
maximum
)
and
$file
[
'size'
]
>
$this
->
maximum
*
1000
)
{
$this
->
error
=
'file_too_big'
;
}
// -----------------------------------------------------
// The File::is method uses the Fileinfo PHP extension
// to determine the MIME type of the file.
// -----------------------------------------------------
foreach
(
$this
->
types
as
$type
)
{
if
(
File
::
is
(
$type
,
$file
[
'tmp_name'
]))
{
break
;
}
$this
->
error
=
'file_wrong_type'
;
}
return
is_null
(
$this
->
error
);
}
/**
* Set the acceptable file types.
*
* @return Upload_Of
*/
public
function
is
()
{
$this
->
types
=
func_get_args
();
return
$this
;
}
/**
* Require that the uploaded file is an image type.
*
* @return Upload_Of
*/
public
function
is_image
()
{
$this
->
types
=
array_merge
(
$this
->
types
,
array
(
'jpg'
,
'gif'
,
'png'
,
'bmp'
));
return
$this
;
}
/**
* Set the maximum file size in kilobytes.
*
* @param int $maximum
* @return Upload_Of
*/
public
function
maximum
(
$maximum
)
{
$this
->
maximum
=
$maximum
;
return
$this
;
}
/**
* Set the validation error message.
*
* @param string $message
* @return Upload_Of
*/
public
function
message
(
$message
)
{
return
$this
->
wrong_type
(
$message
)
->
too_big
(
$message
);
}
/**
* Set the "wrong type" error message.
*
* @param string $message
* @return Upload_Of
*/
public
function
wrong_type
(
$message
)
{
$this
->
wrong_type
=
$message
;
return
$this
;
}
/**
* Set the "too big" error message.
*
* @param string $message
* @return Upload_Of
*/
public
function
too_big
(
$message
)
{
$this
->
too_big
=
$message
;
return
$this
;
}
}
\ No newline at end of file
system/validation/rules/with_callback.php
deleted
100644 → 0
View file @
bbc45f92
<?php
namespace
System\Validation\Rules
;
use
System\Validation\Nullable_Rule
;
class
With_Callback
extends
Nullable_Rule
{
/**
* The callback that will be used to validate the attribute.
*
* @var function
*/
public
$callback
;
/**
* Evaluate the validity of an attribute.
*
* @param string $attribute
* @param array $attributes
* @return bool
*/
public
function
check
(
$attribute
,
$attributes
)
{
if
(
!
is_callable
(
$this
->
callback
))
{
throw
new
\Exception
(
"The validation callback for the [
$attribute
] attribute is not callable."
);
}
if
(
!
is_null
(
$nullable
=
parent
::
check
(
$attribute
,
$attributes
)))
{
return
$nullable
;
}
return
call_user_func
(
$this
->
callback
,
$attributes
[
$attribute
]);
}
/**
* Set the validation callback.
*
* @param function $callback
* @return With_Callback
*/
public
function
using
(
$callback
)
{
$this
->
callback
=
$callback
;
return
$this
;
}
}
\ No newline at end of file
system/validator.php
View file @
c3b8524e
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment