order converter change
This commit is contained in:
@@ -29,6 +29,8 @@
|
||||
|
||||
namespace Espo\Core\Select\Applier\Appliers;
|
||||
|
||||
use Espo\ORM\Query\Part\OrderList;
|
||||
|
||||
use Espo\Core\{
|
||||
Exceptions\Error,
|
||||
Exceptions\Forbidden,
|
||||
@@ -141,11 +143,13 @@ class Order
|
||||
if ($hasItemConverter) {
|
||||
$converter = $this->itemConverterFactory->create($this->entityType, $orderBy);
|
||||
|
||||
$resultOrderBy = $converter->convert(
|
||||
OrderItem::fromArray([
|
||||
'orderBy' => $orderBy,
|
||||
'order' => $order,
|
||||
])
|
||||
$resultOrderBy = $this->orderListToArray(
|
||||
$converter->convert(
|
||||
OrderItem::fromArray([
|
||||
'orderBy' => $orderBy,
|
||||
'order' => $order,
|
||||
])
|
||||
)
|
||||
);
|
||||
}
|
||||
else if (in_array($type, ['link', 'file', 'image', 'linkOne'])) {
|
||||
@@ -185,4 +189,18 @@ class Order
|
||||
|
||||
$queryBuilder->order($resultOrderBy);
|
||||
}
|
||||
|
||||
private function orderListToArray(OrderList $orderList): array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach ($orderList as $order) {
|
||||
$list[] = [
|
||||
$order->getExpression()->getValue(),
|
||||
$order->getDirection(),
|
||||
];
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,9 @@
|
||||
|
||||
namespace Espo\Core\Select\Order;
|
||||
|
||||
use Espo\ORM\Query\Part\OrderList;
|
||||
|
||||
interface ItemConverter
|
||||
{
|
||||
public function convert(Item $item): array;
|
||||
public function convert(Item $item): OrderList;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,9 @@
|
||||
|
||||
namespace Espo\Core\Select\Order\ItemConverters;
|
||||
|
||||
use Espo\ORM\Query\Part\OrderList;
|
||||
use Espo\ORM\Query\Part\Order;
|
||||
|
||||
use Espo\Core\{
|
||||
Select\Order\ItemConverter,
|
||||
Select\Order\Item,
|
||||
@@ -36,15 +39,15 @@ use Espo\Core\{
|
||||
|
||||
class AddressType implements ItemConverter
|
||||
{
|
||||
public function convert(Item $item): array
|
||||
public function convert(Item $item): OrderList
|
||||
{
|
||||
$orderBy = $item->getOrderBy();
|
||||
$order = $item->getOrder();
|
||||
$order = $item->getOrder() ?? Order::ASC;
|
||||
|
||||
return [
|
||||
[$orderBy . 'Country', $order],
|
||||
[$orderBy . 'City', $order],
|
||||
[$orderBy . 'Street', $order],
|
||||
];
|
||||
return OrderList::create([
|
||||
Order::fromString($orderBy . 'Country')->withDirection($order),
|
||||
Order::fromString($orderBy . 'City')->withDirection($order),
|
||||
Order::fromString($orderBy . 'Street')->withDirection($order),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,10 @@
|
||||
|
||||
namespace Espo\Core\Select\Order\ItemConverters;
|
||||
|
||||
use Espo\ORM\Query\Part\OrderList;
|
||||
use Espo\ORM\Query\Part\Order;
|
||||
use Espo\ORM\Query\Part\Expression;
|
||||
|
||||
use Espo\Core\{
|
||||
Select\Order\ItemConverter,
|
||||
Select\Order\Item,
|
||||
@@ -50,19 +54,19 @@ class EnumType implements ItemConverter
|
||||
$this->metadata = $metadata;
|
||||
}
|
||||
|
||||
public function convert(Item $item): array
|
||||
public function convert(Item $item): OrderList
|
||||
{
|
||||
$orderBy = $item->getOrderBy();
|
||||
$order = $item->getOrder();
|
||||
$order = $item->getOrder() ?? SearchParams::ORDER_ASC;
|
||||
|
||||
$list = $this->metadata->get([
|
||||
'entityDefs', $this->entityType, 'fields', $orderBy, 'options'
|
||||
]);
|
||||
|
||||
if (!$list || !is_array($list) || !count($list)) {
|
||||
return [
|
||||
[$orderBy, $order]
|
||||
];
|
||||
return OrderList::create([
|
||||
Order::fromString($orderBy)->withDirection($order)
|
||||
]);
|
||||
}
|
||||
|
||||
$isSorted = $this->metadata->get([
|
||||
@@ -77,14 +81,8 @@ class EnumType implements ItemConverter
|
||||
$list = array_reverse($list);
|
||||
}
|
||||
|
||||
foreach ($list as $i => $listItem) {
|
||||
$list[$i] = str_replace(',', '_COMMA_', $listItem);
|
||||
}
|
||||
|
||||
return [
|
||||
[
|
||||
'LIST:' . $orderBy . ':' . implode(',', $list)
|
||||
]
|
||||
];
|
||||
return OrderList::create([
|
||||
Order::createByPositionInList(Expression::column($orderBy), $list),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Query\Part;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Iterator;
|
||||
|
||||
/**
|
||||
* A list of order items.
|
||||
*/
|
||||
class OrderList implements Iterator
|
||||
{
|
||||
private $position = 0;
|
||||
|
||||
private function __construct(array $list)
|
||||
{
|
||||
foreach ($list as $item) {
|
||||
if (!$item instanceof Order) {
|
||||
throw new InvalidArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
$this->list = $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance.
|
||||
*
|
||||
* @param Order[] $list
|
||||
*/
|
||||
public static function create(array $list): self
|
||||
{
|
||||
return new self($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
$this->position = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Order
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return $this->list[$this->position];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return $this->position;
|
||||
}
|
||||
|
||||
public function next()
|
||||
{
|
||||
++$this->position;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return isset($this->list[$this->position]);
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,9 @@
|
||||
|
||||
namespace tests\unit\Espo\Core\Select\Applier\Appliers;
|
||||
|
||||
use Espo\ORM\Query\Part\OrderList;
|
||||
use Espo\ORM\Query\Part\Order;
|
||||
|
||||
use Espo\Core\{
|
||||
Exceptions\Error,
|
||||
Select\Applier\Appliers\Order as OrderApplier,
|
||||
@@ -47,7 +50,7 @@ use Espo\{
|
||||
|
||||
class OrderApplierTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
protected function setUp() : void
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->user = $this->createMock(User::class);
|
||||
$this->metadataProvider = $this->createMock(MetadataProvider::class);
|
||||
@@ -147,7 +150,11 @@ class OrderApplierTest extends \PHPUnit\Framework\TestCase
|
||||
->with($this->entityType)
|
||||
->willReturn($orderBy);
|
||||
|
||||
$this->initApplyOrderTest($orderBy, $order, 'varchar', [['hello', SearchParams::ORDER_DESC]]);
|
||||
$converterResult = OrderList::create([
|
||||
Order::fromString('hello')->withDesc(),
|
||||
]);
|
||||
|
||||
$this->initApplyOrderTest($orderBy, $order, 'varchar', $converterResult);
|
||||
|
||||
$this->applier->apply($this->queryBuilder, $this->params);
|
||||
}
|
||||
@@ -184,7 +191,7 @@ class OrderApplierTest extends \PHPUnit\Framework\TestCase
|
||||
}
|
||||
|
||||
protected function initApplyOrderTest(
|
||||
string $orderBy, string $order, string $fieldType, ?array $converterResult = null, bool $notExisting = false
|
||||
string $orderBy, string $order, string $fieldType, ?OrderList $converterResult = null, bool $notExisting = false
|
||||
) {
|
||||
$this->metadataProvider
|
||||
->expects($this->any())
|
||||
@@ -246,7 +253,8 @@ class OrderApplierTest extends \PHPUnit\Framework\TestCase
|
||||
->willReturn(false);
|
||||
}
|
||||
|
||||
$expectedOrderBy = $converterResult ?? [[$orderBy, $order]];
|
||||
$expectedOrderBy = ($converterResult ? $this->orderListToArray($converterResult): null)
|
||||
?? [[$orderBy, $order]];
|
||||
|
||||
$expectedOrderBy[] = ['id', $order];
|
||||
|
||||
@@ -255,4 +263,18 @@ class OrderApplierTest extends \PHPUnit\Framework\TestCase
|
||||
->method('order')
|
||||
->with($expectedOrderBy);
|
||||
}
|
||||
|
||||
private function orderListToArray(OrderList $orderList): array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach ($orderList as $order) {
|
||||
$list[] = [
|
||||
$order->getExpression()->getValue(),
|
||||
$order->getDirection(),
|
||||
];
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user