Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Nov 6, 2017
0 parents commit a6bfff9
Show file tree
Hide file tree
Showing 17 changed files with 1,755 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage_clover: build/logs/clover.xml
json_path: build/logs/coveralls-upload.json
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
composer.lock
/vendor/
/build/logs/
.php_cs.cache
34 changes: 34 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

return \PhpCsFixer\Config::create()->setRules([
'@Symfony' => true,
'align_multiline_comment' => ['comment_type' => 'phpdocs_like'],
'array_syntax' => ['syntax' => 'short'],
'blank_line_before_statement' => false,
'cast_spaces' => ['space' => 'none'],
'combine_consecutive_unsets' => true,
'concat_space' => ['spacing' => 'one'],
'heredoc_to_nowdoc' => true,
'list_syntax' => null,
'no_extra_consecutive_blank_lines' => ['tokens' => ['continue', 'parenthesis_brace_block', 'extra', 'return']],
'no_multiline_whitespace_before_semicolons' => true,
'no_null_property_initialization' => true,
'no_short_echo_tag' => false,
'no_superfluous_elseif' => true,
'no_unneeded_curly_braces' => true,
'no_unneeded_final_method' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'not_operator_with_space' => false,
'not_operator_with_successor_space' => false,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_annotation_without_dot' => null,
'phpdoc_no_alias_tag' => false,
'phpdoc_order' => true,
'phpdoc_separation' => false,
'phpdoc_summary' => null,
'phpdoc_types_order' => true,
'return_type_declaration' => true,
'void_return' => false,
'yoda_style' => false,
]);
26 changes: 26 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
checks:
php:
code_rating: true

filter:
excluded_paths:
- tests/*
- vendor/*

build:

environment:
php: '7.1'

dependencies:
before:
- composer install
- mkdir -p build/logs

tests:
override:
-
command: 'vendor/bin/phpunit --coverage-clover build/logs/clover.xml'
coverage:
file: 'build/logs/clover.xml'
format: 'clover'
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
language: php

matrix:
include:
- php: 5.6
env: PHPUNIT_VERSION=5.7
- php: 7.0
- php: 7.1

cache:
directories:
- ./vendor
- $HOME/.composer/cache

before_script:
- composer self-update
- if [ "$PHPUNIT_VERSION" != "" ]; then composer require "phpunit/phpunit:${PHPUNIT_VERSION}"; fi;
- mkdir -p build/logs

script:
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml

after_success:
- travis_retry php vendor/bin/coveralls
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 mpyw

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
281 changes: 281 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
<p align="center">
<img width="320" alt="lampager-idiorm" src="https://user-images.githubusercontent.com/1351893/32459320-948766b8-c372-11e7-84b9-061ffa6233ba.png">
</p>
<p align="center">
<a href="https://travis-ci.org/lampager/lampager-idiorm"><img src="https://travis-ci.org/lampager/lampager-idiorm.svg?branch=master" alt="Build Status"></a>
<a href="https://coveralls.io/github/lampager/lampager-idiorm?branch=master"><img src="https://coveralls.io/repos/github/lampager/lampager-idiorm/badge.svg?branch=master" alt="Coverage Status"></a>
<a href="https://scrutinizer-ci.com/g/lampager/lampager-idiorm/?branch=master"><img src="https://scrutinizer-ci.com/g/lampager/lampager-idiorm/badges/quality-score.png?b=master" alt="Scrutinizer Code Quality"></a>
</p>

# Lampager for Idiorm

Rapid pagination without using OFFSET

## Requirements

- PHP: ^5.6 || ^7.0
- idiorm: ^5.4
- [lampager/lampager](https://github.com/lampager/lampager): ^0.3

## Installing

## Basic Usage

You can wrap `ORM` instance with global function `lampager()` to make it chainable.

```php
$cursor = [
'id' => 3,
'created_at' => '2017-01-10 00:00:00',
'updated_at' => '2017-01-20 00:00:00',
];

$result = lampager(ORM::forTable('posts')->whereEqual('user_id', 1))
->forward()
->limit(5)
->orderByDesc('updated_at') // ORDER BY `updated_at` DESC, `created_at` DESC, `id` DESC
->orderByDesc('created_at')
->orderByDesc('id')
->seekable()
->paginate($cursor)
->toJson(JSON_PRETTY_PRINT);
```

It will run the optimized query.


```sql
(

SELECT * FROM `posts`
WHERE `user_id` = 1
AND (
`updated_at` = '2017-01-20 00:00:00' AND `created_at` = '2017-01-10 00:00:00' AND `id` > 3
OR
`updated_at` = '2017-01-20 00:00:00' AND `created_at` > '2017-01-10 00:00:00'
OR
`updated_at` > '2017-01-20 00:00:00'
)
ORDER BY `updated_at` ASC, `created_at` ASC, `id` ASC
LIMIT 1

) UNION ALL (

SELECT * FROM `posts`
WHERE `user_id` = 1
AND (
`updated_at` = '2017-01-20 00:00:00' AND `created_at` = '2017-01-10 00:00:00' AND `id` <= 3
OR
`updated_at` = '2017-01-20 00:00:00' AND `created_at` < '2017-01-10 00:00:00'
OR
`updated_at` < '2017-01-20 00:00:00'
)
ORDER BY `updated_at` DESC, `created_at` DESC, `id` DESC
LIMIT 6

)
```

And you'll get


```json
{
"records": [
{
"id": 3,
"user_id": 1,
"text": "foo",
"created_at": "2017-01-10 00:00:00",
"updated_at": "2017-01-20 00:00:00"
},
{
"id": 5,
"user_id": 1,
"text": "bar",
"created_at": "2017-01-05 00:00:00",
"updated_at": "2017-01-20 00:00:00"
},
{
"id": 4,
"user_id": 1,
"text": "baz",
"created_at": "2017-01-05 00:00:00",
"updated_at": "2017-01-20 00:00:00"
},
{
"id": 2,
"user_id": 1,
"text": "qux",
"created_at": "2017-01-17 00:00:00",
"updated_at": "2017-01-18 00:00:00"
},
{
"id": 1,
"user_id": 1,
"text": "quux",
"created_at": "2017-01-16 00:00:00",
"updated_at": "2017-01-18 00:00:00"
}
],
"has_previous": false,
"previous_cursor": null,
"has_next": true,
"next_cursor": {
"updated_at": "2017-01-18 00:00:00",
"created_at": "2017-01-14 00:00:00",
"id": 6
}
}
```

<!--
WIP DOCUMENTATION
## Classes
Note: See also [lampager/lampager](https://github.com/lampager/lampager).
| Name | Type | Parent Class | Description |
|:---|:---|:---|:---|
| Lampager\\idiorm\\`Paginator` | Class | Lampager\\`Paginator` | Fluent factory implementation for Idiorm |
| Lampager\\idiorm\\`Processor` | Class | Lampager\\`AbstractProcessor` | Processor implementation for Idiorm |
| Lampager\\idiorm\\`PaginationResult` | Class | Lampager\\`PaginationResult` | PaginationResult implementation for Idiorm |
| Lampager\\idiorm\\`MacroServiceProvider` | Class | Illuminate\\Support\\`ServiceProvider` | Enable macros chainable from QueryBuilder, ElqouentBuilder and Relation |
`Paginator`, `Processor` and `PaginationResult` are macroable.
## API
Note: See also [lampager/lampager](https://github.com/lampager/lampager).
### Paginator::__construct()<br>Paginator::create()
Create a new paginator instance.
If you use idiorm macros, however, you don't need to directly instantiate.
```php
static Paginator create(QueryBuilder|EloquentBuilder|Relation $builder): static
Paginator::__construct(QueryBuilder|EloquentBuilder|Relation $builder)
```
- `QueryBuilder` means `\Illuminate\Database\Query\Builder`
- `EloquentBuilder` means `\Illuminate\Database\Eloquent\Builder`
- `Relation` means `\Illuminate\Database\Eloquent\Relation`
### Paginator::transform()
Transform Lampager Query into Illuminate builder.
```php
Paginator::transform(Query $query): QueryBuilder|EloquentBuilder|Relation
```
### Paginator::build()
Perform configure + transform.
```php
Paginator::build(\Lampager\Cursor|array $cursor = []): QueryBuilder|EloquentBuilder|Relation
```
### Paginator::paginate()
Perform configure + transform + process.
```php
Paginator::paginate(\Lampager\Cursor|array $cursor = []): \Lampager\idiorm\PaginationResult
```
#### Arguments
- **`(mixed)`** __*$cursor*__<br> An associative array that contains `$column => $value` or an object that implements `\Lampager\Cursor`. It must be **all-or-nothing**.
- For initial page, omit this parameter or pass empty array.
- For subsequent pages, pass all parameters. Partial parameters are not allowd.
#### Return Value
e.g.
(Default format when using `\Illuminate\Database\Eloquent\Builder`)
```php
object(Lampager\idiorm\PaginationResult)#1 (5) {
["records"]=>
object(Illuminate\Database\Eloquent\Collection)#2 (1) {
["items":protected]=>
array(5) {
[0]=>
object(App\Post)#2 (26) { ... }
[1]=>
object(App\Post)#3 (26) { ... }
[2]=>
object(App\Post)#4 (26) { ... }
[3]=>
object(App\Post)#5 (26) { ... }
[4]=>
object(App\Post)#6 (26) { ... }
}
}
["hasPrevious"]=>
bool(false)
["previousCursor"]=>
NULL
["hasNext"]=>
bool(true)
["nextCursor"]=>
array(2) {
["updated_at"]=>
string(19) "2017-01-18 00:00:00"
["created_at"]=>
string(19) "2017-01-14 00:00:00"
["id"]=>
int(6)
}
}
```
### Paginator::useFormatter()<br>Paginator::restoreFormatter()<br>Paginator::process()
Invoke Processor methods.
```php
Paginator::useFormatter(Formatter|callable $formatter): $this
Paginator::restoreFormatter(): $this
Paginator::process(\Lampager\Query $query, \Illuminate\Database\Eloquent\Collection $rows): \Illuminate\Support\Collection
```
### PaginationResult::toArray()<br>PaginationResult::jsonSerialize()
Convert the object into array.
**IMPORTANT: `camelCase` properties are converted into `snake_case` form.**
```php
PaginationResult::toArray(): array
PaginationResult::jsonSerialize(): array
```
### PaginationResult::__call()
Call macro or Collection methods.
```php
PaginationResult::__call(string $name, array $args): mixed
```
e.g.
```php
PaginationResult::macro('foo', function () {
return ...;
});
$foo = $result->foo();
```
```php
$first = $result->first();
```
-->
Loading

0 comments on commit a6bfff9

Please sign in to comment.