Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Nayjest committed Feb 25, 2016
1 parent 31f10ce commit cc98934
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vendor
composer.phar
composer.lock
apigen.phar
phpunit.phar
build
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
# eloquent-data-processing
# ViewComponents\EloquentDataProcessing
Eloquent ORM support for ViewComponents

## Installation

The recommended way of installing the component is through [Composer](https://getcomposer.org).

Run following command:

```bash
composer require view-components/eloquent-data-processing
```

## Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

## Testing

Execute phpunit from package folder.

```bash
phpunit
```
Package dependencies must be installed via composer (run 'composer install').

## License

© 2015 — 2016 Vitalii Stepanenko

Licensed under the MIT License.

Please see [License File](LICENSE) for more information.
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "view-components/eloquent-data-processing",
"description": "Eloquent ORM support for ViewComponents",
"keywords": [
"laravel",
"laravel-5",
"laravel5",
"laravel4",
"laravel-4"
],
"homepage": "https://github.com/view-components/eloquent-data-processing",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Vitalii [Nayjest] Stepanenko",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
"view-components/view-components": "*"
},
"autoload": {
"psr-4": {
"ViewComponents\\Eloquent\\": "src/"
}
},
"support": {
"email": "[email protected]",
"source": "https://github.com/view-components/eloquent-data-processing",
"issues": "https://github.com/view-components/eloquent-data-processing/issues"
},
"minimum-stability": "dev"
}
26 changes: 26 additions & 0 deletions src/EloquentDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ViewComponents\Eloquent;

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder;
use ViewComponents\ViewComponents\Data\AbstractDataProvider;
use ViewComponents\ViewComponents\Data\Operation\OperationInterface;

class EloquentDataProvider extends AbstractDataProvider
{
/**
*
* @param Builder|EloquentBuilder $src
* @param OperationInterface[] $operations
*/
public function __construct($src, array $operations = [])
{
$this->operations()->set($operations);
$this->processingService = new EloquentProcessingService(
new EloquentProcessorResolver(),
$this->operations(),
$src
);
}
}
49 changes: 49 additions & 0 deletions src/EloquentProcessingService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace ViewComponents\Eloquent;

use ArrayIterator;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder;
use RuntimeException;
use Traversable;
use ViewComponents\ViewComponents\Data\ProcessingService\AbstractProcessingService;

/**
* Class DbTableProcessingManager.
*/
class EloquentProcessingService extends AbstractProcessingService
{
/**
* @param Builder|EloquentBuilder $data
* @return Traversable
*/
protected function afterOperations($data)
{
if ($data instanceof EloquentBuilder) {
return $data->get();
} elseif ($data instanceof Builder) {
return new ArrayIterator($data->get());
}
throw new RuntimeException('Unsupported type of data source.');
}

/**
* @param Builder|EloquentBuilder $data
* @return Builder|EloquentBuilder
*/
protected function beforeOperations($data)
{
return clone $data;
}

/**
* @return int
*/
public function count()
{
return $this->applyOperations(
$this->beforeOperations($this->dataSource)
)->count();
}
}
23 changes: 23 additions & 0 deletions src/EloquentProcessorResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace ViewComponents\Eloquent;

use ViewComponents\Eloquent\Processor\FilterProcessor;
use ViewComponents\Eloquent\Processor\PaginateProcessor;
use ViewComponents\Eloquent\Processor\SortProcessor;
use ViewComponents\ViewComponents\Data\Operation\FilterOperation;
use ViewComponents\ViewComponents\Data\Operation\PaginateOperation;
use ViewComponents\ViewComponents\Data\Operation\SortOperation;
use ViewComponents\ViewComponents\Data\ProcessorResolver\ProcessorResolver;

class EloquentProcessorResolver extends ProcessorResolver
{
public function __construct()
{
$this
->register(SortOperation::class, SortProcessor::class)
->register(FilterOperation::class, FilterProcessor::class)
->register(PaginateOperation::class, PaginateProcessor::class)
;
}
}
25 changes: 25 additions & 0 deletions src/Processor/FilterProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace ViewComponents\Eloquent\Processor;

use Illuminate\Database\Eloquent\Builder;
use Presentation\Framework\Data\Operation\FilterOperation;
use Presentation\Framework\Data\Operation\OperationInterface;
use Presentation\Framework\Data\Processor\ProcessorInterface;

class FilterProcessor implements ProcessorInterface
{
/**
* @param Builder $src
* @param OperationInterface|FilterOperation $operation
* @return mixed
*/
public function process($src, OperationInterface $operation)
{
$value = $operation->getValue();
$operator = $operation->getOperator();
$field = $operation->getField();
$src->where($field, $operator, $value);
return $src;
}
}
25 changes: 25 additions & 0 deletions src/Processor/PaginateProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace ViewComponents\Eloquent\Processor;

use Illuminate\Database\Eloquent\Builder;
use ViewComponents\ViewComponents\Data\Operation\OperationInterface;
use ViewComponents\ViewComponents\Data\Operation\PaginateOperation;
use ViewComponents\ViewComponents\Data\Processor\AbstractPaginateProcessor;

class PaginateProcessor extends AbstractPaginateProcessor
{
/**
* @param Builder $src
* @param OperationInterface|PaginateOperation $operation
* @return mixed
*/
public function process($src, OperationInterface $operation)
{

$src->getQuery()
->limit($operation->getPageSize())
->offset($this->getOffset($operation));
return $src;
}
}
24 changes: 24 additions & 0 deletions src/Processor/SortProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace ViewComponents\Eloquent\Processor;

use Illuminate\Database\Eloquent\Builder;
use ViewComponents\ViewComponents\Data\Operation\OperationInterface;
use ViewComponents\ViewComponents\Data\Operation\SortOperation;
use ViewComponents\ViewComponents\Data\Processor\ProcessorInterface;

class SortProcessor implements ProcessorInterface
{
/**
* @param Builder $src
* @param OperationInterface|SortOperation $operation
* @return mixed
*/
public function process($src, OperationInterface $operation)
{
$field = $operation->getField();
$order = $operation->getOrder();
$src->orderBy($field, $order);
return $src;
}
}

0 comments on commit cc98934

Please sign in to comment.