Skip to content

Commit

Permalink
Fixed Configuration. Added translation_domain to columns
Browse files Browse the repository at this point in the history
  • Loading branch information
pfilsx committed Oct 20, 2019
1 parent 529e72b commit 53508c4
Show file tree
Hide file tree
Showing 24 changed files with 204 additions and 73 deletions.
4 changes: 2 additions & 2 deletions src/Config/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ public function merge(ConfigurationInterface $configuration): ConfigurationInter
{
$result = clone $this;
foreach ($configuration->getConfigsArray() as $key => $value) {
if (!empty($value)) {
if ($value !== null) {
$setter = 'set' . ucfirst($key);
$this->$setter($value);
$result->$setter($value);
}
}
return $result;
Expand Down
6 changes: 6 additions & 0 deletions src/Grid/Columns/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ abstract class AbstractColumn
*/
protected $container;

protected $translationDomain = null;

public function __construct(DataGridServiceContainer $container, array $config = [])
{

Expand Down Expand Up @@ -196,4 +198,8 @@ protected function setVisible(bool $visibility): void
$this->isVisible = $visibility;
}

public function setTranslationDomain(?string $domain): void {
$this->translationDomain = $domain;
}

}
2 changes: 1 addition & 1 deletion src/Grid/Columns/DataColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function getHeadContent()
{
$label = $this->getLabel();
if (($translator = $this->container->getTranslator()) !== null){
$label = $translator->trans($label, []);
$label = $translator->trans($label, [], $this->translationDomain);
}
return ucfirst($label);
}
Expand Down
11 changes: 6 additions & 5 deletions src/Grid/DataGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public function __construct(
$this->configuration = $defaultConfiguration->getInstance($builder->getInstance())->merge($builder->getConfiguration());
$this->setTemplate($this->configuration->getTemplate());
$this->configurePagerOptions();
if (!empty($this->configuration->getTranslationDomain())){
foreach ($this->builder->getColumns() as $column){
$column->setTranslationDomain($this->configuration->getTranslationDomain());
}
}
}

/**
Expand All @@ -71,6 +76,7 @@ protected function configurePagerOptions()
$pager->setLimit($this->configuration->getPaginationLimit());
if ($this->configuration->getPaginationEnabled()){
$pager->enable();
$pager->setTotalCount($this->getProvider()->getTotalCount());
} else {
$pager->disable();
}
Expand Down Expand Up @@ -151,11 +157,6 @@ public function getNoDataMessage()
: ucfirst($this->configuration->getNoDataMessage());
}

protected function setNoDataMessage(string $message)
{
$this->configuration->setNoDataMessage($message);
}

public function hasPagination()
{
return $this->builder->hasPagination();
Expand Down
6 changes: 4 additions & 2 deletions src/Grid/DataGridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ public function setShowTitles(bool $flag): DataGridBuilderInterface
return $this;
}

public function enablePagination(bool $enabled = true, int $limit = 10): DataGridBuilderInterface
public function enablePagination(bool $enabled = true, ?int $limit = null): DataGridBuilderInterface
{
$this->configuration->setPaginationEnabled($enabled);
$this->configuration->setPaginationLimit($limit);
if ($limit !== null){
$this->configuration->setPaginationLimit($limit);
}
return $this;
}

Expand Down
38 changes: 6 additions & 32 deletions src/Grid/DataGridFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ public function createGrid(string $gridType, $dataProvider): DataGrid
/** @var AbstractGridType $type */
$this->gridType = new $gridType($this->container);
$this->gridType->buildGrid($this->gridBuilder);
//TODO move handle request to grid init
$this->handleRequest();
if ($this->gridBuilder->hasPagination()) {
$this->gridBuilder->getPager()->setTotalCount($provider->getTotalCount());
}

return new DataGrid($this->gridBuilder, $this->defaultConfiguration, $this->container);
}
Expand All @@ -93,13 +91,11 @@ protected function handleSorting()

protected function handlePagination()
{
if ($this->gridBuilder->hasPagination()) {
if (array_key_exists('page', $this->queryParams)) {
$this->setPage($this->queryParams['page']);
unset($this->queryParams['page']);
} else {
$this->setPage(1);
}
if (array_key_exists('page', $this->queryParams)) {
$this->setPage($this->queryParams['page']);
unset($this->queryParams['page']);
} else {
$this->setPage(1);
}
}

Expand All @@ -124,26 +120,4 @@ protected function setPage($page)
{
$this->gridBuilder->getPager()->setPage(is_numeric($page) ? (int)$page : 1);
}

protected function setDefaultTemplate($template)
{
$this->gridBuilder->setTemplate($template);
}

protected function setDefaultNoDataMessage($message)
{
$this->gridBuilder->setNoDataMessage($message);
}

protected function setDefaultPagination($pagination)
{
if ($pagination['enabled']) {
$this->gridBuilder->enablePagination($pagination['options']);
}
}

protected function setDefaultShowTitles($showTitles)
{
$this->gridBuilder->setShowTitles($showTitles);
}
}
19 changes: 19 additions & 0 deletions tests/DataGridBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,23 @@ public function testSetFiltersValues($builder): void
}
}
}

public function testConfiguration(): void
{
$this->builder->setNoDataMessage('Test Message');
$this->builder->setTemplate('test_template');
$this->builder->setShowTitles(false);
$this->builder->enablePagination(true, 15);
$this->assertEquals([
'template' => 'test_template',
'paginationEnabled' => true,
'paginationLimit' => 15,
'noDataMessage' => 'Test Message',
'showTitles' => false,
'translationDomain' => null
], $this->builder->getConfiguration()->getConfigsArray());

$this->builder->setInstance('test');
$this->assertEquals('test', $this->builder->getInstance());
}
}
18 changes: 8 additions & 10 deletions tests/DataGridFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
use InvalidArgumentException;
use Pfilsx\DataGrid\Config\ConfigurationContainer;
use Pfilsx\DataGrid\DataGridServiceContainer;
use Pfilsx\DataGrid\Grid\AbstractGridType;
use Pfilsx\DataGrid\Grid\DataGridFactory;
use Pfilsx\tests\app\Entity\Node;
use Pfilsx\tests\app\Grid\NodeGridType;
use Pfilsx\tests\app\Grid\NodeGridType2;
use Pfilsx\tests\OrmTestCase;
use Pfilsx\tests\TestEntities\Node;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Template;
Expand Down Expand Up @@ -65,18 +66,18 @@ public function testWrongGridTypeException(): void

public function testCreateGrid(): void
{
$grid = $this->factory->createGrid(get_class($this->createMock(AbstractGridType::class)), $this->getEntityManager()->getRepository(Node::class));
$grid = $this->factory->createGrid(NodeGridType::class, $this->getEntityManager()->getRepository(Node::class));
$this->assertEquals('empty', $grid->getNoDataMessage());
$this->assertTrue($grid->hasPagination());
$this->assertInstanceOf(Template::class, $grid->getTemplate());
$this->assertIsArray($grid->getData());
$this->assertNotEmpty($grid->getData());
$this->assertFalse($grid->hasFilters());
$this->assertEmpty($grid->getColumns());
$this->assertNotEmpty($grid->getColumns());
$this->assertFalse($grid->getShowTitles());
$this->assertEquals([
'currentPage' => 1,
'pages' => []
'pages' => [1,2,3]
], $grid->getPaginationOptions());

$request = new Request();
Expand All @@ -96,11 +97,8 @@ public function testCreateGrid(): void
static::$kernel->getContainer()->get('translator')
);
$factory2 = new DataGridFactory($container, $this->configuration);
$grid2 = $factory2->createGrid(get_class($this->createMock(AbstractGridType::class)), $this->getEntityManager()->getRepository(Node::class));
$grid2 = $factory2->createGrid(NodeGridType2::class, $this->getEntityManager()->getRepository(Node::class));

$this->assertEquals([
'currentPage' => 1,
'pages' => []
], $grid2->getPaginationOptions());
$this->assertFalse($grid2->hasPagination());
}
}
4 changes: 2 additions & 2 deletions tests/DataGridFiltersBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Doctrine\Common\Collections\Expr\Expression;
use Pfilsx\DataGrid\Grid\DataGridFiltersBuilder;
use Pfilsx\DataGrid\Grid\Providers\DataProvider;
use Pfilsx\tests\app\Entity\Node;
use Pfilsx\tests\app\Entity\NodeAssoc;
use Pfilsx\tests\OrmTestCase;
use Pfilsx\tests\TestEntities\Node;
use Pfilsx\tests\TestEntities\NodeAssoc;

class DataGridFiltersBuilderTest extends OrmTestCase
{
Expand Down
4 changes: 2 additions & 2 deletions tests/OrmTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use DateTime;
use Doctrine\ORM\EntityManager;
use Pfilsx\DataGrid\DataGridServiceContainer;
use Pfilsx\tests\TestEntities\Node;
use Pfilsx\tests\TestEntities\NodeAssoc;
use Pfilsx\tests\app\Entity\Node;
use Pfilsx\tests\app\Entity\NodeAssoc;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
Expand Down
2 changes: 1 addition & 1 deletion tests/TestEntities/Node.php → tests/app/Entity/Node.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Pfilsx\tests\TestEntities;
namespace Pfilsx\tests\app\Entity;

class Node
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php


namespace Pfilsx\tests\TestEntities;
namespace Pfilsx\tests\app\Entity;


use Doctrine\Common\Collections\ArrayCollection;
Expand Down
33 changes: 33 additions & 0 deletions tests/app/Grid/NodeGridType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php


namespace Pfilsx\tests\app\Grid;


use Pfilsx\DataGrid\Grid\AbstractGridType;
use Pfilsx\DataGrid\Grid\DataGridBuilderInterface;
use Pfilsx\DataGrid\Grid\DataGridFiltersBuilderInterface;

class NodeGridType extends AbstractGridType
{

public function buildGrid(DataGridBuilderInterface $builder): void
{
$builder
->addColumn('id')
->addColumn('user')
->addColumn('content')
->addColumn('createdAt', self::DATE_COLUMN, [
'dateFormat' => 'Y.m.d'
])
->addDataColumn('parentId')
->addActionColumn([
'prefix' => 'test_prefix_'
]);
}

public function handleFilters(DataGridFiltersBuilderInterface $builder, array $filters): void
{

}
}
34 changes: 34 additions & 0 deletions tests/app/Grid/NodeGridType2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php


namespace Pfilsx\tests\app\Grid;


use Pfilsx\DataGrid\Grid\AbstractGridType;
use Pfilsx\DataGrid\Grid\DataGridBuilderInterface;
use Pfilsx\DataGrid\Grid\DataGridFiltersBuilderInterface;

class NodeGridType2 extends AbstractGridType
{

public function buildGrid(DataGridBuilderInterface $builder): void
{
$builder
->addColumn('id')
->addColumn('user')
->addColumn('content')
->addColumn('createdAt', self::DATE_COLUMN, [
'dateFormat' => 'Y.m.d'
])
->addDataColumn('parentId')
->addActionColumn([
'prefix' => 'test_prefix_'
])
->enablePagination(false);
}

public function handleFilters(DataGridFiltersBuilderInterface $builder, array $filters): void
{

}
}
8 changes: 4 additions & 4 deletions tests/app/Resources/config/doctrine/Node.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Pfilsx\tests\TestEntities\Node" table="node">
<entity name="Pfilsx\tests\app\Entity\Node" table="node">

<id name="id" type="integer" column="id"/>
<field name="content" column="content" type="text" nullable="true"/>
<field name="user" column="user" type="text" nullable="true"/>
<field name="createdAt" column="created_at" type="datetime"/>
<field name="parentId" column="parent_id" type="integer" nullable="true"/>

<one-to-many field="subNodeList" target-entity="Pfilsx\tests\TestEntities\Node" mapped-by="mainNode"/>
<many-to-one field="mainNode" target-entity="Pfilsx\tests\TestEntities\Node" inversed-by="subNodeList">
<one-to-many field="subNodeList" target-entity="Pfilsx\tests\app\Entity\Node" mapped-by="mainNode"/>
<many-to-one field="mainNode" target-entity="Pfilsx\tests\app\Entity\Node" inversed-by="subNodeList">
<join-column name="mainNodeId" referenced-column-name="id"/>
</many-to-one>
<many-to-one field="assoc" target-entity="Pfilsx\tests\TestEntities\NodeAssoc" inversed-by="nodeList">
<many-to-one field="assoc" target-entity="Pfilsx\tests\app\Entity\NodeAssoc" inversed-by="nodeList">
<join-column name="assoc_id" referenced-column-name="id"/>
</many-to-one>

Expand Down
4 changes: 2 additions & 2 deletions tests/app/Resources/config/doctrine/NodeAssoc.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Pfilsx\tests\TestEntities\NodeAssoc" table="node_assoc">
<entity name="Pfilsx\tests\app\Entity\NodeAssoc" table="node_assoc">

<id name="id" type="integer" column="id"/>
<id name="name" type="string" column="name"/>

<one-to-many field="nodeList" mapped-by="assoc" target-entity="Pfilsx\tests\TestEntities\Node"/>
<one-to-many field="nodeList" mapped-by="assoc" target-entity="Pfilsx\tests\app\Entity\Node"/>

</entity>

Expand Down
4 changes: 2 additions & 2 deletions tests/app/config/config_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ doctrine:
entity_managers:
default:
mappings:
Pfilsx\tests\TestEntities\Node:
Pfilsx\tests\app\Entity\Node:
type: xml
dir: "%kernel.root_dir%/app/Resources/config/doctrine"
prefix: Pfilsx\tests\TestEntities
prefix: Pfilsx\tests\app\Entity
3 changes: 2 additions & 1 deletion tests/columns/ActionColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use Pfilsx\DataGrid\DataGridException;
use Pfilsx\DataGrid\Grid\Columns\ActionColumn;
use Pfilsx\DataGrid\Grid\Items\EntityGridItem;
use Pfilsx\tests\app\Entity\Node;
use Pfilsx\tests\OrmTestCase;
use Pfilsx\tests\TestEntities\Node;


/**
* Class ActionColumnTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Pfilsx\DataGrid\Config\ConfigurationContainer;
use PHPUnit\Framework\TestCase;

class DataGridConfigurationTest extends TestCase
class ConfigurationContainerTest extends TestCase
{
/**
* @param $config
Expand Down

0 comments on commit 53508c4

Please sign in to comment.