Skip to content

Commit 63a27f5

Browse files
committed
fixed bug for file without extension
1 parent fcca49a commit 63a27f5

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

.editorconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ end_of_line = lf
1111
insert_final_newline = true
1212
trim_trailing_whitespace = true
1313

14+
1415
[*.md]
15-
trim_trailing_whitespace = false
16+
trim_trailing_whitespace = false

src/Converter.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ class Converter
2525
public function __construct(string $file, string $fileType = null)
2626
{
2727
try {
28-
$file = new File($file);
29-
} catch(\Exception $e) {
28+
$file = new File($file, $fileType);
29+
} catch (\Exception $e) {
3030
throw new ConverterException($e->getMessage(), 1);
3131
}
3232

33-
foreach(self::$converters as $converter) {
34-
if(!$converter::canHandleExtension($file->getExtension())) {
33+
foreach (self::$converters as $converter) {
34+
if (!$converter::canHandleExtension($file->getType())) {
3535
continue;
3636
}
3737

@@ -40,8 +40,8 @@ public function __construct(string $file, string $fileType = null)
4040
break;
4141
}
4242

43-
if($this->file === null) {
44-
throw new ConverterException('Can not handle file type '.$file->getExtension());
43+
if ($this->file === null) {
44+
throw new ConverterException('Can not handle file type '.$file->getType());
4545
}
4646
}
4747

@@ -101,7 +101,7 @@ public function save(string $path, string $extension = null): bool
101101
}
102102

103103
if (!$this->isConvertableTo($extension)) {
104-
throw new ConverterException("Invalid conversion. Can not convert ".$this->file->getExtension()." to ".$extension, 1);
104+
throw new ConverterException("Invalid conversion. Can not convert ".$this->file->getType()." to ".$extension, 1);
105105
}
106106

107107

@@ -122,7 +122,7 @@ public function save(string $path, string $extension = null): bool
122122
public function content(string $extension = null): string
123123
{
124124
if (!$this->isConvertableTo($extension)) {
125-
throw new ConverterException("Invalid conversion. Can not convert ".$this->file->getExtension()." to ".$extension, 1);
125+
throw new ConverterException("Invalid conversion. Can not convert ".$this->file->getType()." to ".$extension, 1);
126126
}
127127

128128
$tempDir = (new TemporaryDirectory(__DIR__))
@@ -143,6 +143,10 @@ public function content(string $extension = null): string
143143

144144
protected function getNewFilename(string $extension)
145145
{
146+
if ($this->file->getExtension() === null) {
147+
return $this->file->getName() . '.' . $extension;
148+
}
149+
146150
return str_replace($this->file->getExtension(), $extension, $this->file->getName());
147151
}
148152

src/File.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,32 @@ class File
77
protected $extension = null;
88
protected $directory = null;
99
protected $name = null;
10+
protected $type = null;
1011

11-
function __construct(string $file)
12+
public function __construct(string $file, string $type = null)
1213
{
13-
if(!file_exists($file)) {
14+
if (!file_exists($file)) {
1415
throw new \Exception("File '${file}'' not found!", 1);
1516
}
1617

1718
$this->original = $file;
19+
$pathInfo = pathinfo($file);
20+
$pathInfo['extension'] = isset($pathInfo['extension']) ? $pathInfo['extension'] : null;
1821
// using php 7.1 array destruction
1922
[
2023
'extension' => $this->extension,
2124
'dirname' => $this->directory,
2225
'basename' => $this->name
23-
] = pathinfo($file);
26+
] = $pathInfo;
27+
$this->type = $type ?? $pathInfo['extension'];
2428
}
2529

2630
public function getDirectory(): string
2731
{
2832
return $this->directory;
2933
}
3034

31-
public function getExtension(): string
35+
public function getExtension()
3236
{
3337
return $this->extension;
3438
}
@@ -38,6 +42,11 @@ public function getName(): string
3842
return $this->name;
3943
}
4044

45+
public function getType(): string
46+
{
47+
return $this->type;
48+
}
49+
4150
public function getOriginal(): string
4251
{
4352
return $this->original;

0 commit comments

Comments
 (0)