php validator,Validator.php-9

/**

* Get the excluded ID column and value for the unique rule.

*

* @param  array  $parameters

* @return array

*/

protected function getUniqueIds($parameters)

{

$idColumn = isset($parameters[3]) ? $parameters[3] : 'id';//default is id

return [$idColumn, $parameters[2]];// return this id and other parameters

}// this is a protected function to  get the excluded ID column

// means these column not has ID

// get column and value for the unique rule.

/**

* Get the extra conditions for a unique rule.

*

* @param  array  $parameters

* @return array

*/

protected function getUniqueExtra($parameters)

{// get more conditions, like extra exists.

if (isset($parameters[4])) {// if has return this value

return $this->getExtraConditions(array_slice($parameters, 4));

}

//else return null

return [];

}//Get the extra conditions for a unique rule.

/**

* Validate the existence of an attribute value in a database table.

*

* @param  string  $attribute

* @param  mixed   $value

* @param  array   $parameters

* @return bool

*/

protected function validateExists($attribute, $value, $parameters)

{//Validate the existence of an attribute value in a database table

$this->requireParameterCount(1, $parameters, 'exists');// require Parameter Count

list($connection, $table) = $this->parseTable($parameters[0]);//list get this value

// The second parameter position holds the name of the column that should be

// verified as existing. If this parameter is not specified we will guess

// that the columns being "verified" shares the given attribute's name.

$column = isset($parameters[1]) ? $parameters[1] : $attribute;

// The second parameter position holds the name of the column that should be verified as existing.

// means this second parameter must be existing.

// if this parameter not exists, we will default think this column being "verified".

// and will shares the given attribute name

// get this column

$expected = (is_array($value)) ? count($value) : 1;// get the number

return $this->getExistCount($connection, $table, $column, $value, $parameters) >= $expected;

}// too simple and this type.

/**

* Get the number of records that exist in storage.

*

* @param  mixed   $connection

* @param  string  $table

* @param  string  $column

* @param  mixed   $value

* @param  array   $parameters

* @return int

*/

protected function getExistCount($connection, $table, $column, $value, $parameters)

{// Get the number of records that exist in storage.

//this storage maybe is mysql or redis.

$verifier = $this->getPresenceVerifier();//first get the verifier

$verifier->setConnection($connection);// set connection

$extra = $this->getExtraExistConditions($parameters);// get the extra condtion

if (is_array($value)) {// if has value

// return this value

return $verifier->getMultiCount($table, $column, $value, $extra);

}

// get Count

return $verifier->getCount($table, $column, $value, null, null, $extra);

}

/**

* Get the extra exist conditions.

*

* @param  array  $parameters

* @return array

*/

protected function getExtraExistConditions(array $parameters)

{// get Extra Conditions

return $this->getExtraConditions(array_values(array_slice($parameters, 2)));

}// Get the extra exist conditons.

/**

* Get the extra conditions for a unique / exists rule.

*

* @param  array  $segments

* @return array

*/

protected function getExtraConditions(array $segments)

{

$extra = [];// extra this storage

$count = count($segments);// get count number and segments

for ($i = 0; $i < $count; $i = $i + 2) {// loop this segments

$extra[$segments[$i]] = $segments[$i + 1];

}

return $extra;//return this result

}//Get the extra conditions for a unique /exists rule.

/**

* Validate that an attribute is a valid IP.

*

* @param  string  $attribute

* @param  mixed   $value

* @return bool

*/

protected function validateIp($attribute, $value)

{

return filter_var($value, FILTER_VALIDATE_IP) !== false;

}//validate that an attribute is a valid IP.

/**

* Validate that an attribute is a valid e-mail address.

*

* @param  string  $attribute

* @param  mixed   $value

* @return bool

*/

protected function validateEmail($attribute, $value)

{

return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;

}//validate that an attribute is a valid e-mail address

/**

* Validate that an attribute is a valid URL.

*

* @param  string  $attribute

* @param  mixed   $value

* @return bool

*/

protected function validateUrl($attribute, $value)

{// validate that an attribute is a valid URL

/*

* This pattern is derived from Symfony\Component\Validator\Constraints\UrlValidator (2.7.4)

* (c) Fabien Potencier http://symfony.com

*/

$pattern = '~^

((aaa|aaas|about|acap|acct|acr|adiumxtra|afp|afs|aim|apt|attachment|aw|barion|beshare|bitcoin|blob|bolo|callto|cap|chrome|chrome-extension|cid|coap|coaps|com-eventbrite-attendee|content|crid|cvs|data|dav|dict|dlna-playcontainer|dlna-playsingle|dns|dntp|dtn|dvb|ed2k|example|facetime|fax|feed|feedready|file|filesystem|finger|fish|ftp|geo|gg|git|gizmoproject|go|gopher|gtalk|h323|ham|hcp|http|https|iax|icap|icon|im|imap|info|iotdisco|ipn|ipp|ipps|irc|irc6|ircs|iris|iris.beep|iris.lwz|iris.xpc|iris.xpcs|itms|jabber|jar|jms|keyparc|lastfm|ldap|ldaps|magnet|mailserver|mailto|maps|market|message|mid|mms|modem|ms-help|ms-settings|ms-settings-airplanemode|ms-settings-bluetooth|ms-settings-camera|ms-settings-cellular|ms-settings-cloudstorage|ms-settings-emailandaccounts|ms-settings-language|ms-settings-location|ms-settings-lock|ms-settings-nfctransactions|ms-settings-notifications|ms-settings-power|ms-settings-privacy|ms-settings-proximity|ms-settings-screenrotation|ms-settings-wifi|ms-settings-workplace|msnim|msrp|msrps|mtqp|mumble|mupdate|mvn|news|nfs|ni|nih|nntp|notes|oid|opaquelocktoken|pack|palm|paparazzi|pkcs11|platform|pop|pres|prospero|proxy|psyc|query|redis|rediss|reload|res|resource|rmi|rsync|rtmfp|rtmp|rtsp|rtsps|rtspu|secondlife|service|session|sftp|sgn|shttp|sieve|sip|sips|skype|smb|sms|smtp|snews|snmp|soap.beep|soap.beeps|soldat|spotify|ssh|steam|stun|stuns|submit|svn|tag|teamspeak|tel|teliaeid|telnet|tftp|things|thismessage|tip|tn3270|turn|turns|tv|udp|unreal|urn|ut2004|vemmi|ventrilo|videotex|view-source|wais|webcal|ws|wss|wtai|wyciwyg|xcon|xcon-userid|xfire|xmlrpc\.beep|xmlrpc.beeps|xmpp|xri|ymsgr|z39\.50|z39\.50r|z39\.50s))://                                 # protocol

(([\pL\pN-]+:)?([\pL\pN-]+)@)?          # basic auth

(

([\pL\pN\pS-\.])+(\.?([\pL]|xn\-\-[\pL\pN-]+)+\.?) # a domain name

|                                              # or

\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}                 # a IP address

|                                              # or

\[

(?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,1}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){3})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,2}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){2})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,3}(?:(?:[0-9a-f]{1,4})))?::(?:(?:[0-9a-f]{1,4})):)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,4}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::))))

\]  # a IPv6 address

)

(:[0-9]+)?                              # a port (optional)

(/?|/\S+)                               # a /, nothing or a / with something

$~ixu';

return preg_match($pattern, $value) === 1;

}// a big power full rules.

/**

* Validate that an attribute is an active URL.

*

* @param  string  $attribute

* @param  mixed   $value

* @return bool

*/

protected function validateActiveUrl($attribute, $value)

{

if ($url = parse_url($value, PHP_URL_HOST)) {

return count(dns_get_record($url, DNS_A | DNS_AAAA)) > 0;

}// a super parse_url

return false;

}// validate that an attribute is an active URL

/**

* Validate the MIME type of a file is an image MIME type.

*

* @param  string  $attribute

* @param  mixed   $value

* @return bool

*/

protected function validateImage($attribute, $value)

{

return $this->validateMimes($attribute, $value, ['jpeg', 'png', 'gif', 'bmp', 'svg']);

}// validate the MIME type of a file is an images MIME type.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值