Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit c7b426a

Browse files
Merge pull request #25 from assemblie/refactor-format-error-handling
Json format: refactor error handling + add JSON_UNESCAPED_UNICODE
2 parents 45907af + 3e2a65c commit c7b426a

File tree

11 files changed

+191
-42
lines changed

11 files changed

+191
-42
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ $database = new \Filebase\Database([
4545
]);
4646

4747
// in this example, you would search an exact user name
48-
// It would technically be stored as user_name.json in the directories
48+
// it would technically be stored as user_name.json in the directories
49+
// if user_name.json doesn't exists get will return new empty Document
4950
$item = $database->get('kingslayer');
5051

5152
// display property values

src/Database.php

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?php namespace Filebase;
22

33
use Exception;
4-
use Filebase\Config;
5-
use Filebase\Cache;
6-
use Filebase\Filesystem;
7-
use Filebase\Document;
8-
use Filebase\Backup;
4+
use Filebase\Format\EncodingException;
5+
use Filebase\Filesystem\SavingException;
6+
use Filebase\Filesystem\ReadingException;
7+
use Filebase\Filesystem\FilesystemException;
98

109
class Database
1110
{
@@ -31,9 +30,12 @@ class Database
3130

3231

3332
/**
34-
* __construct
35-
*
36-
*/
33+
* Database constructor.
34+
*
35+
* @param array $config
36+
*
37+
* @throws FilesystemException
38+
*/
3739
public function __construct(array $config = [])
3840
{
3941
$this->config = new Config($config);
@@ -46,12 +48,12 @@ public function __construct(array $config = [])
4648
{
4749
if (!@mkdir($this->config->dir, 0777, true))
4850
{
49-
throw new Exception(sprintf('`%s` doesn\'t exist and can\'t be created.', $this->config->dir));
51+
throw new FilesystemException(sprintf('`%s` doesn\'t exist and can\'t be created.', $this->config->dir));
5052
}
5153
}
5254
else if (!is_writable($this->config->dir))
5355
{
54-
throw new Exception(sprintf('`%s` is not writable.', $this->config->dir));
56+
throw new FilesystemException(sprintf('`%s` is not writable.', $this->config->dir));
5557
}
5658
}
5759

@@ -235,18 +237,16 @@ public function count()
235237

236238

237239
/**
238-
* save
239-
*
240-
* @param $document \Filebase\Document object
241-
* @param mixed $data should be an array, new data to replace all existing data within
242-
*
243-
* @return (bool) true or false if file was saved
244-
*/
240+
* @param Document $document
241+
* @param string $wdata
242+
* @return bool|Document
243+
* @throws SavingException
244+
*/
245245
public function save(Document $document, $wdata = '')
246246
{
247247
if ($this->config->read_only === true)
248248
{
249-
throw new Exception("This database is set to be read-only. No modifications can be made.");
249+
throw new SavingException("This database is set to be read-only. No modifications can be made.");
250250
}
251251

252252
$format = $this->config->format;
@@ -270,7 +270,12 @@ public function save(Document $document, $wdata = '')
270270

271271
$document->setUpdatedAt(time());
272272

273-
$data = $format::encode( $document->saveAs(), $this->config->pretty );
273+
try {
274+
$data = $format::encode( $document->saveAs(), $this->config->pretty );
275+
} catch (EncodingException $e) {
276+
// TODO: add logging
277+
throw new SavingException("Can not encode document.", 0, $e);
278+
}
274279

275280
if (Filesystem::write($file_location, $data))
276281
{
@@ -302,17 +307,30 @@ public function query()
302307
//--------------------------------------------------------------------
303308

304309

305-
306310
/**
307-
* read
308-
*
309-
* @param string $name
310-
* @return decoded file data
311-
*/
311+
* Read and return Document from filesystem by name.
312+
* If doesn't exists return new empty Document.
313+
*
314+
* @param $name
315+
*
316+
* @throws Exception|ReadingException
317+
* @return array|null
318+
*/
312319
protected function read($name)
313320
{
314321
$format = $this->config->format;
315-
return $format::decode( Filesystem::read( $this->config->dir.'/'.Filesystem::validateName($name, $this->config->safe_filename).'.'.$format::getFileExtension() ) );
322+
323+
$file = Filesystem::read(
324+
$this->config->dir . '/'
325+
. Filesystem::validateName($name, $this->config->safe_filename)
326+
. '.' . $format::getFileExtension()
327+
);
328+
329+
if ($file !== false) {
330+
return $format::decode($file);
331+
}
332+
333+
return null;
316334
}
317335

318336

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace Filebase\Filesystem;
2+
3+
class FilesystemException extends \Exception
4+
{
5+
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace Filebase\Filesystem;
2+
3+
class ReadingException extends FilesystemException
4+
{
5+
6+
}

src/Filesystem/SavingException.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace Filebase\Filesystem;
2+
3+
class SavingException extends FilesystemException
4+
{
5+
6+
}

src/Format/DecodingException.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace Filebase\Format;
2+
3+
class DecodingException extends FormatException
4+
{
5+
6+
}

src/Format/EncodingException.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace Filebase\Format;
2+
3+
class EncodingException extends FormatException
4+
{
5+
6+
}

src/Format/FormatException.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php namespace Filebase\Format;
2+
3+
class FormatException extends \Exception
4+
{
5+
private $inputData;
6+
7+
public function __construct($message, $code = 0, \Exception $previous = null, $inputData = null)
8+
{
9+
parent::__construct($message, $code, $previous);
10+
$this->inputData = $inputData;
11+
}
12+
13+
public function getInputData()
14+
{
15+
return $this->inputData;
16+
}
17+
}
18+

src/Format/Json.php

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33

44
class Json implements FormatInterface
55
{
6-
76
/**
8-
* getFileExtension
9-
*
10-
*/
7+
* @return string
8+
*/
119
public static function getFileExtension()
1210
{
1311
return 'json';
@@ -18,28 +16,54 @@ public static function getFileExtension()
1816

1917

2018
/**
21-
* encode
22-
*
23-
*/
19+
* @param array $data
20+
* @param bool $pretty
21+
* @return string
22+
* @throws FormatException
23+
*/
2424
public static function encode($data = [], $pretty = true)
2525
{
26-
$p = 1;
27-
if ($pretty==true) $p = JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES;
28-
29-
return json_encode($data, $p);
26+
$options = 0;
27+
if ($pretty == true) {
28+
$options = JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE;
29+
}
30+
31+
$encoded = json_encode($data, $options);
32+
if ($encoded === false) {
33+
throw new EncodingException(
34+
"json_encode: '" . json_last_error_msg() . "'",
35+
0,
36+
null,
37+
$data
38+
);
39+
}
40+
41+
return $encoded;
3042
}
3143

3244

3345
//--------------------------------------------------------------------
3446

3547

3648
/**
37-
* decode
38-
*
39-
*/
49+
* @param $data
50+
* @return mixed
51+
* @throws FormatException
52+
*/
4053
public static function decode($data)
4154
{
42-
return json_decode($data, 1);
55+
$decoded = json_decode($data, true);
56+
57+
if ($data !== false && $decoded === null) {
58+
throw new DecodingException(
59+
"json_decode: '" . json_last_error_msg() . "'",
60+
0,
61+
null,
62+
$data
63+
);
64+
}
65+
66+
return $decoded;
4367
}
4468

4569

tests/DatabaseTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php namespace Filebase;
22

33

4+
use Filebase\Filesystem\SavingException;
5+
46
class badformat {
57

68
}
@@ -241,4 +243,19 @@ public function testDatabaseFindAllDataOnly()
241243
$db->flush(true);
242244
}
243245

246+
public function testDatabaseSavingNotEncodableDocument()
247+
{
248+
$this->expectException(SavingException::class);
249+
250+
$db = new \Filebase\Database([
251+
'dir' => __DIR__.'/databases'
252+
]);
253+
254+
$doc = $db->get("testDatabaseSavingNotEncodableDocument");
255+
256+
// insert invalid utf-8 characters
257+
$doc->testProp = "\xB1\x31";
258+
259+
$doc->save();
260+
}
244261
}

0 commit comments

Comments
 (0)