Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the best way is to perform the check twice:</p> <ul> <li>first time: using the validator, so you know it's one of these video provider and then return the video link (not the id)</li> <li>second time: redefine the <code>setLink()</code> so it takes the link, extract the id and save both the <code>link</code> and the <code>style_id</code></li> </ul> <p>How to do that.</p> <p>Create a custom lib, like <code>lib/videoProvider.class.php</code>. This is kind of prototyped class to valid &amp; retrieve id from a video provider. It, of course, needs improvements.</p> <pre><code>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-&gt;url = $url; } /** * @return true / false */ private function checkYoutube() { return preg_match($this-&gt;youtubePattern, $this-&gt;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-&gt;deezerPattern, $this-&gt;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-&gt;soundcloudPattern, $this-&gt;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-&gt;providers as $provider) { $function = 'check'.ucfirst($provider); if (true === $this-&gt;$function()) { return true; } } return false; } /** * @return string */ public function getId() { if ($this-&gt;checkYoutube() &amp;&amp; preg_match($this-&gt;youtubePattern, $this-&gt;url, $matches)) { return $matches[1]; } if ($this-&gt;checkDeezer() &amp;&amp; preg_match($this-&gt;deezerPattern, $this-&gt;url, $matches)) { return $matches[1]; } if ($this-&gt;checkSoundcloud() &amp;&amp; preg_match($this-&gt;deezerPattern, $this-&gt;url, $matches)) { return $matches[1]; } } /** * @return string */ public function getProvider() { if ($this-&gt;checkYoutube()) { return 'youtube'; } if ($this-&gt;checkDeezer()) { return 'deezer'; } if ($this-&gt;checkSoundcloud()) { return 'soundcloud'; } } } </code></pre> <p>Then in the <code>doClean</code> of your validator, you just need to call this class, like that:</p> <pre><code>$videoProvider = new videoProvider($url); if (false === $videoProvider-&gt;isValid()) { throw new sfValidatorError($this, 'invalid', array('value' =&gt; $url)); } return $url; </code></pre> <p>Finally, the <code>setLink</code> in Song.class.php should now be:</p> <pre><code>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-&gt;getId()); // define type id $provider = $videoProvider-&gt;getProvider(); $type = Doctrine_Core::getTable('Type')-&gt;findOneByName($provider); parent::_set('type_id', $type-&gt;getId()); } } </code></pre> <p>This is a first draft that must be tested and improved (test if getId() returns an id and not false, same for getProvider, etc ...)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload