-
Notifications
You must be signed in to change notification settings - Fork 2
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
9 changed files
with
245 additions
and
1 deletion.
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,6 @@ | ||
vendor | ||
composer.phar | ||
composer.lock | ||
apigen.phar | ||
phpunit.phar | ||
build |
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 |
---|---|---|
@@ -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. |
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,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" | ||
} |
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,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 | ||
); | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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,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) | ||
; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |