refactoring
This commit is contained in:
@@ -143,10 +143,7 @@ class Order
|
||||
|
||||
$resultOrderBy = $this->orderListToArray(
|
||||
$converter->convert(
|
||||
OrderItem::fromArray([
|
||||
'orderBy' => $orderBy,
|
||||
'order' => $order,
|
||||
])
|
||||
OrderItem::create($orderBy, $order)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -35,66 +35,45 @@ use InvalidArgumentException;
|
||||
|
||||
class Item
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $orderBy = false;
|
||||
private string $orderBy;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $order = false;
|
||||
private string $order;
|
||||
|
||||
private function __construct()
|
||||
private function __construct(string $orderBy, string $order)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $params
|
||||
*/
|
||||
public static function fromArray(array $params): self
|
||||
{
|
||||
$object = new self();
|
||||
|
||||
$object->orderBy = $params['orderBy'] ?? null;
|
||||
$object->order = $params['order'] ?? null;
|
||||
|
||||
foreach ($params as $key => $value) {
|
||||
if (!property_exists($object, $key)) {
|
||||
throw new InvalidArgumentException("Unknown parameter '{$key}'.");
|
||||
}
|
||||
}
|
||||
|
||||
if ($object->orderBy && !is_string($object->orderBy)) {
|
||||
throw new InvalidArgumentException("Bad orderBy.");
|
||||
}
|
||||
|
||||
if (
|
||||
$object->order &&
|
||||
$object->order !== SearchParams::ORDER_ASC &&
|
||||
$object->order !== SearchParams::ORDER_DESC
|
||||
$order !== SearchParams::ORDER_ASC &&
|
||||
$order !== SearchParams::ORDER_DESC
|
||||
) {
|
||||
throw new InvalidArgumentException("Bad order.");
|
||||
}
|
||||
|
||||
return $object;
|
||||
$this->orderBy = $orderBy;
|
||||
$this->order = $order;
|
||||
}
|
||||
|
||||
public static function create(string $orderBy, ?string $order = null): self
|
||||
{
|
||||
if ($order === null) {
|
||||
$order = SearchParams::ORDER_ASC;
|
||||
}
|
||||
|
||||
return new self($orderBy, $order);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an order-by field.
|
||||
* Get a field.
|
||||
*/
|
||||
public function getOrderBy(): ?string
|
||||
public function getOrderBy(): string
|
||||
{
|
||||
/** @var ?string */
|
||||
return $this->orderBy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a direction of order.
|
||||
* Get a direction.
|
||||
*/
|
||||
public function getOrder(): ?string
|
||||
public function getOrder(): string
|
||||
{
|
||||
/** @var ?string */
|
||||
return $this->order;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ class AddressType implements ItemConverter
|
||||
public function convert(Item $item): OrderList
|
||||
{
|
||||
$orderBy = $item->getOrderBy();
|
||||
$order = $item->getOrder() ?? Order::ASC;
|
||||
$order = $item->getOrder();
|
||||
|
||||
return OrderList::create([
|
||||
Order::fromString($orderBy . 'Country')->withDirection($order),
|
||||
|
||||
@@ -58,7 +58,7 @@ class EnumType implements ItemConverter
|
||||
{
|
||||
/** @var string */
|
||||
$orderBy = $item->getOrderBy();
|
||||
$order = $item->getOrder() ?? SearchParams::ORDER_ASC;
|
||||
$order = $item->getOrder();
|
||||
|
||||
/** @var ?string[] */
|
||||
$list = $this->metadata->get([
|
||||
|
||||
@@ -228,10 +228,7 @@ class OrderApplierTest extends \PHPUnit\Framework\TestCase
|
||||
->with($this->entityType, $orderBy)
|
||||
->willReturn($converter);
|
||||
|
||||
$item = Item::fromArray([
|
||||
'orderBy' => $orderBy,
|
||||
'order' => $order,
|
||||
]);
|
||||
$item = Item::create($orderBy, $order);
|
||||
|
||||
$converter
|
||||
->expects($this->once())
|
||||
|
||||
@@ -29,62 +29,23 @@
|
||||
|
||||
namespace tests\unit\Espo\Core\Select\Order;
|
||||
|
||||
use Espo\Core\{
|
||||
Select\Order\Item,
|
||||
};
|
||||
use Espo\Core\Select\Order\Item;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class ItemTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
protected function setUp() : void
|
||||
public function testCreate1()
|
||||
{
|
||||
}
|
||||
|
||||
public function testFromArray()
|
||||
{
|
||||
$item = Item::fromArray([
|
||||
'order' => 'DESC',
|
||||
'orderBy' => 'test',
|
||||
]);
|
||||
$item = Item::create('test', 'DESC');
|
||||
|
||||
$this->assertEquals('DESC', $item->getOrder());
|
||||
$this->assertEquals('test', $item->getOrderBy());
|
||||
}
|
||||
|
||||
public function testEmpty()
|
||||
{
|
||||
$item = Item::fromArray([
|
||||
]);
|
||||
|
||||
$this->assertEquals(null, $item->getOrder());
|
||||
$this->assertEquals(null, $item->getOrderBy());
|
||||
}
|
||||
|
||||
public function testBadOrderBy()
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
$params = Item::fromArray([
|
||||
'orderBy' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testBadOrder()
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
$params = Item::fromArray([
|
||||
'order' => 'd',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testNonExistingParam()
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
$params = Item::fromArray([
|
||||
'bad' => 'd',
|
||||
]);
|
||||
Item::create('test', 'test');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user