用户可以发布音乐链接,因为音乐提供者可以多种多样(Youtube、Deezer、Soundcloud 等),我有一个自定义验证器和一个自定义setLink()。在我的数据库中,链接是视频的 ID(youtube 的示例:CWMYxYoaRBQ)
但结果是我的 sfValidatorDoctrineUnique 不再起作用了。
我的验证器:
<?php
class videoValidator extends sfValidatorUrl
{
protected function configure($options = array(), $messages = array())
{
parent::configure($options, $messages);
$this->setMessage('invalid', 'Error message.');
}
protected function doClean($url)
{
$videoProvider = new videoProvider($url);
if (false === $videoProvider->isValid())
{
throw new sfValidatorError($this, 'invalid', array('value' => $url));
}
return $url;
}
}
?>
我的 videoProvider 类:
class videoProvider
{
private $url;
private $providers = array('youtube','deezer','soundcloud');
private $youtubePattern = '%^# Match any youtube URL
(?:https?://)? # Optional scheme. Either http or https
(?:www\.)? # Optional www subdomain
(?: # Group host alternatives
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com
(?: # Group path alternatives
/embed/ # Either /embed/
| /v/ # or /v/
| /watch\?v= # or /watch\?v=
) # End path alternatives.
) # End host alternatives.
([\w-]{10,12}) # Allow 10-12 for 11 char youtube id.
$%x';
private $deezerPattern = '/\d+/';
private $soundcloudPattern = '[\w-]+/[\w-]+$';
public function __construct($url)
{
$this->url = $url;
}
/**
* @return true / false
*/
private function checkYoutube()
{
return preg_match($this->youtubePattern, $this->url) ? true : false;
}
/**
* @return true / false
*/
private function checkDeezer()
{
// A Deezer URL has this format : http://www.deezer.com/track/61340079
return preg_match($this->deezerPattern, $this->url) ? true : false;
}
/**
* @return true / false
*/
private function checkSoundcloud()
{
// A Soundcloud URL has this format : http://soundcloud.com/[A-Z Artist]/[A-Z Title]
return preg_match($this->soundcloudPattern, $this->url) ? true : false;
}
/**
* @return true / false
*/
public function isValid()
{
// check all video provider as you do in your validator
// so it will return true if it find one, otherwise false
foreach ($this->providers as $provider)
{
$function = 'check'.ucfirst($provider);
if (true === $this->$function())
{
return true;
}
}
return false;
}
/**
* @return string
*/
public function getId()
{
if ($this->checkYoutube() && preg_match($this->youtubePattern, $this->url, $matches))
{
return $matches[1];
}
if ($this->checkDeezer() && preg_match($this->deezerPattern, $this->url, $matches))
{
return $matches[1];
}
if ($this->checkSoundcloud() && preg_match($this->deezerPattern, $this->url, $matches))
{
return $matches[1];
}
}
/**
* @return string
*/
public function getProvider()
{
if ($this->checkYoutube())
{
return 'youtube';
}
if ($this->checkDeezer())
{
return 'deezer';
}
if ($this->checkSoundcloud())
{
return 'soundcloud';
}
}
}
我的歌曲课:
class Song extends BaseSong
{
public function setLink($value)
{
// only perform this tweak if the value is a http link
if (preg_match('/^http/i', $value))
{
$videoProvider = new videoProvider($value);
// define url id
parent::_set('link', $videoProvider->getId());
// define type id
$provider = $videoProvider->getProvider();
$type = Doctrine_Core::getTable('Type')->findOneByName($provider);
parent::_set('type_id', $type->getId());
}
}
}
我认为 sfValidatorDoctrineUnique 尝试将完整的 URL(表单字段)与视频 ID(数据库字段)进行比较。我该如何解决?