<?php
/**
* $Id: Playlist.php 1631 2007-05-12 22:40:28Z matthieu $
*/
if (!class_exists('Validator_Playlist')) {
if (!defined('__CLASS_PATH__')) {
define('__CLASS_PATH__', realpath(diirname(__FILE__) . '/../'));
}
require_once __CLASS_PATH__ . '/Autoload.php';
/**
* validate a folder
* @author Matthieu MARY <hide@address.com>
* @licence GPL Gnu Public Licence
*/
class Validator_Playlist extends Validator_Base {
/**
* @access private
* @var array $_expectedAudioExtensions : the expected extensions
*/
private $_expectedAudioExtensions = array (
'mp3'
);
/**
* @access private
* @var array $_filesFound : the files found in folder
*/
private $_filesFound = array ();
/**
* @access private
* @var string $_folderPath : the folder to analyse
*/
private $_folderPath = '.';
/**
* constructor
* @param string $playlist : the playlist to analyse
* @return void
* @access public
* @see Audio_Playlist
* @throws Exception
*/
public function __construct(Audio_Playlist $playlist) {
parent :: __construct();
if (!is_dir($playlist->getFoldername())) {
throw new Exception($playlist->getFoldername() . ' is not a valid folder');
}
$this->_folderPath = $playlist->getFoldername();
$this->_expectedAudioExtensions = $playlist->getExpectedAudioFilesExtensions();
$this->_filesFound = $playlist->getFilesForPlaylist();
$this->validate();
}
/**
* @access private
* @return void
*/
private function validate() {
if ($this->validateHaveFiles()) {
$this->validateFiles();
}
}
/**
* check if their is some files in folder
* @access private
* @return boolean
*/
private function validateHaveFiles() {
if (count($this->_filesFound) == 0) {
$this->setError("Folder " . $this->_folderPath . ' is empty');
}
return $this->isValid();
}
/**
*
*/
private function validateFilenamePattern($filename) {
$basename = basename($filename);
if (!preg_match("/^[0-9]+\\s*(-\.)?\\s*.+\.(" . implode("|", $this->_expectedAudioExtensions) . ")$/", $basename)) {
$this->setError('Folder ' . $this->_folderPath . ' contains file with wrong filename (' . $basename . ')');
}
}
/**
* validate that a file is not empty
* @access private
* @return void
* @param string $filename : the filepath
*/
private function validateNotEmptyFile($filename) {
if (filesize($filename) == 0) {
$this->setError('Folder ' . $this->_folderPath . ' contains empty file (' . basename($filename) . ')');
}
}
/**
* validate that a file have the right extension
* @access private
* @return void
* @param string $filename : the filepath
*/
private function validateExtension($filename) {
$basename = basename($filename);
$extension = substr($basename, strrpos($basename, '.') + 1);
if (!in_array($extension, $this->_expectedAudioExtensions)) {
$this->setError('Folder ' . $this->_folderPath . ' have unexpected extensions file (' . $basename . ')');
}
}
/**
* validate encoding path
* @access private
* @return void
* @param string $filename : the filepath
*/
private function validateEncoding($filename) {
if (utf8_decode($filename) != $filename) {
$this->setError('Folder ' . $this->_folderPath . ' contains wrong encoding (' . $filename . ')');
}
}
/**
* validate expected extensions
* @acces private
* @return void
*/
private function validateFiles() {
foreach ($this->_filesFound as $currentFile) {
$this->validateNotEmptyFile($currentFile);
$this->validateExtension($currentFile);
$this->validateFilenamePattern($currentFile);
$this->validateEncoding($currentFile);
}
}
}
}