Skip to content

Commit

Permalink
Added sorting utility...
Browse files Browse the repository at this point in the history
  • Loading branch information
Zrnik committed Nov 9, 2021
1 parent 6435549 commit 80049df
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 2 deletions.
1 change: 0 additions & 1 deletion .idea/MkSQL.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/webResources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion src/Repository/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Zrnik\MkSQL\Repository\Fetch\Dispenser;
use Zrnik\MkSQL\Repository\Saver\Saver;
use Zrnik\MkSQL\Utilities\Reflection;
use Zrnik\MkSQL\Utilities\Sorting\Sorting;
use function count;
use function is_array;

Expand Down Expand Up @@ -172,8 +173,25 @@ public function distinctValues(string $className, string $propertyName): array

//region Executed Query Count
private int $executedQueries = 0;
public function getExecutedQueryCount() : int {

public function getExecutedQueryCount(): int
{
return $this->executedQueries;
}
//endregion

/**
* @param BaseEntity[] $entities
* @param string $propertyName
* @return BaseEntity[]
*/
public function sortBy(array $entities, string $propertyName): array
{
/**
* @var BaseEntity[] $sorted
* @noinspection PhpUnnecessaryLocalVariableInspection
*/
$sorted = Sorting::sortObjectsByProperty($entities, $propertyName);
return $sorted;
}
}
14 changes: 14 additions & 0 deletions src/Utilities/Sorting/SortedObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace Zrnik\MkSQL\Utilities\Sorting;

class SortedObject
{
/**
* @param int|string $key
* @param object $object
*/
public function __construct(public mixed $key, public object $object)
{
}
}
64 changes: 64 additions & 0 deletions src/Utilities/Sorting/Sorting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php declare(strict_types=1);

namespace Zrnik\MkSQL\Utilities\Sorting;

use function array_key_exists;
use function chr;
use function count;
use function ord;

class Sorting
{


/**
* WILL NOT PRESERVE KEY!
* @param object[] $objects
* @param string $propertyName
* @param bool $preserveKeys
* @return object[]
*/
public static function sortObjectsByProperty(array $objects, string $propertyName, bool $preserveKeys = false): array
{

/** @var array<string, SortedObject> $arrayToSort */
$arrayToSort = [];


foreach ($objects as $key => $object) {

$sortableKey = (string)$object->$propertyName;

while (array_key_exists($sortableKey, $arrayToSort)) {
$sortableKey = self::incrementKey($sortableKey);
}

$arrayToSort[$sortableKey] = new SortedObject($key, $object);
}

ksort($arrayToSort);

$result = [];
foreach ($arrayToSort as $sortedObject) {
if ($preserveKeys) {
$result[$sortedObject->key] = $sortedObject->object;
} else {
$result[] = $sortedObject->object;
}
}

return $result;

}

private static function incrementKey(string $sortableKey): string
{
$chars = str_split($sortableKey);
$lastIndex = count($chars) - 1;
$chars[$lastIndex] = chr(ord($chars[$lastIndex]) + 1);
return implode('', $chars);
}


}

16 changes: 16 additions & 0 deletions tests/Utilities/Sorting/SortableClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace Tests\Utilities\Sorting;

use Brick\DateTime\LocalDate;

class SortableClass
{
public function __construct(
public string $name,
public string $text,
public LocalDate $date,
)
{ }

}
53 changes: 53 additions & 0 deletions tests/Utilities/Sorting/SortingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types=1);

namespace Tests\Utilities\Sorting;

use Brick\DateTime\LocalDate;
use PHPUnit\Framework\TestCase;
use Zrnik\MkSQL\Utilities\Sorting\Sorting;

class SortingTest extends TestCase
{
public function testSorting(): void
{
$sortable1 = new SortableClass(
'First', 'y', LocalDate::of(2002,1,1)
);

$sortable2 = new SortableClass(
'Second', 'z', LocalDate::of(2002,1,1)
);

$sortable3 = new SortableClass(
'Third', 'z', LocalDate::of(2001,1,2)
);

$sortable4 = new SortableClass(
'Third', 'z', LocalDate::of(2000,1,2)
);

$sorted = Sorting::sortObjectsByProperty(
[$sortable3, $sortable2, $sortable1], 'name'
);

static::assertSame([$sortable1, $sortable2, $sortable3], $sorted);

$sorted = Sorting::sortObjectsByProperty(
[$sortable2, $sortable1, $sortable3], 'text'
);

static::assertSame([$sortable1, $sortable2, $sortable3], $sorted);

$sorted = Sorting::sortObjectsByProperty(
[$sortable3, $sortable1, $sortable2, $sortable4], 'text'
);

static::assertSame([$sortable1, $sortable3, $sortable2, $sortable4], $sorted);

$sorted = Sorting::sortObjectsByProperty(
[$sortable1, $sortable2, $sortable3, $sortable4], 'date'
);

static::assertSame([$sortable4, $sortable3, $sortable1, $sortable2], $sorted);
}
}

0 comments on commit 80049df

Please sign in to comment.