继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Validator.php-9

跃然一笑
关注TA
已关注
手记 267
粉丝 40
获赞 164

/**

     * 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 <fabien@symfony.com> 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.


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP