-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
381 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
<?php | ||
|
||
namespace Spec\Adapters; | ||
|
||
use DataTables\Adapters\ArrayAdapter; | ||
use DataTables\ParamsParser; | ||
use Phalcon\Mvc\Model\Query; | ||
|
||
describe("Array", function() { | ||
|
||
beforeEach(function() { | ||
|
||
$di = \Phalcon\DI::getDefault(); | ||
|
||
$query = $di->get('modelsManager')->createQuery("SELECT * FROM \Spec\Models\User")->execute(); | ||
$this->array = $query->toArray(); | ||
|
||
}); | ||
|
||
it("should work withot any filter", function() { | ||
|
||
$dataTables = new ArrayAdapter(20); | ||
$dataTables->setArray($this->array); | ||
$dataTables->setParser(new ParamsParser(10)); | ||
|
||
$response = $dataTables->getResponse(); | ||
expect($dataTables->getParser())->toBeA('object'); | ||
expect(array_keys($response))->toBe(['draw', 'recordsTotal', 'recordsFiltered', 'data']); | ||
expect($response['recordsTotal'])->toBe(100); | ||
expect($response['recordsFiltered'])->toBe(100); | ||
expect(count($response['data']))->toBe(10); | ||
|
||
foreach($response['data'] as $data) { | ||
expect(array_keys($data))->toBe(['id', 'name', 'email', 'balance', 'DT_RowId']); | ||
expect($data['DT_RowId'])->toBe($data['id']); | ||
} | ||
|
||
}); | ||
|
||
describe("Limit&Offset", function() { | ||
|
||
beforeEach(function() { | ||
|
||
$_GET = ['start' => 2, 'length' => 1]; | ||
|
||
}); | ||
|
||
it("should work with start&length", function() { | ||
|
||
$dataTables = new ArrayAdapter(20); | ||
$dataTables->setArray($this->array); | ||
$dataTables->setParser(new ParamsParser(10)); | ||
$response = $dataTables->getResponse(); | ||
expect(count($response['data']))->toBe(1); | ||
|
||
$dataOne = $response['data']; | ||
|
||
$_GET['start'] = 3; | ||
$dataTables = new ArrayAdapter(20); | ||
$dataTables->setArray($this->array); | ||
$dataTables->setParser(new ParamsParser(10)); | ||
$response = $dataTables->getResponse(); | ||
expect(count($response['data']))->toBe(1); | ||
expect($response['data'])->not->toBe($dataOne); | ||
|
||
}); | ||
|
||
it("should work with a filter", function() { | ||
|
||
$_GET['search'] = ['value' => 'kr']; | ||
$_GET['columns'] = [ | ||
[ | ||
'data' => 'name', | ||
'searchable' => "true" | ||
] | ||
]; | ||
|
||
$dataTables = new ArrayAdapter(20); | ||
$dataTables->setArray($this->array); | ||
$dataTables->setParser(new ParamsParser(10)); | ||
$dataTables->setColumns(['name']); | ||
$response = $dataTables->getResponse(); | ||
expect(count($response['data']))->toBe(1); | ||
|
||
}); | ||
|
||
afterEach(function() { | ||
|
||
unset($_GET); | ||
|
||
}); | ||
|
||
}); | ||
|
||
it("should work with a global search", function() { | ||
|
||
$_GET = [ | ||
'search' => ['value' => 'kr'], | ||
'columns' => [ | ||
[ | ||
'data' => 'name', | ||
'searchable' => "true" | ||
] | ||
] | ||
]; | ||
|
||
$dataTables = new ArrayAdapter(20); | ||
$dataTables->setArray($this->array); | ||
$dataTables->setColumns(['name', 'email']); | ||
$dataTables->setParser(new ParamsParser(10)); | ||
|
||
$response = $dataTables->getResponse(); | ||
expect(count($response['data']))->toBe(3); | ||
$names = array_reduce($response['data'], function($carry, $item) { | ||
$carry[] = $item['name']; | ||
return $carry; | ||
}); | ||
|
||
expect($names)->toBe(['krajcik.rylee', 'kraig.mann', 'kara.krajcik']); | ||
|
||
}); | ||
|
||
it("should work with a column search", function() { | ||
|
||
$_GET = [ | ||
'columns' => [ | ||
[ | ||
'data' => 'name', | ||
'searchable' => "true", | ||
'search' => [ | ||
'value' => 'be' | ||
] | ||
] | ||
] | ||
]; | ||
|
||
$dataTables = new ArrayAdapter(20); | ||
$dataTables->setArray($this->array); | ||
$dataTables->setColumns(['name', 'email']); | ||
$dataTables->setParser(new ParamsParser(10)); | ||
|
||
$response = $dataTables->getResponse(); | ||
expect(count($response['data']))->toBe(5); | ||
$names = array_reduce($response['data'], function($carry, $item) { | ||
$carry[] = $item['name']; | ||
return $carry; | ||
}); | ||
expect($names)->toBe(['beahan.abbigail', 'gebert', 'breitenberg.ted', 'marvin.maybelle', 'zabernathy']); | ||
|
||
}); | ||
|
||
it("should work with a column&global search", function() { | ||
|
||
$_GET = [ | ||
'columns' => [ | ||
[ | ||
'data' => 'name', | ||
'searchable' => "true", | ||
'search' => [ | ||
'value' => 'be' | ||
] | ||
], | ||
[ | ||
'data' => 'email', | ||
'searchable' => "true", | ||
'search' => [ | ||
'value' => "@gmail.com" | ||
] | ||
] | ||
] | ||
]; | ||
|
||
$dataTables = new ArrayAdapter(20); | ||
$dataTables->setArray($this->array); | ||
$dataTables->setColumns(['name', 'email']); | ||
$dataTables->setParser(new ParamsParser(10)); | ||
|
||
$response = $dataTables->getResponse(); | ||
expect(count($response['data']))->toBe(3); | ||
$names = array_reduce($response['data'], function($carry, $item) { | ||
$carry[] = $item['name']; | ||
return $carry; | ||
}); | ||
expect($names)->toBe(['gebert', 'marvin.maybelle', 'zabernathy']); | ||
|
||
}); | ||
|
||
it("should order", function() { | ||
|
||
$_GET = [ | ||
'columns' => [ | ||
[ | ||
'data' => 'name', | ||
'searchable' => "true", | ||
] | ||
], | ||
'order' => [ | ||
[ | ||
'column' => 'name', | ||
'dir' => 'desc' | ||
] | ||
] | ||
]; | ||
|
||
$dataTables = new ArrayAdapter(20); | ||
$dataTables->setArray($this->array); | ||
$dataTables->setColumns(['name', 'email']); | ||
$dataTables->setParser(new ParamsParser(10)); | ||
|
||
$response = $dataTables->getResponse(); | ||
expect(count($response['data']))->toBe(10); | ||
expect($response['data'][0]['name'])->toBe('zcremin'); | ||
|
||
}); | ||
|
||
it("should order asc", function() { | ||
|
||
$_GET = [ | ||
'columns' => [ | ||
[ | ||
'data' => 'name', | ||
'searchable' => "true", | ||
] | ||
], | ||
'order' => [ | ||
[ | ||
'column' => 'name', | ||
'dir' => 'asc' | ||
] | ||
] | ||
]; | ||
|
||
$dataTables = new ArrayAdapter(20); | ||
$dataTables->setArray($this->array); | ||
$dataTables->setColumns(['name', 'email']); | ||
$dataTables->setParser(new ParamsParser(10)); | ||
|
||
$response = $dataTables->getResponse(); | ||
expect(count($response['data']))->toBe(10); | ||
expect($response['data'][0]['name'])->toBe('adelia13'); | ||
|
||
}); | ||
|
||
afterEach(function() { | ||
|
||
unset($_GET); | ||
unset($_POST); | ||
unset($_REQUEST); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace DataTables\Adapters; | ||
|
||
class ArrayAdapter extends AdapterInterface { | ||
|
||
protected $array = []; | ||
protected $filter = []; | ||
protected $order = []; | ||
|
||
public function setArray(array $array) { | ||
$this->array = $array; | ||
} | ||
|
||
public function getResponse() { | ||
$limit = $this->parser->getLimit(); | ||
$offset = $this->parser->getOffset(); | ||
$total = count($this->array); | ||
|
||
$this->bind('global_search', function($column, $search) { | ||
$this->filter[$column][] = $search; | ||
}); | ||
|
||
$this->bind('column_search', function($column, $search) { | ||
$this->filter[$column][] = $search; | ||
}); | ||
|
||
$this->bind('order', function($order) { | ||
$this->order = $order; | ||
}); | ||
|
||
if(count($this->filter)) { | ||
$items = array_filter($this->array, function($item) { | ||
$check = true; | ||
|
||
foreach($this->filter as $column=>$filters) { | ||
foreach($filters as $search) { | ||
$check = (strpos($item[$column], $search) !== false); | ||
if (!$check) break 2; | ||
} | ||
} | ||
|
||
if ($check) { | ||
return $item; | ||
} | ||
}); | ||
} else { | ||
$items = $this->array; | ||
} | ||
|
||
$filtered = count($items); | ||
|
||
if ($this->order) { | ||
$args = []; | ||
|
||
foreach($this->order as $order) { | ||
$tmp = []; | ||
list($column, $dir) = explode(' ', $order); | ||
|
||
foreach($items as $key=>$item) { | ||
$tmp[$key] = $item[$column]; | ||
} | ||
|
||
$args[] = $tmp; | ||
$args[] = ($dir == 'desc') ? SORT_DESC : SORT_ASC; | ||
} | ||
|
||
|
||
$args[] = &$items; | ||
call_user_func_array('array_multisort', $args); | ||
} | ||
|
||
if ($offset > 1) { | ||
$items = array_slice($items, $offset); | ||
} | ||
|
||
if ($limit) { | ||
$items = array_slice($items, 0, $limit); | ||
} | ||
|
||
return $this->formResponse([ | ||
'total' => (int)$total, | ||
'filtered' => (int)$filtered, | ||
'data' => $items, | ||
]); | ||
} | ||
|
||
} |
Oops, something went wrong.