Skip to content

Commit cc98934

Browse files
committed
Initial commit
1 parent 31f10ce commit cc98934

File tree

9 files changed

+245
-1
lines changed

9 files changed

+245
-1
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vendor
2+
composer.phar
3+
composer.lock
4+
apigen.phar
5+
phpunit.phar
6+
build

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,33 @@
1-
# eloquent-data-processing
1+
# ViewComponents\EloquentDataProcessing
22
Eloquent ORM support for ViewComponents
3+
4+
## Installation
5+
6+
The recommended way of installing the component is through [Composer](https://getcomposer.org).
7+
8+
Run following command:
9+
10+
```bash
11+
composer require view-components/eloquent-data-processing
12+
```
13+
14+
## Security
15+
16+
If you discover any security related issues, please email mail@vitaliy.in instead of using the issue tracker.
17+
18+
## Testing
19+
20+
Execute phpunit from package folder.
21+
22+
```bash
23+
phpunit
24+
```
25+
Package dependencies must be installed via composer (run 'composer install').
26+
27+
## License
28+
29+
© 2015 — 2016 Vitalii Stepanenko
30+
31+
Licensed under the MIT License.
32+
33+
Please see [License File](LICENSE) for more information.

composer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "view-components/eloquent-data-processing",
3+
"description": "Eloquent ORM support for ViewComponents",
4+
"keywords": [
5+
"laravel",
6+
"laravel-5",
7+
"laravel5",
8+
"laravel4",
9+
"laravel-4"
10+
],
11+
"homepage": "https://github.com/view-components/eloquent-data-processing",
12+
"type": "library",
13+
"license": "MIT",
14+
"authors": [
15+
{
16+
"name": "Vitalii [Nayjest] Stepanenko",
17+
"email": "mail@vitaliy.in",
18+
"role": "Developer"
19+
}
20+
],
21+
"require": {
22+
"view-components/view-components": "*"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"ViewComponents\\Eloquent\\": "src/"
27+
}
28+
},
29+
"support": {
30+
"email": "mail@vitaliy.in",
31+
"source": "https://github.com/view-components/eloquent-data-processing",
32+
"issues": "https://github.com/view-components/eloquent-data-processing/issues"
33+
},
34+
"minimum-stability": "dev"
35+
}

src/EloquentDataProvider.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace ViewComponents\Eloquent;
4+
5+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6+
use Illuminate\Database\Query\Builder;
7+
use ViewComponents\ViewComponents\Data\AbstractDataProvider;
8+
use ViewComponents\ViewComponents\Data\Operation\OperationInterface;
9+
10+
class EloquentDataProvider extends AbstractDataProvider
11+
{
12+
/**
13+
*
14+
* @param Builder|EloquentBuilder $src
15+
* @param OperationInterface[] $operations
16+
*/
17+
public function __construct($src, array $operations = [])
18+
{
19+
$this->operations()->set($operations);
20+
$this->processingService = new EloquentProcessingService(
21+
new EloquentProcessorResolver(),
22+
$this->operations(),
23+
$src
24+
);
25+
}
26+
}

src/EloquentProcessingService.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace ViewComponents\Eloquent;
4+
5+
use ArrayIterator;
6+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
7+
use Illuminate\Database\Query\Builder;
8+
use RuntimeException;
9+
use Traversable;
10+
use ViewComponents\ViewComponents\Data\ProcessingService\AbstractProcessingService;
11+
12+
/**
13+
* Class DbTableProcessingManager.
14+
*/
15+
class EloquentProcessingService extends AbstractProcessingService
16+
{
17+
/**
18+
* @param Builder|EloquentBuilder $data
19+
* @return Traversable
20+
*/
21+
protected function afterOperations($data)
22+
{
23+
if ($data instanceof EloquentBuilder) {
24+
return $data->get();
25+
} elseif ($data instanceof Builder) {
26+
return new ArrayIterator($data->get());
27+
}
28+
throw new RuntimeException('Unsupported type of data source.');
29+
}
30+
31+
/**
32+
* @param Builder|EloquentBuilder $data
33+
* @return Builder|EloquentBuilder
34+
*/
35+
protected function beforeOperations($data)
36+
{
37+
return clone $data;
38+
}
39+
40+
/**
41+
* @return int
42+
*/
43+
public function count()
44+
{
45+
return $this->applyOperations(
46+
$this->beforeOperations($this->dataSource)
47+
)->count();
48+
}
49+
}

src/EloquentProcessorResolver.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace ViewComponents\Eloquent;
4+
5+
use ViewComponents\Eloquent\Processor\FilterProcessor;
6+
use ViewComponents\Eloquent\Processor\PaginateProcessor;
7+
use ViewComponents\Eloquent\Processor\SortProcessor;
8+
use ViewComponents\ViewComponents\Data\Operation\FilterOperation;
9+
use ViewComponents\ViewComponents\Data\Operation\PaginateOperation;
10+
use ViewComponents\ViewComponents\Data\Operation\SortOperation;
11+
use ViewComponents\ViewComponents\Data\ProcessorResolver\ProcessorResolver;
12+
13+
class EloquentProcessorResolver extends ProcessorResolver
14+
{
15+
public function __construct()
16+
{
17+
$this
18+
->register(SortOperation::class, SortProcessor::class)
19+
->register(FilterOperation::class, FilterProcessor::class)
20+
->register(PaginateOperation::class, PaginateProcessor::class)
21+
;
22+
}
23+
}

src/Processor/FilterProcessor.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace ViewComponents\Eloquent\Processor;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Presentation\Framework\Data\Operation\FilterOperation;
7+
use Presentation\Framework\Data\Operation\OperationInterface;
8+
use Presentation\Framework\Data\Processor\ProcessorInterface;
9+
10+
class FilterProcessor implements ProcessorInterface
11+
{
12+
/**
13+
* @param Builder $src
14+
* @param OperationInterface|FilterOperation $operation
15+
* @return mixed
16+
*/
17+
public function process($src, OperationInterface $operation)
18+
{
19+
$value = $operation->getValue();
20+
$operator = $operation->getOperator();
21+
$field = $operation->getField();
22+
$src->where($field, $operator, $value);
23+
return $src;
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace ViewComponents\Eloquent\Processor;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use ViewComponents\ViewComponents\Data\Operation\OperationInterface;
7+
use ViewComponents\ViewComponents\Data\Operation\PaginateOperation;
8+
use ViewComponents\ViewComponents\Data\Processor\AbstractPaginateProcessor;
9+
10+
class PaginateProcessor extends AbstractPaginateProcessor
11+
{
12+
/**
13+
* @param Builder $src
14+
* @param OperationInterface|PaginateOperation $operation
15+
* @return mixed
16+
*/
17+
public function process($src, OperationInterface $operation)
18+
{
19+
20+
$src->getQuery()
21+
->limit($operation->getPageSize())
22+
->offset($this->getOffset($operation));
23+
return $src;
24+
}
25+
}

src/Processor/SortProcessor.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace ViewComponents\Eloquent\Processor;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use ViewComponents\ViewComponents\Data\Operation\OperationInterface;
7+
use ViewComponents\ViewComponents\Data\Operation\SortOperation;
8+
use ViewComponents\ViewComponents\Data\Processor\ProcessorInterface;
9+
10+
class SortProcessor implements ProcessorInterface
11+
{
12+
/**
13+
* @param Builder $src
14+
* @param OperationInterface|SortOperation $operation
15+
* @return mixed
16+
*/
17+
public function process($src, OperationInterface $operation)
18+
{
19+
$field = $operation->getField();
20+
$order = $operation->getOrder();
21+
$src->orderBy($field, $order);
22+
return $src;
23+
}
24+
}

0 commit comments

Comments
 (0)