Repository URL to install this package:
|
Version:
1.0.0 ▾
|
<?php
/**
* Created by PhpStorm.
* User: danilo
* Date: 10/09/15
* Time: 15:23
*/
namespace Modules\Core\Services;
use Illuminate\Validation\Validator;
use Carbon\Carbon;
class ValidatorService extends Validator
{
public function validateCheckUrl($attribute, $value, $parameters)
{
$url = $parameters[0];
//Your pattern, not pretending to be super precise, so it's up to you
$pattern = '/^((http|https)\:\/\/)?(www\.)?' . $url . '\..+$/';
if (preg_match($pattern, $value)) {
return true;
}
return false;
}
public function validateCnpj($attribute, $value, $parameters)
{
$value = preg_replace('/\D/', '', $value);
//Code ported from jsfromhell.com
$b = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
if (strlen($value) != 14) {
return false;
}
$calcMod = function ($value, $j = 11) use ($b) {
$i = $n = 0;
while ($i <= $j) {
$n += $value[$i] * $b[++$i];
}
if ($value[$j + 1] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
return false;
}
return true;
};
if (!$calcMod(11) || !$calcMod(12)) {
return false;
}
return true;
}
/**
* Valida Cpf.
* @param string $attribute
* @param string $value
* @param string $parameters
*
* @return bool
*/
public function validateCpf($attribute, $value, $parameters)
{
$value = preg_replace('/\D/', '', $value);
//Code ported from jsfromhell.com
if (strlen($value) != 11 || preg_match("/^{$value[0]}{11}$/", $value)) {
return false;
}
$calcMod = function ($value, $j = 10) {
$i = $n = 0;
while ($j >= 2) {
$n += $value[$i++] * $j--;
}
if ($value[$j - 1] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
return false;
}
return true;
};
if (!$calcMod(10) || $calcMod(11)) {
return false;
}
return true;
}
public function validateYoutube($attribute, $value, $parameters)
{
$pattern = '@^https?://(www\.)?(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^\"&?/]{11})@i';
return (preg_match($pattern, $value) > 0);
}
public function validateVimeo($attribute, $value, $parameters)
{
$pattern = '@^https?://(www\.)?(player\.)?(vimeo\.com/)((channels/[A-z]+/)|(groups/[A-z]+/videos/)|(video/))?([0-9]+)@i';
return (preg_match($pattern, $value) > 0);
}
}