Skip to content

Commit

Permalink
Moved LocationFilter, Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thedava committed Jul 15, 2017
1 parent 99b1fd4 commit 4c796da
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/Xspf/File/Type/HtmlFileType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function fromStructure(Structure $structure)

foreach ($structure->getTracks() as $track) {
$result[] = $t . $t . $t . '<div class="track">';
foreach ($track->toArray(true) as $key => $value) {
foreach ($track->toArray() as $key => $value) {
$result[] = $t . $t . $t . $t . '<div class="' . $key . '">' . $value . '</div>';
}
$result[] = $t . $t . $t . '</div>';
Expand Down
12 changes: 8 additions & 4 deletions src/Xspf/File/Type/XspfFileType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Xspf\File\Type;

use Xspf\File\Structure;
use Xspf\LocationFilter;
use Xspf\Filter\FileUrlFilter;
use Xspf\Filter\LocalFileFilter;
use Xspf\Track;

class XspfFileType extends AbstractFileType
Expand Down Expand Up @@ -32,9 +33,9 @@ protected function toStructure($data)
$tracks = [];
foreach ($xml->{'trackList'}->{'track'} as $track) {
if (!isset($track->{'location'})) {
$trackObj = new Track(LocationFilter::filter((string)$track));
$trackObj = new Track(LocalFileFilter::filter((string)$track));
} else {
$trackObj = new Track(LocationFilter::filter((string)$track->{'location'}));
$trackObj = new Track(LocalFileFilter::filter((string)$track->{'location'}));

if (isset($track->{'duration'})) {
$trackObj->setDuration((int)(string)$track->{'duration'});
Expand Down Expand Up @@ -64,7 +65,10 @@ protected function fromStructure(Structure $structure)
foreach ($structure->getTracks() as $track) {
$eTrack = $trackList->addChild('track');

foreach ($track->toArray(true) as $key => $value) {
foreach ($track->toArray() as $key => $value) {
if ($key === 'location') {
$value = FileUrlFilter::filter($value);
}
$eTrack->addChild($key, htmlspecialchars($value));
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/Xspf/Filter/FileUrlFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Xspf\Filter;

class FileUrlFilter
{
/**
* @param string $location
* @param string $directorySeparator
*
* @return string
*/
public static function filter($location, $directorySeparator = DIRECTORY_SEPARATOR)
{
$parts = explode($directorySeparator, $location);
list($first) = $parts;
$parts = array_map('rawurlencode', $parts);

// Check if windows
if (preg_match('/^[A-Z]\:$/', $first)) {
$parts[0] = '/' . $first;
} elseif ($first == '' && isset($parts[1], $parts[2]) && $parts[1] == 'cygdrive' && preg_match('/^[a-z]$/', $parts[2])) {
$parts[0] = '/' . strtoupper($parts[2]) . ':';
unset($parts[1], $parts[2]);
}

return 'file://' . implode('/', $parts);
}
}
29 changes: 29 additions & 0 deletions src/Xspf/Filter/LocalFileFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Xspf\Filter;

class LocalFileFilter
{
/**
* @param string $location
* @param string $directorySeparator
*
* @return string
*/
public static function filter($location, $directorySeparator = DIRECTORY_SEPARATOR)
{
// file-"protocol"
$scheme = 'file://';
if (strpos($location, $scheme) === 0) {
$location = urldecode($location);
$location = str_replace(['/', '\\', $directorySeparator], $directorySeparator, substr($location, strlen($scheme)));
}

// Windows path
if (strpos($location, '\\') === 0) {
$location = substr($location, 1);
}

return $location;
}
}
22 changes: 0 additions & 22 deletions src/Xspf/LocationFilter.php

This file was deleted.

28 changes: 2 additions & 26 deletions src/Xspf/Track.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,41 +38,17 @@ public function __construct($location)
}

/**
* @param bool $useFileUrl
*
* @return array
*/
public function toArray($useFileUrl = false)
public function toArray()
{
$result = [];
$result['location'] = ($useFileUrl) ? $this->getFileUrl() : $this->getLocation();
$result['location'] = $this->getLocation();
$this->getDuration() && $result['duration'] = (int)$this->getDuration();

return $result;
}

/**
* @param string $directorySeparator
*
* @return string
*/
public function getFileUrl($directorySeparator = DIRECTORY_SEPARATOR)
{
$parts = explode($directorySeparator, $this->getLocation());
list($first) = $parts;
$parts = array_map('rawurlencode', $parts);

// Check if windows
if (preg_match('/^[A-Z]\:$/', $first)) {
$parts[0] = '/' . $first;
} elseif ($first == '' && isset($parts[1], $parts[2]) && $parts[1] == 'cygdrive' && preg_match('/^[a-z]$/', $parts[2])) {
$parts[0] = '/' . strtoupper($parts[2]) . ':';
unset($parts[1], $parts[2]);
}

return 'file://' . implode('/', $parts);
}

public function update()
{
$result = self::getId3()->analyze($this->getLocation());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace XspfTest;
namespace XspfTest\Filter;

use Xspf\Track;
use Xspf\Filter\FileUrlFilter;

class TrackTest extends \PHPUnit_Framework_TestCase
class FileUrlFilterTest extends \PHPUnit_Framework_TestCase
{
public function fileUrlDataProvider()
{
Expand All @@ -29,14 +29,12 @@ public function fileUrlDataProvider()
/**
* @dataProvider fileUrlDataProvider
*
* @param string $filePath
* @param string $localFile
* @param string $directorySeparator
* @param string $expectedFileUrl
*/
public function testGetFileUrl($filePath, $directorySeparator, $expectedFileUrl)
public function testFilter($localFile, $directorySeparator, $expectedFileUrl)
{
$track = new Track($filePath);

$this->assertEquals($expectedFileUrl, $track->getFileUrl($directorySeparator));
$this->assertEquals($expectedFileUrl, FileUrlFilter::filter($localFile, $directorySeparator));
}
}
35 changes: 35 additions & 0 deletions test/XspfTest/Filter/LocalFileFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace XspfTest\Filter;

use Xspf\Filter\LocalFileFilter;

class LocalFileFilterTest extends \PHPUnit_Framework_TestCase
{
public function localFileDataProvider()
{
return [
// Windows
['file:///C:/Video/video.mp4', '\\', 'C:\Video\video.mp4'],
['file:///C:/Video/spaces%20video.mp4', '\\', 'C:\Video\spaces video.mp4'],
['file:///C:/Video/%28strange%29%20symbols%20video.mp4', '\\', 'C:\Video\(strange) symbols video.mp4'],

// Linux
['file:///home/user/video/video.mp4', '/', '/home/user/video/video.mp4'],
['file:///home/user/video/spaces%20video.mp4', '/', '/home/user/video/spaces video.mp4'],
['file:///home/user/video/%28strange%29%20symbols%20video.mp4', '/', '/home/user/video/(strange) symbols video.mp4'],
];
}

/**
* @dataProvider localFileDataProvider
*
* @param string $fileUrl
* @param string $directorySeparator
* @param string $expectedLocalFile
*/
public function testFilter($fileUrl, $directorySeparator, $expectedLocalFile)
{
$this->assertEquals($expectedLocalFile, LocalFileFilter::filter($fileUrl, $directorySeparator));
}
}
1 change: 0 additions & 1 deletion test/XspfTest/XspfSchemeValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace XspfTest;


use Xspf\XspfSchemeValidator;

class XspfSchemeValidatorTest extends \PHPUnit_Framework_TestCase
Expand Down

0 comments on commit 4c796da

Please sign in to comment.