Skip to content

Commit

Permalink
Merge pull request #8 from Laurentvw/develop
Browse files Browse the repository at this point in the history
Allow to reverse the order of the results
  • Loading branch information
Laurentvw committed Aug 27, 2015
2 parents bad977d + 330559f commit 504c983
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ $results = $matches->orderBy('title')->orderBy('url', 'desc')->get();

// Custom sorting: For values that do not lend well with sorting, e.g. dates*.
$results = $matches->orderBy('date', 'desc', 'date_create')->get();

// Simply reverse the order of the results
$results = $matches->reverse()->get();
```
* See [date_create](http://php.net/manual/en/function.date-create.php)

Expand Down
23 changes: 23 additions & 0 deletions src/Laurentvw/Scrapher/Scrapher.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class Scrapher
*/
protected $orderBy;

/**
* Whether to return results in reverse order
*
* @var bool
*/
protected $reverse = false;

/**
* Create a new Scrapher instance.
*
Expand Down Expand Up @@ -156,6 +163,18 @@ public function filter($filter)
return $this;
}

/**
* Reverse the order of the resulting matches.
*
* @return Scrapher
*/
public function reverse()
{
$this->reverse = true;

return $this;
}

/**
* Take n-number of matches.
*
Expand Down Expand Up @@ -301,6 +320,10 @@ protected function scrape()
}
}

if ($this->reverse) {
krsort($results);
}

$this->clear();

return $results;
Expand Down

0 comments on commit 504c983

Please sign in to comment.