l11n, unit/app and simplification fixes

This commit is contained in:
Dennis Eichhorn 2023-01-07 19:00:33 +01:00
parent 8e670f3a27
commit 41a2ea4492
12 changed files with 41 additions and 349 deletions

View File

@ -15,7 +15,7 @@ declare(strict_types=1);
namespace Modules\Tag\Controller;
use Modules\Tag\Models\Tag;
use Modules\Tag\Models\TagL11n;
use phpOMS\Localization\BaseStringL11n;
use Modules\Tag\Models\TagL11nMapper;
use Modules\Tag\Models\TagMapper;
use phpOMS\Message\Http\RequestStatusCode;
@ -201,18 +201,18 @@ final class ApiController extends Controller
*
* @param RequestAbstract $request Request
*
* @return TagL11n
* @return BaseStringL11n
*
* @since 1.0.0
*/
private function createTagL11nFromRequest(RequestAbstract $request) : TagL11n
private function createTagL11nFromRequest(RequestAbstract $request) : BaseStringL11n
{
$tagL11n = new TagL11n();
$tagL11n->tag = (int) ($request->getData('tag') ?? 0);
$tagL11n = new BaseStringL11n();
$tagL11n->ref = (int) ($request->getData('tag') ?? 0);
$tagL11n->setLanguage((string) (
$request->getData('language') ?? $request->getLanguage()
));
$tagL11n->title = (string) ($request->getData('title') ?? '');
$tagL11n->content = (string) ($request->getData('title') ?? '');
return $tagL11n;
}
@ -277,7 +277,7 @@ final class ApiController extends Controller
$tags = TagMapper::getAll()
->with('title')
->where('title/language', $request->getLanguage())
->where('title/title', '%' . ($request->getData('search') ?? '') . '%', 'LIKE')
->where('title/content', '%' . ($request->getData('search') ?? '') . '%', 'LIKE')
->execute();
$response->header->set('Content-Type', MimeType::M_JSON, true);

View File

@ -113,7 +113,7 @@ final class BackendController extends Controller
$view->addData('nav', $this->app->moduleManager->get('Navigation')->createNavigationMid(1007501001, $request, $response));
$view->addData('tag', $tag);
/** @var \Modules\Tag\Models\TagL11n[] $l11n */
/** @var \phpOMS\Localization\BaseStringL11n[] $l11n */
$l11n = TagL11nMapper::getAll()->where('tag', $tag->getId())->execute();
$view->addData('l11n', $l11n);

View File

@ -1,47 +0,0 @@
<?php
/**
* Karaka
*
* PHP Version 8.1
*
* @package Modules\Tag\Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace Modules\Tag\Models;
/**
* Null model
*
* @package Modules\Tag\Models
* @license OMS License 1.0
* @link https://jingga.app
* @since 1.0.0
*/
final class NullTagL11n extends TagL11n
{
/**
* Constructor
*
* @param int $id Model id
*
* @since 1.0.0
*/
public function __construct(int $id = 0)
{
$this->id = $id;
parent::__construct();
}
/**
* {@inheritdoc}
*/
public function jsonSerialize() : mixed
{
return ['id' => $this->id];
}
}

View File

@ -16,6 +16,7 @@ namespace Modules\Tag\Models;
use Modules\Admin\Models\Account;
use phpOMS\Localization\ISO639x1Enum;
use phpOMS\Localization\BaseStringL11n;
/**
* Tag class.
@ -38,10 +39,10 @@ class Tag implements \JsonSerializable
/**
* Title.
*
* @var string|TagL11n
* @var string|BaseStringL11n
* @since 1.0.0
*/
protected string | TagL11n $title = '';
protected string | BaseStringL11n $title = '';
/**
* Color RGBA.
@ -120,28 +121,28 @@ class Tag implements \JsonSerializable
*/
public function getL11n() : string
{
return $this->title instanceof TagL11n ? $this->title->title : $this->title;
return $this->title instanceof BaseStringL11n ? $this->title->content : $this->title;
}
/**
* Set title
*
* @param string|TagL11n $title Tag article title
* @param string $lang Language
* @param string|BaseStringL11n $title Tag article title
* @param string $lang Language
*
* @return void
*
* @since 1.0.0
*/
public function setL11n(string | TagL11n $title, string $lang = ISO639x1Enum::_EN) : void
public function setL11n(string | BaseStringL11n $title, string $lang = ISO639x1Enum::_EN) : void
{
if ($title instanceof TagL11n) {
if ($title instanceof BaseStringL11n) {
$this->title = $title;
} elseif (isset($this->title) && $this->title instanceof TagL11n) {
$this->title->title = $title;
} elseif (isset($this->title) && $this->title instanceof BaseStringL11n) {
$this->title->content = $title;
} else {
$this->title = new TagL11n();
$this->title->title = $title;
$this->title = new BaseStringL11n();
$this->title->content = $title;
$this->title->setLanguage($lang);
}
}

View File

@ -1,132 +0,0 @@
<?php
/**
* Karaka
*
* PHP Version 8.1
*
* @package Modules\Tag\Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace Modules\Tag\Models;
use phpOMS\Localization\ISO639x1Enum;
/**
* Tag class.
*
* @package Modules\Tag\Models
* @license OMS License 1.0
* @link https://jingga.app
* @since 1.0.0
*/
class TagL11n implements \JsonSerializable
{
/**
* ID.
*
* @var int
* @since 1.0.0
*/
protected int $id = 0;
/**
* Tag ID.
*
* @var int
* @since 1.0.0
*/
public int $tag = 0;
/**
* Language.
*
* @var string
* @since 1.0.0
*/
protected string $language = ISO639x1Enum::_EN;
/**
* Title.
*
* @var string
* @since 1.0.0
*/
public string $title = '';
/**
* Constructor.
*
* @param string $title Title
*
* @since 1.0.0
*/
public function __construct(string $title = '', string $language = ISO639x1Enum::_EN)
{
$this->title = $title;
$this->language = $language;
}
/**
* Get id
*
* @return int
*
* @since 1.0.0
*/
public function getId() : int
{
return $this->id;
}
/**
* Get language
*
* @return string
*
* @since 1.0.0
*/
public function getLanguage() : string
{
return $this->language;
}
/**
* Set language
*
* @param string $language Language
*
* @return void
*
* @since 1.0.0
*/
public function setLanguage(string $language) : void
{
$this->language = $language;
}
/**
* {@inheritdoc}
*/
public function toArray() : array
{
return [
'id' => $this->id,
'title' => $this->title,
'tag' => $this->tag,
'language' => $this->language,
];
}
/**
* {@inheritdoc}
*/
public function jsonSerialize() : mixed
{
return $this->toArray();
}
}

View File

@ -15,6 +15,7 @@ declare(strict_types=1);
namespace Modules\Tag\Models;
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
use phpOMS\Localization\BaseStringL11n;
/**
* Tag mapper class.
@ -34,8 +35,8 @@ final class TagL11nMapper extends DataMapperFactory
*/
public const COLUMNS = [
'tag_l11n_id' => ['name' => 'tag_l11n_id', 'type' => 'int', 'internal' => 'id'],
'tag_l11n_title' => ['name' => 'tag_l11n_title', 'type' => 'string', 'internal' => 'title', 'autocomplete' => true],
'tag_l11n_tag' => ['name' => 'tag_l11n_tag', 'type' => 'int', 'internal' => 'tag'],
'tag_l11n_title' => ['name' => 'tag_l11n_title', 'type' => 'string', 'internal' => 'content', 'autocomplete' => true],
'tag_l11n_tag' => ['name' => 'tag_l11n_tag', 'type' => 'int', 'internal' => 'ref'],
'tag_l11n_language' => ['name' => 'tag_l11n_language', 'type' => 'string', 'internal' => 'language'],
];
@ -54,4 +55,12 @@ final class TagL11nMapper extends DataMapperFactory
* @since 1.0.0
*/
public const PRIMARYFIELD ='tag_l11n_id';
/**
* Model to use by the mapper.
*
* @var string
* @since 1.0.0
*/
public const MODEL = BaseStringL11n::class;
}

View File

@ -51,7 +51,7 @@ final class TagMapper extends DataMapperFactory
'mapper' => TagL11nMapper::class,
'table' => 'tag_l11n',
'self' => 'tag_l11n_tag',
'column' => 'title',
'column' => 'content',
'external' => null,
],
];

View File

@ -143,7 +143,7 @@ final class ApiControllerTest extends \PHPUnit\Framework\TestCase
$this->module->apiTagL11nCreate($request, $response);
self::assertEquals('ApiTagDE', $response->get('')['response']->title);
self::assertEquals('ApiTagDE', $response->get('')['response']->content);
self::assertGreaterThan(0, $response->get('')['response']->getId());
}

View File

@ -1,42 +0,0 @@
<?php
/**
* Karaka
*
* PHP Version 8.1
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace Modules\Tag\tests\Models;
use Modules\Tag\Models\NullTagL11n;
/**
* @internal
*/
final class NullTagL11nTest extends \PHPUnit\Framework\TestCase
{
/**
* @covers Modules\Tag\Models\NullTagL11n
* @group framework
*/
public function testNull() : void
{
self::assertInstanceOf('\Modules\Tag\Models\TagL11n', new NullTagL11n());
}
/**
* @covers Modules\Tag\Models\NullTagL11n
* @group framework
*/
public function testId() : void
{
$null = new NullTagL11n(2);
self::assertEquals(2, $null->getId());
}
}

View File

@ -15,7 +15,7 @@ declare(strict_types=1);
namespace Modules\Tag\tests\Models;
use Modules\Tag\Models\Tag;
use Modules\Tag\Models\TagL11n;
use phpOMS\Localization\BaseStringL11n;
use Modules\Tag\Models\TagL11nMapper;
use Modules\Tag\Models\TagMapper;
use Modules\Tag\Models\TagType;
@ -40,17 +40,17 @@ final class TagL11nMapperTest extends \PHPUnit\Framework\TestCase
self::assertGreaterThan(0, $tag->getId());
self::assertEquals($id, $tag->getId());
$l11n = new TagL11n();
$l11n->title = 'TestTitle';
$l11n = new BaseStringL11n();
$l11n->content = 'TestTitle';
$l11n->setLanguage(ISO639x1Enum::_EN);
$l11n->tag = $id;
$l11n->ref = $id;
$id = TagL11nMapper::create()->execute($l11n);
self::assertGreaterThan(0, $l11n->getId());
self::assertEquals($id, $l11n->getId());
$l11nR = TagL11nMapper::get()->where('id', $l11n->getId())->execute();
self::assertEquals($l11n->title, $l11nR->title);
self::assertEquals($l11n->content, $l11nR->content);
self::assertEquals($l11n->getLanguage(), $l11nR->getLanguage());
}
}

View File

@ -1,97 +0,0 @@
<?php
/**
* Karaka
*
* PHP Version 8.1
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace Modules\Tag\tests\Models;
use Modules\Tag\Models\TagL11n;
use phpOMS\Localization\ISO639x1Enum;
/**
* @internal
*/
final class TagL11nTest extends \PHPUnit\Framework\TestCase
{
private TagL11n $l11n;
/**
* {@inheritdoc}
*/
protected function setUp() : void
{
$this->l11n = new TagL11n();
}
/**
* @covers Modules\Tag\Models\TagL11n
* @group module
*/
public function testDefault() : void
{
self::assertEquals(0, $this->l11n->getId());
self::assertEquals(0, $this->l11n->tag);
self::assertEquals('', $this->l11n->title);
self::assertEquals(ISO639x1Enum::_EN, $this->l11n->getLanguage());
}
/**
* @covers Modules\Tag\Models\TagL11n
* @group module
*/
public function testTagInputOutput() : void
{
$this->l11n->tag = 2;
self::assertEquals(2, $this->l11n->tag);
}
/**
* @covers Modules\Tag\Models\TagL11n
* @group module
*/
public function testLanguageInputOutput() : void
{
$this->l11n->setLanguage(ISO639x1Enum::_DE);
self::assertEquals(ISO639x1Enum::_DE, $this->l11n->getLanguage());
}
/**
* @covers Modules\Tag\Models\TagL11n
* @group module
*/
public function testTitleInputOutput() : void
{
$this->l11n->title = 'Title';
self::assertEquals('Title', $this->l11n->title);
}
/**
* @covers Modules\Tag\Models\TagL11n
* @group module
*/
public function testSerialize() : void
{
$this->l11n->title = 'Title';
$this->l11n->tag = 2;
$this->l11n->setLanguage(ISO639x1Enum::_DE);
self::assertEquals(
[
'id' => 0,
'title' => 'Title',
'tag' => 2,
'language' => ISO639x1Enum::_DE,
],
$this->l11n->jsonSerialize()
);
}
}

View File

@ -16,7 +16,7 @@ namespace Modules\Tag\tests\Models;
use Modules\Admin\Models\NullAccount;
use Modules\Tag\Models\Tag;
use Modules\Tag\Models\TagL11n;
use phpOMS\Localization\BaseStringL11n;
use Modules\Tag\Models\TagType;
/**
@ -56,7 +56,7 @@ final class TagTest extends \PHPUnit\Framework\TestCase
$this->tag->setL11n('Test');
self::assertEquals('Test', $this->tag->getL11n());
$this->tag->setL11n(new TagL11n('Test2'));
$this->tag->setL11n(new BaseStringL11n('Test2'));
self::assertEquals('Test2', $this->tag->getL11n());
$this->tag->setL11n('Test3');
@ -99,7 +99,7 @@ final class TagTest extends \PHPUnit\Framework\TestCase
*/
public function testSerialize() : void
{
$this->tag->setL11n($t = new TagL11n('Test'));
$this->tag->setL11n($t = new BaseStringL11n('Test'));
$this->tag->owner = new NullAccount(2);
$this->tag->color = 'ffffffff';
$this->tag->setType(TagType::SHARED);