Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Nov 18, 2017
0 parents commit 4f7b2cd
Show file tree
Hide file tree
Showing 19 changed files with 1,919 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.
256 changes: 256 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
<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 and Paris

Rapid pagination without using OFFSET

## Requirements

- PHP: ^5.6 || ^7.0
- idiorm: ^5.4, paris: ^1.5
- [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::for_table('posts')->where_equal('user_id', 1))
->forward()
->limit(5)
->order_by_desc('updated_at') // ORDER BY `updated_at` DESC, `created_at` DESC, `id` DESC
->order_by_desc('created_at')
->order_by_desc('id')
->seekable()
->paginate($cursor)
->to_json(JSON_PRETTY_PRINT);
```

It will run the optimized query.


```sql
SELECT * FROM (

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

) `temporary_table`

UNION ALL

SELECT * FROM (

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

) `temporary_table`
```

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
}
}
```

## 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 and Paris |
| Lampager\\Idiorm\\`Processor` | Class | Lampager\\`AbstractProcessor` | Processor implementation for Idiorm and Paris |
| Lampager\\Idiorm\\`PaginationResult` | Class | Lampager\\`PaginationResult` | PaginationResult implementation for Idiorm and Paris |

- All *camelCase* methods in `Paginator`, `Processor` and `PaginationResult` can be invoked by *snake_case* style.

## API

Note: See also [lampager/lampager](https://github.com/lampager/lampager).

### Paginator::__construct()<br>Paginator::create()

Create a new paginator instance.
If you use global function `lampager()`, however, you don't need to directly instantiate.

```php
static Paginator create(\ORM|\ORMWrapper $builder): static
Paginator::__construct(\ORM|\ORMWrapper $builder)
```

### Paginator::transform()

Transform Lampager Query into Illuminate builder.

```php
Paginator::transform(\Lampager\Query $query): ORM|ORMWrapper
```

### Paginator::build()

Perform configure + transform.

```php
Paginator::build(\Lampager\Cursor|array $cursor = []): ORM|ORMWrapper
```

### 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.

```php
object(Lampager\Idiorm\PaginationResult)#1 (5) {
["records"]=>
array(5) {
[0]=>
object(ORM)#2 (22) { ... }
[1]=>
object(ORM)#3 (22) { ... }
[2]=>
object(ORM)#4 (22) { ... }
[3]=>
object(ORM)#5 (22) { ... }
[4]=>
object(ORM)#6 (22) { ... }
}
["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(\Lampager\Formatter|callable $formatter): $this
Paginator::restoreFormatter(): $this
Paginator::process(\Lampager\Query $query, array|\IdiormResultSet $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 `IdiormResultSet` methods.

```php
PaginationResult::__call(string $name, array $args): mixed
```
Loading

0 comments on commit 4f7b2cd

Please sign in to comment.