Skip to content
This repository has been archived by the owner on Jan 27, 2022. It is now read-only.

Commit

Permalink
Add simple pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
iamfredric committed Jun 22, 2017
1 parent 38774c0 commit 3adcb0c
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/Modest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ public static function find($id)
return QueryBuilder::find($id, new static);
}

/**
* Paginated results
*
* @param integer $limit
*
* @return QueryBuilder
*/
public static function paginate($limit = null)
{
return (new QueryBuilder(new static))->paginate($limit);
}

/**
* @return mixed
*/
Expand Down
77 changes: 77 additions & 0 deletions src/Pagination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Wordpriest\Modest;

use Illuminate\Support\Collection;

class Pagination implements \ArrayAccess, \JsonSerializable
{
public $items;

public function __construct($items)
{
$this->items = $items;
}

public function paginate($args = [])
{
return paginate_links($args);
}

public function toArray()
{
return $this->items;
}

public function jsonSerialize()
{
return $this->toArray();
}

/**
* Determines whether a offset exists
*
* @param mixed $offset
*
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->items[$offset]);
}

/**
* Sets offset to retrieve
*
* @param mixed $offset
*
* @return mixed|null|Modest
*/
public function offsetGet($offset)
{
return $this->items[$offset];
}

/**
* Offset to set
*
* @param mixed $offset
* @param mixed $value
*
* @return void
*/
public function offsetSet($offset, $value)
{
$this->items[$offset] = $value;
}

/**
* Offset to unset
*
* @param mixed $offset
*/
public function offsetUnset($offset)
{
unset($this->items[$offset]);
}
}
26 changes: 22 additions & 4 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ public static function find($id, $model = null)
{
$instance = new static($model);

$instance->arguments = [];
$instance->setArgument('id', $id);

return $instance->first();
return $instance->buildItem(get_post($id));
}

/**
Expand All @@ -86,6 +83,27 @@ public function get()
return $posts;
}

/**
* Paginates results
*
* @param integere $limit
*
* @return Paginator
*/
public function paginate($limit = null)
{
$posts = [];

$this->setArgument('posts_per_page', $limit);
$this->setArgument('paged', get_query_var('paged') ?: 1);

foreach ((array) get_posts($this->getArguments()) as $post) {
$posts[] = $this->buildItem($post);
}

return new Pagination($posts);
}

protected function buildItem($post)
{
if ($this->model) {
Expand Down
5 changes: 3 additions & 2 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ function snake_to_camel($string) {

if (! function_exists('content_to_excerpt')) {
function content_to_excerpt($string, $length) {
$string = mb_substr(strip_tags($string), 0, $length);
$originalString = strip_tags($string);
$string = mb_substr($originalString, 0, $length);

if (strlen($string) > $length) {
if (strlen($originalString) > $length) {
return "{$string}...";
}

Expand Down

0 comments on commit 3adcb0c

Please sign in to comment.