Skip to content

Commit

Permalink
Merge pull request #2 from cfpinto/add-get-social-id-and-social-type
Browse files Browse the repository at this point in the history
Added post and profile validation
  • Loading branch information
cfpinto authored Feb 19, 2019
2 parents c90eff5 + 80549b5 commit 6d7390a
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 25 deletions.
54 changes: 43 additions & 11 deletions src/Validators/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ abstract class AbstractValidator implements ValidatorInterface
protected $urlValidator;

/**
* @var array|string
* @var array
*/
protected $patterns = '~will fail~';
protected $patterns = ['~will fail~'];

/**
* @var array
*/
protected $patternMaps = ['fail', 0];

/**
* AbstractValidator constructor.
Expand Down Expand Up @@ -51,21 +55,49 @@ public function normalizeUrl(string $url): string
* @return bool
*/
final public function isValid(string $url): bool
{
return $this->split($url)->isValid;
}

/**
* @param string $url
*
* @return \stdClass
*/
final public function split(string $url): \stdClass
{
if (empty($this->patterns)) {
throw new \RuntimeException('Invalid Regex validation pattern');
}

if (is_array($this->patterns)) {
foreach ($this->patterns as $pattern) {
if (preg_match($pattern, $url)) {
return true;
}
}
$result = new \stdClass();
$result->isValid = false;
$result->id = null;
$result->type = null;

$patterns = $this->patterns;
$patternMaps = $this->patternMaps;

if (!is_array($patterns)) {
$patterns = [$patterns];
$patternMaps = [$patternMaps];
}

return false;
} else {
return preg_match($this->patterns, $this->normalizeUrl($url));
foreach ($patterns as $idx => $pattern) {
if (preg_match($pattern, $url, $matches)) {
$result->isValid = true;
$result->id = $matches[$patternMaps[$idx]['id']];
$result->type = is_numeric($patternMaps[$idx]['type']) ?
$matches[$patternMaps[$idx]['type']] :
$patternMaps[$idx]['type'];

break;
}
}


dd($result);

return $result;
}
}
14 changes: 14 additions & 0 deletions src/Validators/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,19 @@ class Facebook extends AbstractValidator
'~facebook\.com/media/set/?\?set=(?:[^/ ]+)~i',
'~facebook\.com/(?:[^/]+)/videos/(?:[^/]+)/?~i',
'~facebook\.com/video\.php\?(?:id|v)=(?:[^ ]+)~i',
'~facebook\.com/pages/([^/]+)(.*)~',
'~facebook\.com/([^/]+)(.*)~',
];

protected $patternMaps = [
['type' => 2, 'id' => 1],
['type' => 'notes', 'id' => 3],
['type' => 1, 'id' => 3],
['type' => 1, 'id' => 2],
['type' => 'media_set', 'id' => 1],
['type' => 'videos', 'id' => 2],
['type' => 'videos', 'id' => 2],
['type' => 'pages', 'id' => 1],
['type' => 'profile', 'id' => 1],
];
}
12 changes: 11 additions & 1 deletion src/Validators/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@
class Instagram extends AbstractValidator
{
/** inline {@inheritdoc} */
protected $patterns = '~/p/([A-Za-z0-9-_]+)/?$~i';
protected $patterns = [
'~instagram.com/p/([A-Za-z0-9-_.]+)/?$~i',
'~instagram.com/pages/([A-Za-z0-9-_.]+)/?$~i',
'~instagram.com/([A-Za-z0-9-_.]+)/?$~i',
];

protected $patternMaps = [
['type' => 'post', 'id' => 1],
['type' => 'page', 'id' => 1],
['type' => 'page', 'id' => 1],
];

/** inline {@inheritdoc} */
public function normalizeUrl(string $url): string
Expand Down
19 changes: 9 additions & 10 deletions src/Validators/Twitch.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@

class Twitch extends AbstractValidator
{
protected $patterns = "~twitch\.tv/(videos)/([0-9]+)~i";

public function normalizeUrl(string $url): string
{
if (preg_match('~twitch\.tv/(videos)/([0-9]+)~i', $url, $matches)) {
return 'https://twitch.tv/' . $matches['1'] . '/' . $matches['2'];
}

return $url;
}
protected $patterns = [
"~twitch\.tv/videos/([0-9]+)~i",
"~twitch\.tv/([0-9]+)~i",
];

protected $patternMaps = [
['type' => 'video', 'id' => 1],
['type' => 'channel', 'id' => 1],
];
}
10 changes: 9 additions & 1 deletion src/Validators/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@
class Twitter extends AbstractValidator
{
/** inline {@inheritdoc} */
protected $patterns = '~twitter\.com/(?:[\w\d-_]+)/(?:status|moments)/(?:[0-9]+)~i';
protected $patterns = [
'~twitter\.com/([\w\d-_]+)/(status|moments)/([0-9]+)~i',
'~twitter\.com/([\w\d-_]+)~i',
];

protected $patternMaps = [
['type' => 2, 'id' => 2],
['type' => 'profile', 'id' => 1],
];

/** inline {@inheritdoc} */
public function normalizeUrl(string $url): string
Expand Down
1 change: 1 addition & 0 deletions src/Validators/ValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ interface ValidatorInterface
*/
public function isValid(string $url): bool;
public function normalizeUrl(string $url): string;
public function split(string $url): \stdClass;
}
14 changes: 12 additions & 2 deletions src/Validators/Youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ class Youtube extends AbstractValidator
{

/** inline {@inheritdoc} */
protected $patterns = '~v=(?:[a-z0-9_-]+)~i';
protected $patterns = [
'~v=(?:[a-z0-9_-]+)~i',
'~www.youtube.com/(channel|user)/([^/]+)~i'
];

protected $patternMaps = [
['type' => 'video', 'id' => 1],
['type' => 'channel', 'id' => 2],
];

/** inline {@inheritdoc} */
public function normalizeUrl(string $url):string
Expand All @@ -26,6 +34,8 @@ public function normalizeUrl(string $url):string
return 'http://www.youtube.com/watch?v=' . $matches[1];
}

return $url;
$parts = explode('?', $url);

return $parts[0];
}
}

0 comments on commit 6d7390a

Please sign in to comment.