Skip to content

Commit a0dca76

Browse files
committed
beutify
1 parent 2fa33db commit a0dca76

10 files changed

Lines changed: 100 additions & 100 deletions

File tree

src/DataBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class DataBase
3333
public function __construct($pathToFile)
3434
{
3535
$this->dbFile = fopen($pathToFile, 'r+b');
36-
if(!$this->dbFile){
36+
if (!$this->dbFile) {
3737
throw new \RuntimeException("cannot open file $pathToFile");
3838
}
3939
flock($this->dbFile, LOCK_EX);

src/JsonServer.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class JsonServer
3535
*/
3636
public function __construct()
3737
{
38-
$this->jsonDb = new DataBase(__DIR__ .'/..'. Config::get('pathToDb'));
38+
$this->jsonDb = new DataBase(__DIR__ . '/..' . Config::get('pathToDb'));
3939
$this->response = new Response();
4040
$this->response->headers->set('Content-Type', 'application/vnd.api+json');
4141
}
@@ -74,7 +74,7 @@ public function handleRequest($method, $uri, $data = [])
7474
*/
7575
public function GET($tabName, $id, $parent, $data)
7676
{
77-
try{
77+
try {
7878
$result = $this->jsonDb->$tabName->filterByParent($parent);
7979
$result = $this->processFilters($result);
8080
//fetching single resource
@@ -88,8 +88,7 @@ public function GET($tabName, $id, $parent, $data)
8888
$this->response->headers->set('X-Total-Count', $result->count());
8989
$this->response->setStatusCode(200);
9090
}
91-
}
92-
catch(\OutOfRangeException $e){
91+
} catch (\OutOfRangeException $e) {
9392
return call_user_func(Config::get('resourceNotFound'), $this->response);
9493
}
9594
return $this->response;
@@ -151,7 +150,7 @@ public function DELETE($tabName, $id, $parent, $data)
151150
{
152151
if ($id) {
153152
$table = $this->jsonDb->$tabName;
154-
try{
153+
try {
155154
$table->delete($id);
156155
$this->jsonDb->save();
157156
$this->response->setContent('');

src/Row.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ public function setData($data)
9494
*/
9595
public function toArray()
9696
{
97-
if (Config::get('fieldsAutoSorting')){
98-
uksort ($this->fields, Config::get('fieldsAutoSortingFunc'));
97+
if (Config::get('fieldsAutoSorting')) {
98+
uksort($this->fields, Config::get('fieldsAutoSortingFunc'));
9999
}
100-
if($this->table){
100+
if ($this->table) {
101101
$this->embedResources($this->table->getEmbeds());
102102
}
103103
$result = $this->fields;
104-
foreach($this->embeddedResources as $resName=>$resource){
104+
foreach ($this->embeddedResources as $resName => $resource) {
105105
$result[$resName] = $resource->toArray();
106106
}
107107
return $result;
@@ -125,8 +125,8 @@ public function getContent()
125125
*/
126126
public function search($q)
127127
{
128-
foreach($this->fields as $field){
129-
if(strpos($field, $q) !== false){
128+
foreach ($this->fields as $field) {
129+
if (strpos($field, $q) !== false) {
130130
return true;
131131
}
132132
}
@@ -140,8 +140,8 @@ public function search($q)
140140
*/
141141
public function embedResources($resources)
142142
{
143-
if($resources){
144-
foreach($resources as $resource){
143+
if ($resources) {
144+
foreach ($resources as $resource) {
145145
$tabName = $this->table->getTabName();
146146
$this->embeddedResources[$resource] = $this->table->getDb()->$resource->filterByParent(['table' => $tabName, 'id' => $this->id]);
147147
}

src/Table.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function filterByParent($parent)
152152
public function insert($data, $parent = null)
153153
{
154154
$row = new Row($data, $this);
155-
if($parent){
155+
if ($parent) {
156156
$parentName = $this->getParentKeyName($parent['table']);
157157
$row->$parentName = $parent['id'];
158158
}
@@ -204,7 +204,7 @@ public function getNewId()
204204
foreach ($this->rows as $row) {
205205
$ids[] = $row->id;
206206
}
207-
if(count($ids) === 0){
207+
if (count($ids) === 0) {
208208
return 1;
209209
}
210210
sort($ids);
@@ -329,7 +329,7 @@ public function _sort($field)
329329
public function _order($order)
330330
{
331331
$order = mb_strtolower($order);
332-
if($order !== 'asc' && $order !== 'desc' ){
332+
if ($order !== 'asc' && $order !== 'desc') {
333333
throw new \DomainException ("Unknown sort type $order");
334334
}
335335
$this->sortOrder = $order;
@@ -344,7 +344,7 @@ public function _order($order)
344344
*/
345345
public function _start($start)
346346
{
347-
if( !is_numeric($start) || $start < 0){
347+
if (!is_numeric($start) || $start < 0) {
348348
throw new \DomainException ("Start '$start' must be a positive integer");
349349
}
350350
$this->start = $start;
@@ -359,7 +359,7 @@ public function _start($start)
359359
*/
360360
public function _end($end)
361361
{
362-
if( !is_numeric($end) || $end < 0){
362+
if (!is_numeric($end) || $end < 0) {
363363
throw new \DomainException ("end '$end'' must be a positive integer");
364364
}
365365
$this->end = $end;
@@ -374,7 +374,7 @@ public function _end($end)
374374
*/
375375
public function _limit($limit)
376376
{
377-
if( !is_numeric($limit) || $limit < 0){
377+
if (!is_numeric($limit) || $limit < 0) {
378378
throw new \DomainException ("limit '$limit'' must be a positive integer");
379379
}
380380
$this->limit = $limit;
@@ -431,18 +431,18 @@ private function sort()
431431
{
432432
$sortField = $this->sortField;
433433
$sortOrder = $this->sortOrder;
434-
$sortFunc = function($a, $b) use ( $sortField, $sortOrder ){
435-
if ($a->$sortField === $b->$sortField){
434+
$sortFunc = function ($a, $b) use ($sortField, $sortOrder) {
435+
if ($a->$sortField === $b->$sortField) {
436436
return 0;
437437
}
438-
if ($a->$sortField > $b->$sortField){
438+
if ($a->$sortField > $b->$sortField) {
439439
return $sortOrder === 'asc' ? 1 : -1;
440440
}
441-
if ($a->$sortField < $b->$sortField){
441+
if ($a->$sortField < $b->$sortField) {
442442
return $sortOrder === 'asc' ? -1 : 1;
443443
}
444444
};
445-
usort ($this->rows, $sortFunc);
445+
usort($this->rows, $sortFunc);
446446
}
447447

448448
/**
@@ -454,7 +454,7 @@ private function sort()
454454
private function filter($callback)
455455
{
456456
$table = clone $this;
457-
$table->rows = array_slice(array_filter($this->rows, $callback),0);
457+
$table->rows = array_slice(array_filter($this->rows, $callback), 0);
458458
return $table;
459459
}
460460

tests/DataBaseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class DataBaseTest extends PHPUnit_Framework_TestCase
77

88
protected function setUp()
99
{
10-
$this->fixture = new \JsonServer\DataBase(getcwd().'/tests/Mock/pluralDB.json');
10+
$this->fixture = new \JsonServer\DataBase(getcwd() . '/tests/Mock/pluralDB.json');
1111
}
1212

1313
protected function tearDown()

tests/Mock/pluralDB.json

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
{
2-
"posts": [
3-
{
4-
"id": 1,
5-
"title": "json-server",
6-
"author": "zlob"
7-
},
8-
{
9-
"id": 2,
10-
"title": "json-server",
11-
"author": "zlob"
12-
}
13-
],
14-
"comments": [
15-
{
16-
"id": 1,
17-
"body": "some comment",
18-
"post_id": 1
19-
}
20-
],
21-
"votes": [
22-
{
23-
"id": 1,
24-
"rating": 1,
25-
"comment_id": 1
26-
}
27-
]
2+
"posts": [
3+
{
4+
"id": 1,
5+
"title": "json-server",
6+
"author": "zlob"
7+
},
8+
{
9+
"id": 2,
10+
"title": "json-server",
11+
"author": "zlob"
12+
}
13+
],
14+
"comments": [
15+
{
16+
"id": 1,
17+
"body": "some comment",
18+
"post_id": 1
19+
}
20+
],
21+
"votes": [
22+
{
23+
"id": 1,
24+
"rating": 1,
25+
"comment_id": 1
26+
}
27+
]
2828
}

tests/Mock/singularDB.json

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
{
2-
"post": [
3-
{
4-
"id": 1,
5-
"title": "json-server",
6-
"author": "zlob"
7-
},
8-
{
9-
"id": 2,
10-
"title": "json-server",
11-
"author": "zlob"
12-
}
13-
],
14-
"comment": [
15-
{
16-
"id": 1,
17-
"body": "some comment",
18-
"post_id": 1
19-
}
20-
],
21-
"vote": [
22-
{
23-
"id": 1,
24-
"rating": 1,
25-
"comment_id": 1
26-
}
27-
]
2+
"post": [
3+
{
4+
"id": 1,
5+
"title": "json-server",
6+
"author": "zlob"
7+
},
8+
{
9+
"id": 2,
10+
"title": "json-server",
11+
"author": "zlob"
12+
}
13+
],
14+
"comment": [
15+
{
16+
"id": 1,
17+
"body": "some comment",
18+
"post_id": 1
19+
}
20+
],
21+
"vote": [
22+
{
23+
"id": 1,
24+
"rating": 1,
25+
"comment_id": 1
26+
}
27+
]
2828
}

tests/RowTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class RowTest extends PHPUnit_Framework_TestCase
77

88
protected function setUp()
99
{
10-
$this->fixture = new \JsonServer\Row(['id'=>0, 'field1' => 1, 'field2' => 'some string']);
10+
$this->fixture = new \JsonServer\Row(['id' => 0, 'field1' => 1, 'field2' => 'some string']);
1111
}
1212

1313
protected function tearDown()
@@ -28,17 +28,17 @@ public function testSetPassed()
2828

2929
public function testSetFields()
3030
{
31-
$data = ['id'=>77, 'field1' => 11, 'field2' => 22];
31+
$data = ['id' => 77, 'field1' => 11, 'field2' => 22];
3232
$this->fixture->setData($data);
33-
$dataExpext = ['id'=>0, 'field1' => 11, 'field2' => 22];
33+
$dataExpext = ['id' => 0, 'field1' => 11, 'field2' => 22];
3434
static::assertEquals($this->fixture->toArray(), $dataExpext, 'mass assignment trough setData is working');
3535
}
3636

3737
public function testSortingFields()
3838
{
39-
$data = ['field2' => 22, 'field1' => 11,'id'=>77 ];
39+
$data = ['field2' => 22, 'field1' => 11, 'id' => 77];
4040
$this->fixture->setData($data);
41-
$dataExpext = ['id'=>0, 'field1' => 11, 'field2' => 22];
41+
$dataExpext = ['id' => 0, 'field1' => 11, 'field2' => 22];
4242
static::assertEquals($this->fixture->toArray(), $dataExpext, 'fields sorting correctly');
4343
}
4444

tests/ServerTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ protected function tearDown()
1010
{
1111
$this->fixture->__destruct();
1212
}
13+
1314
/**
1415
* @dataProvider requestProviderSingular
1516
*/
16-
public function testRequestSingularSingular ($method, $url, $data, $expected, $msg)
17+
public function testRequestSingularSingular($method, $url, $data, $expected, $msg)
1718
{
1819
$config = \JsonServer\Config::getInstance();
1920
$config->set('urlNamingForm', 'singularize');
@@ -28,7 +29,7 @@ public function testRequestSingularSingular ($method, $url, $data, $expected, $m
2829
/**
2930
* @dataProvider requestProviderSingular
3031
*/
31-
public function testRequestSingularPlural ($method, $url, $data, $expected, $msg)
32+
public function testRequestSingularPlural($method, $url, $data, $expected, $msg)
3233
{
3334
$config = \JsonServer\Config::getInstance();
3435
$config->set('urlNamingForm', 'singularize');
@@ -53,7 +54,7 @@ public function requestProviderSingular()
5354
/**
5455
* @dataProvider requestProviderPlural
5556
*/
56-
public function testRequestPluralSingular ($method, $url, $data, $expected, $msg)
57+
public function testRequestPluralSingular($method, $url, $data, $expected, $msg)
5758
{
5859
$config = \JsonServer\Config::getInstance();
5960
$config->set('urlNamingForm', 'pluralize');
@@ -68,7 +69,7 @@ public function testRequestPluralSingular ($method, $url, $data, $expected, $msg
6869
/**
6970
* @dataProvider requestProviderPlural
7071
*/
71-
public function testRequestPluralPlural ($method, $url, $data, $expected, $msg)
72+
public function testRequestPluralPlural($method, $url, $data, $expected, $msg)
7273
{
7374
$config = \JsonServer\Config::getInstance();
7475
$config->set('urlNamingForm', 'pluralize');

0 commit comments

Comments
 (0)