Skip to content

Commit

Permalink
Sort revamp, CreatedAt/UpdatedAt traits
Browse files Browse the repository at this point in the history
  • Loading branch information
kapxapot committed Mar 28, 2020
1 parent 7018760 commit 73e514e
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 150 deletions.
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public function toArray() : array
}

/**
* Groups collection by column/property or callable.
* Groups collection by column/property or \Closure.
*
* @param mixed $by Column/property name or callable, returning generated column/property name. Default = 'id'.
* @param mixed $by Column/property name or \Closure, returning generated column/property name. Default = 'id'.
* @return array Returns associative array of collections.
*/
public function group($by = null) : array
Expand Down
16 changes: 9 additions & 7 deletions src/Models/Traits/Created.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Plasticode\Models\Traits;

use Plasticode\Models\User;
use Plasticode\Util\Date;

/**
* @property integer|null $createdBy
* @property string|null $createdAt
*/
trait Created
{
use CreatedAt;

protected ?User $creator = null;

public function createdBy() : ?int
Expand All @@ -29,6 +29,13 @@ public function creator() : ?User
return $this->creator;
}

public function isCreatedBy(User $user) : bool
{
return $this->creator
? $this->creator->equals($user)
: false;
}

/**
* Sets or updates createdBy and creator.
*
Expand All @@ -45,9 +52,4 @@ protected function stampCreator(User $user) : self

return $this->withCreator($user);
}

public function createdAtIso() : string
{
return Date::iso($this->createdAt);
}
}
16 changes: 16 additions & 0 deletions src/Models/Traits/CreatedAt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Plasticode\Models\Traits;

use Plasticode\Util\Date;

/**
* @property string|null $createdAt
*/
trait CreatedAt
{
public function createdAtIso() : string
{
return Date::iso($this->createdAt);
}
}
9 changes: 2 additions & 7 deletions src/Models/Traits/Updated.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Plasticode\Models\Traits;

use Plasticode\Models\User;
use Plasticode\Util\Date;

/**
* @property integer|null $updatedBy
* @property string|null $updatedAt
*/
trait Updated
{
use UpdatedAt;

protected ?User $updater = null;

public function updatedBy() : ?int
Expand Down Expand Up @@ -41,9 +41,4 @@ protected function stampUpdater(User $user) : self

return $this->withUpdater($user);
}

public function updatedAtIso() : string
{
return Date::iso($this->updatedAt);
}
}
16 changes: 16 additions & 0 deletions src/Models/Traits/UpdatedAt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Plasticode\Models\Traits;

use Plasticode\Util\Date;

/**
* @property string|null $updatedAt
*/
trait UpdatedAt
{
public function updatedAtIso() : string
{
return Date::iso($this->updatedAt);
}
}
4 changes: 3 additions & 1 deletion src/Repositories/Idiorm/Basic/IdiormRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ protected function getSortOrder() : array
}

return [
new SortStep($this->sortField, $this->sortReverse)
$this->sortReverse
? SortStep::create($this->sortField)
: SortStep::createDesc($this->sortField)
];
}

Expand Down
10 changes: 5 additions & 5 deletions src/Util/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ public static function groupById(array $array) : array
}

/**
* Groups array by column/property or Closure.
* Groups array by column/property or \Closure.
*
* @param array $array
* @param string|\Closure $by Column/property name or Closure, returning generated column/property name.
* @param string|\Closure $by Column/property name or \Closure, returning generated column/property name.
* @return array
*/
public static function groupBy(array $array, $by) : array
Expand Down Expand Up @@ -528,11 +528,11 @@ public static function orderByStrDesc(array $array, string $column) : array
* Shortcut for Sort::multi().
*
* @param array $array
* @param array $sorts
* @param SortStep[] $steps
* @return array
*/
public static function multiSort(array $array, array $sorts) : array
public static function multiSort(array $array, array $steps) : array
{
return Sort::multi($array, $sorts);
return Sort::multi($array, $steps);
}
}
101 changes: 63 additions & 38 deletions src/Util/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Plasticode\Util;

use Plasticode\Util\Traits\PropertyAccess;
use Webmozart\Assert\Assert;

class Sort
{
Expand All @@ -11,6 +12,7 @@ class Sort
const ASC = 'asc';
const DESC = 'desc';

const NUMBER = 'number';
const STRING = 'string';
const NULL = 'null';
const BOOL = 'bool';
Expand All @@ -19,35 +21,29 @@ class Sort
/**
* Sorts array by multiple fields.
*
* Example config:
*
* $sorts = [
* 'remote_online' => [], // dir = asc by default
* 'priority' => [ 'dir' => 'desc' ], // type is numeric by default
* 'priority_game' => [ 'dir' => 'desc' ],
* 'remote_viewers' => [ 'dir' => 'desc' ],
* 'title' => [ 'type' => 'string' ],
* ];
* @param SortStep[] $steps
*/
public static function multi(array $array, array $sorts) : array
public static function multi(array $array, array $steps) : array
{
if (empty($array)) {
return [];
}

if (empty($sorts)) {
if (empty($steps)) {
return $array;
}

Assert::allIsInstanceOf($steps, SortStep::class);

usort(
$array,
function($itemA, $itemB) use ($sorts) {
foreach ($sorts as $field => $settings) {
$dir = $settings['dir'] ?? self::ASC;
$type = $settings['type'] ?? null;
function($itemA, $itemB) use ($steps) {
foreach ($steps as $step) {
$dir = $step->isDesc() ? self::DESC : self::ASC;
$type = $step->getType();

$propA = self::getProperty($itemA, $field);
$propB = self::getProperty($itemB, $field);
$propA = $step->getValue($itemA);
$propB = $step->getValue($itemB);

switch ($type) {
case self::STRING:
Expand Down Expand Up @@ -111,61 +107,90 @@ function($itemA, $itemB) use ($sorts) {
* Sorts array by $field asc/desc.
*
* @param array $array
* @param string $field
* @param string|null $dir 'asc' or 'desc'. null = 'asc'
* @param string|null $type null = numeric
* @param string|\Closure $by
* @param string|null $dir Sort::ASC (default) or Sort::DESC
* @param string|null $type null = Sort::NUMBER
*
* @return array
*/
public static function by(array $array, string $field, ?string $dir = null, ?string $type = null) : array
public static function by(
array $array,
$by,
?string $dir = null,
?string $type = null
) : array
{
$sorts = [
$field => [
'dir' => $dir ?? self::ASC,
'type' => $type
],
/** @var string|null */
$field = null;

/** @var \Closure|null */
$closure = null;

if ($by instanceof \Closure) {
$closure = $by;
} else {
$field = $by;
}

$steps = [
new SortStep(
$field,
$closure,
$dir === self::DESC,
$type
)
];

return self::multi($array, $sorts);
return self::multi($array, $steps);
}

/**
* Alias for by($array, $field).
*
* @param string|\Closure $by
*/
public static function asc(array $array, string $field, ?string $type = null) : array
public static function asc(array $array, $by, ?string $type = null) : array
{
return self::by($array, $field, null, $type);
return self::by($array, $by, null, $type);
}

/**
* Shortcut for by($array, $field, 'desc').
* Shortcut for by($array, $field, Sort::DESC).
*
* @param string|\Closure $by
*/
public static function desc(array $array, string $field, ?string $type = null) : array
public static function desc(array $array, $by, ?string $type = null) : array
{
return self::by($array, $field, self::DESC, $type);
return self::by($array, $by, self::DESC, $type);
}

/**
* Sort by $field as strings.
*
* @param string|\Closure $by
*/
public static function byStr(array $array, string $field, ?string $dir = null) : array
public static function byStr(array $array, $by, ?string $dir = null) : array
{
return self::by($array, $field, $dir, self::STRING);
return self::by($array, $by, $dir, self::STRING);
}

/**
* Sort ascending by $field as strings.
*
* @param string|\Closure $by
*/
public static function ascStr(array $array, string $field) : array
public static function ascStr(array $array, $by) : array
{
return self::byStr($array, $field);
return self::byStr($array, $by);
}

/**
* Sort descending by $field as strings.
*
* @param string|\Closure $by
*/
public static function descStr(array $array, string $field) : array
public static function descStr(array $array, $by) : array
{
return self::byStr($array, $field, self::DESC);
return self::byStr($array, $by, self::DESC);
}
}
Loading

0 comments on commit 73e514e

Please sign in to comment.