Permalink
Browse files

Refactored sort function into its own service. Updated docs. General …

…cleanup.
  • Loading branch information...
michaelrog committed Dec 24, 2017
1 parent fa72d0b commit 28e2e208c6bdce7a0f6ae83f5236ac346424f1b2
Showing with 309 additions and 2,880 deletions.
  1. +9 −31 .gitignore
  2. +16 −4 CHANGELOG.md
  3. +1 −1 LICENSE.txt
  4. +46 −22 README.md
  5. +18 −15 composer.json
  6. +0 −2,587 composer.lock
  7. +0 −11 phpcs/ruleset.xml
  8. +0 −59 phpmd/ruleset.xml
  9. +38 −13 src/SuperSort.php
  10. +18 −137 src/SuperSortTwigExtension.php
  11. +163 −0 src/services/Sorter.php
@@ -1,47 +1,30 @@
#------------------------------------
# Project
#------------------------------------

# Project custom ignores





#------------------------------------
# Tools
#------------------------------------
# --- Ignore workflow metadata

.env
configLocal.json
localStorage/*
!localStorage/dbBackups
localStorage/dbBackups/*
bower_components/
node_modules/
out/
.*~
.bundle
.idea/
.komodotools
.project
.sass-cache
.settings/
.tern-project
.tmproj
*.esproj
*.komodoproject
*.sublime-*
*.swp
browserstack.js
codekit-config.json
mixture.json
npm-debug.log
prepros.json





#------------------------------------
# System
#------------------------------------
# --- Ignore system cruft

.cache
.DS_Store
@@ -60,12 +43,7 @@ Homestead.yaml
vendor/*
*.vagrant

# --- Include .gitignore directives




#------------------------------------
# Gitkeep
#------------------------------------

!.gitkeep
!.gitignore
!*/.gitignore
@@ -1,17 +1,29 @@
# SuperSort Changelog

## Unreleased
### New
- Refactored for Craft 3 support
_...a super-duper sorting function for your Craft templates._

The format of this file is based on [Keep a Changelog](http://keepachangelog.com/). This project adheres to [Semantic Versioning](http://semver.org/).


## 3.0.0 - 2017-12-25

### Added

- SuperSort is ready for Craft 3!

## 1.1.0 - 2015-11-05

### Added

- Added the `shuffle` method.

## 1.0.0 - 2015-11-05

### Fixed

- Fixed a bug where the `rsortas` method was erroneously set to sort the source array rather than the transformed `asArray`, causing incorrect results.

## 0.0.1 - 2015-01-24
- Beta release

- Beta release!

@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Michael Rog (Top Shelf Craft)
Copyright (c) 2017, Michael Rog (Top Shelf Craft)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -1,15 +1,17 @@
# SuperSort

_A super-duper sorting filter for your Craft CMS templates_
_...a super-duper sorting function for your Craft CMS templates._

by Michael Rog
[https://topshelfcraft.com](https://topshelfcraft.com)
**A [Top Shelf Craft](https://topshelfcraft.com) creation**
[Michael Rog](https://michaelrog.com), Proprietor


* * *


## What is SuperSort?

The **SuperSort** plugin provides a powerful Twig filter for ordering an array of values - either using one of PHP's built-in methods to sort the actual values, or using custom "sort as" methods to arrange the array based on the rendered results of running each member through a Twig object template.
The **SuperSort** plugin provides a powerful Twig filter that helps with ordering an array of values — either using one of PHP's built-in methods to sort the actual values, or using custom "sort as" methods to arrange the array based on the rendered results of processing each member as a Twig object template.

Using **SuperSort**'s "sort as" methods, you can sort an array of elements by any accessible object value — including values from Matrix blocks, attributes of related elements, math calculations, etc.

@@ -18,13 +20,17 @@ Using **SuperSort**'s "sort as" methods, you can sort an array of elements by an

### Installation

`composer require topshelfcraft/supersort`
Visit the _Plugin Store_ in your Craft control panel, search for **SuperSort**, and click to _Install_ the plugin.

Or, if you're feeling frisky, you can install **SuperSort** via [Composer](https://getcomposer.org/):

composer require topshelfcraft/supersort

### General usage

Use the `supersort` filter to sort an array of objects.

(You can apply `supersort` to any array or *ElementCriteriaModel*. A non-array-like source will be converted into an empty array... which probably isn't very useful for sorting.)
(You can apply `supersort` to any array or *ElementQuery*. A non-array-like source will be converted into an empty array... which probably isn't very useful for sorting.)


### Basic Sorting
@@ -33,24 +39,26 @@ The first parameter of the filter specifies the sort method.

{% set sortedSource = source | supersort('asort') %}

You can use any of PHP's built-in sort methods:
You can use any of [PHP's built-in sort methods](http://php.net/manual/en/array.sorting.php):

- `'asort'`
- `'arsort'`
- `'ksort'`
- `'krsort'`
- `'natsort'`
- `'natcasesort'`
- `'sort'`
- `'rsort'`
- `'shuffle'`
- [`'asort'`](http://php.net/manual/en/function.asort.php)
- [`'arsort'`](http://php.net/manual/en/function.arsort.php)
- [`'ksort'`](http://php.net/manual/en/function.ksort.php)
- [`'krsort'`](http://php.net/manual/en/function.krsort.php)
- [`'natsort'`](http://php.net/manual/en/function.natsort.php)
- [`'natcasesort'`](http://php.net/manual/en/function.natcasesort.php)
- [`'sort'`](http://php.net/manual/en/function.sort.php)
- [`'rsort'`](http://php.net/manual/en/function.rsort.php)
- [`'shuffle'`](http://php.net/manual/en/function.shuffle.php)

If you don't supply this first parameter, a plain vanilla `asort` will be applied:

{% set sortedSource = source | supersort %}

In many common cases — such as when you're sorting names, numbers, or filenames — the default `'asort'` algorithm may produce unwanted results. In those cases, consider using the case-insensitive natural sorting algorithm ([`'natcasesort'`](http://php.net/manual/en/function.natcasesort.php)).


### Advanced Sorting
### Advanced ("As") Sorting

You can also sort by one of **SuperSort**'s custom methods:

@@ -59,13 +67,13 @@ You can also sort by one of **SuperSort**'s custom methods:
- `'natsortAs'`
- `'natcasesortAs'`

To use these methods, you will provide a second parameter, which is a Twig object template to [render](https://craftcms.com/classreference/services/TemplatesService#renderObjectTemplate-detail) using each object in the array:
To use these methods, you will provide a second parameter, which is a Twig object template to [render](https://docs.craftcms.com/api/v3/craft-web-view.html#renderObjectTemplate()-detail) using each object in the array:

{% upcomingEvents | supersort('sortAs', '{eventDates.first.date}') %}

{% upcomingEvents | supersort('sortAs', '{{ object.eventDates.first.date }}') %}

You can construct an object template using the same syntax as you might use to define a [Dynamic Subfolder Path](http://buildwithcraft.com/docs/assets-fields#dynamic-subfolder-paths) for an Assets field.
(This "object template" syntax is the same as you might use to define a [Dynamic Subfolder Path](http://buildwithcraft.com/docs/assets-fields#dynamic-subfolder-paths) for an Assets field.


### Sort Flags
@@ -74,11 +82,26 @@ You can optionally provide a third parameter — one of PHP's [sort flags](h

{% source | supersort('sortAs', '{foo}', SORT_NUMERIC) %}

{% source | supersort('asort', false, SORT_NUMERIC) %}
{% source | supersort('asort', null, SORT_NUMERIC) %}

If you don't supply this third parameter, the default (`SORT_REGULAR`) flag is used.


### Using SuperSort as a PHP helper

If you'd like to use the `superSort()` method as a helper in another Craft plugin or component, you can access it in PHP directly from the Sorter service:

```php
$result = SuperSort::$plugin->sorter->superSort($array, $method, $as, $sortFlag);
```

(Note: [PHP's built-in sort methods](http://php.net/manual/en/array.sorting.php) sort an array in place and return a _boolean_ representing success. The `superSort()` method, by contrast, returns the result _array_.)

* * *


## Support

### What are the system requirements?

Craft 3.0+ and PHP 7.0+
@@ -91,12 +114,13 @@ Nah...

### I triple-checked. It's a bug.

Well, alright. Please open a GitHub Issue, submit a PR to the `dev` branch, or just email me to let me know.
Well, alright. Please open a GitHub Issue, submit a PR to the `3.x.dev` branch, or just email me to let me know.


* * *

#### Contributors:

- Plugin development: [Michael Rog](http://michaelrog.com) / @michaelrog
- Plugin development: [Michael Rog](https://michaelrog.com) / @michaelrog
- Craft 3 port: [TJ Draper](https://buzzingpixel.com) / @tjdraperpro
- Icon: [Ralf Schmitzer](https://dribbble.com/schmitzer), via [The Noun Project](https://thenounproject.com/term/sort/503379)
@@ -1,22 +1,26 @@
{
"name": "topshelfcraft/supersort",
"description": "A super-duper sorting filter for your CraftCMS templates",
"description": "...a super-duper sorting function for your Craft templates.",
"type": "craft-plugin",
"version": "2.0.0",
"support": {
"docs": "https://github.com/TopShelfCraft/SuperSort"
},
"version": "3.0.1",
"keywords": [
"craft",
"cms",
"craftcms",
"craft-plugin",
"SuperSort"
"SuperSort",
"sort",
"sorting",
"array",
"list"
],
"support": {
"docs": "https://github.com/TopShelfCraft/SuperSort"
},
"authors": [
{
"name": "Michael Rog",
"homepage": "https://michaelrog.com"
"name": "Top Shelf Craft (Michael Rog)",
"homepage": "https://topshelfcraft.com"
},
{
"name": "TJ Draper",
@@ -30,10 +34,7 @@
}
],
"require": {
"php": ">=7.0.0",
"craftcms/cms": "^3.0.0-beta.28",
"vlucas/phpdotenv": "^2.4.0",
"roave/security-advisories": "dev-master"
"craftcms/cms": "^3.0.0-RC3"
},
"autoload": {
"psr-4": {
@@ -43,13 +44,15 @@
"extra": {
"name": "SuperSort",
"handle": "supersort",
"developer": "Top Shelf Craft",
"developer": "Top Shelf Craft (Michael Rog)",
"developerUrl": "https://topshelfcraft.com",
"schemaVersion": "0.0.0.0",
"hasCpSettings": false,
"hasCpSection": false,
"class": "topshelfcraft\\supersort\\SuperSort",
"changelogUrl": "https://github.com/TopShelfCraft/SuperSort/raw/master/CHANGELOG.md",
"downloadUrl": "https://github.com/TopShelfCraft/SuperSort/releases"
"components": {
"sorter": "topshelfcraft\\supersort\\services\\Sorter"
},
"class": "topshelfcraft\\supersort\\SuperSort"
}
}
Oops, something went wrong.

0 comments on commit 28e2e20

Please sign in to comment.