Skip to content

Commit

Permalink
added file extension of SplFileObject
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Jan 11, 2024
1 parent 370f848 commit 7bc59af
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 0 deletions.
102 changes: 102 additions & 0 deletions src/PhPease/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php declare(strict_types=1);

/*
* This file is part of the PhPease package.
*
* (c) Timo Reith <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PhPease;

class File extends \SplFileObject
{
private $mimeType;

public function getMimetype()
{
if ($this->mimeType === null && class_exists('finfo')) {
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$this->mimeType = $finfo->file($this->getPathname());
}

return $this->mimeType;
}

/**
* @return bool
*/
public function isZipFile(): bool
{
if ($this->getMimetype() === 'application/zip' ||
strtolower($this->getExtension()) === 'zip') {
return true;
}

return false;
}

/**
* @return bool
*/
public function isGzipFile(): bool
{
if ($this->getMimetype() === 'application/gzip' ||
strtolower($this->getExtension()) === 'gz') {
return true;
}

return false;
}

/**
* @return bool
*/
public function isCsvFile(): bool
{
if ($this->getMimetype() === 'text/csv' ||
$this->getMimetype() === 'application/vnd.ms-excel' ||
strtolower($this->getExtension()) === 'csv') {
return true;
}

return false;
}

/**
* @return bool
*/
public function isXmlFile(): bool
{
if ($this->getMimetype() === 'text/xml' ||
$this->getMimetype() === 'application/xml' ||
strtolower($this->getExtension()) === 'xml') {
return true;
}

return false;
}

/**
* @return bool
*/
public function isTxtFile(): bool
{
if ($this->getMimetype() === 'text/plain' ||
strtolower($this->getExtension()) === 'txt') {
return true;
}

return false;
}

/**
* @return bool
*/
public function delete()
{
return unlink($this->getPathname());
}
}
71 changes: 71 additions & 0 deletions tests/FileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class FileTest extends TestCase
{
public function testTxtFile()
{
$filepath = __DIR__ . '/data/text.txt';

$file = new \PhPease\File($filepath);

$this->assertFalse($file->isCsvFile(), 'is csv file');
$this->assertFalse($file->isXmlFile(), 'is xml file');
$this->assertTrue($file->isTxtFile(), 'is txt file');
$this->assertFalse($file->isZipFile(), 'is zip file');
$this->assertFalse($file->isGzipFile(), 'is gzip file');
}

public function testCsvFile()
{
$filepath = __DIR__ . '/data/test.csv';

$file = new \PhPease\File($filepath);

$this->assertTrue($file->isCsvFile(), 'is csv file');
$this->assertFalse($file->isXmlFile(), 'is xml file');
$this->assertTrue($file->isTxtFile(), 'is txt file');
$this->assertFalse($file->isZipFile(), 'is zip file');
$this->assertFalse($file->isGzipFile(), 'is gzip file');
}

public function testXmlFile()
{
$filepath = __DIR__ . '/data/test.xml';

$file = new \PhPease\File($filepath);

$this->assertTrue($file->isXmlFile(), 'is xml file');
$this->assertFalse($file->isCsvFile(), 'is csv file');
$this->assertFalse($file->isTxtFile(), 'is txt file');
$this->assertFalse($file->isZipFile(), 'is zip file');
$this->assertFalse($file->isGzipFile(), 'is gzip file');
}

public function testZipFile()
{
$filepath = __DIR__ . '/data/text.txt.zip';

$file = new \PhPease\File($filepath);

$this->assertFalse($file->isXmlFile(), 'is xml file');
$this->assertFalse($file->isCsvFile(), 'is csv file');
$this->assertFalse($file->isTxtFile(), 'is txt file');
$this->assertTrue($file->isZipFile(), 'is zip file');
$this->assertFalse($file->isGzipFile(), 'is gzip file');
}

public function testGzFile()
{
$filepath = __DIR__ . '/data/text.txt.gz';

$file = new \PhPease\File($filepath);

$this->assertFalse($file->isXmlFile(), 'is xml file');
$this->assertFalse($file->isCsvFile(), 'is csv file');
$this->assertFalse($file->isTxtFile(), 'is txt file');
$this->assertFalse($file->isZipFile(), 'is zip file');
$this->assertTrue($file->isGzipFile(), 'is gzip file');
}
}
1 change: 1 addition & 0 deletions tests/data/test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo;bar
120 changes: 120 additions & 0 deletions tests/data/test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.</description>
</book>
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-09-10</publish_date>
<description>The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.</description>
</book>
<book id="bk106">
<author>Randall, Cynthia</author>
<title>Lover Birds</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-09-02</publish_date>
<description>When Carla meets Paul at an ornithology
conference, tempers fly as feathers get ruffled.</description>
</book>
<book id="bk107">
<author>Thurman, Paula</author>
<title>Splish Splash</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-11-02</publish_date>
<description>A deep sea diver finds true love twenty
thousand leagues beneath the sea.</description>
</book>
<book id="bk108">
<author>Knorr, Stefan</author>
<title>Creepy Crawlies</title>
<genre>Horror</genre>
<price>4.95</price>
<publish_date>2000-12-06</publish_date>
<description>An anthology of horror stories about roaches,
centipedes, scorpions and other insects.</description>
</book>
<book id="bk109">
<author>Kress, Peter</author>
<title>Paradox Lost</title>
<genre>Science Fiction</genre>
<price>6.95</price>
<publish_date>2000-11-02</publish_date>
<description>After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.</description>
</book>
<book id="bk110">
<author>O'Brien, Tim</author>
<title>Microsoft .NET: The Programming Bible</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-09</publish_date>
<description>Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.</description>
</book>
<book id="bk111">
<author>O'Brien, Tim</author>
<title>MSXML3: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-01</publish_date>
<description>The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.</description>
</book>
<book id="bk112">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
<description>Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.</description>
</book>
</catalog>
1 change: 1 addition & 0 deletions tests/data/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
Binary file added tests/data/text.txt.gz
Binary file not shown.
Binary file added tests/data/text.txt.zip
Binary file not shown.

0 comments on commit 7bc59af

Please sign in to comment.