Use [[PLACEHOLDER]] instead of %%PLACEHOLDER%% for label placeholders.

This commit is contained in:
Jan Böhmer 2020-04-25 18:19:15 +02:00
parent cb2d0d9845
commit 3163a7ba09
14 changed files with 80 additions and 78 deletions

View file

@ -65,7 +65,7 @@ class LabelTextReplacer
public function replace(string $lines, object $target): string public function replace(string $lines, object $target): string
{ {
$patterns = [ $patterns = [
'/(%%[A-Z_]+%%)/' => function ($match) use ($target) { '/(\[\[[A-Z_]+\]\])/' => function ($match) use ($target) {
return $this->handlePlaceholder($match[0], $target); return $this->handlePlaceholder($match[0], $target);
}, },
]; ];

View file

@ -40,11 +40,11 @@ class AbstractDBElementProvider implements PlaceholderProviderInterface
{ {
if ($label_target instanceof AbstractDBElement) { if ($label_target instanceof AbstractDBElement) {
if ($placeholder === '%%TYPE%%') { if ($placeholder === '[[TYPE]]') {
return $this->elementTypeNameGenerator->getLocalizedTypeLabel($label_target); return $this->elementTypeNameGenerator->getLocalizedTypeLabel($label_target);
} }
if ($placeholder === '%%ID%%') { if ($placeholder === '[[ID]]') {
return (string) ($label_target->getID() ?? 'unknown'); return (string) ($label_target->getID() ?? 'unknown');
} }

View file

@ -47,20 +47,20 @@ class GlobalProviders implements PlaceholderProviderInterface
*/ */
public function replace(string $placeholder, object $label_target, array $options = []): ?string public function replace(string $placeholder, object $label_target, array $options = []): ?string
{ {
if ($placeholder === "%%INSTALL_NAME%%") { if ($placeholder === "[[INSTALL_NAME]]") {
return $this->partdb_title; return $this->partdb_title;
} }
$user = $this->security->getUser(); $user = $this->security->getUser();
if ($placeholder === "%%USERNAME%%") { if ($placeholder === "[[USERNAME]]") {
if ($user instanceof User) { if ($user instanceof User) {
return $user->getName(); return $user->getName();
} }
return 'anonymous'; return 'anonymous';
} }
if ($placeholder === "%%USERNAME_FULL%%") { if ($placeholder === "[[USERNAME_FULL]]") {
if ($user instanceof User) { if ($user instanceof User) {
return $user->getFullName(true); return $user->getFullName(true);
} }
@ -69,7 +69,7 @@ class GlobalProviders implements PlaceholderProviderInterface
$now = new \DateTime(); $now = new \DateTime();
if ($placeholder === '%%DATETIME%%') { if ($placeholder === '[[DATETIME]]') {
$formatter = IntlDateFormatter::create( $formatter = IntlDateFormatter::create(
Locale::getDefault(), Locale::getDefault(),
IntlDateFormatter::SHORT, IntlDateFormatter::SHORT,
@ -80,7 +80,7 @@ class GlobalProviders implements PlaceholderProviderInterface
return $formatter->format($now); return $formatter->format($now);
} }
if ($placeholder === '%%DATE%%') { if ($placeholder === '[[DATE]]') {
$formatter = IntlDateFormatter::create( $formatter = IntlDateFormatter::create(
Locale::getDefault(), Locale::getDefault(),
IntlDateFormatter::SHORT, IntlDateFormatter::SHORT,
@ -91,7 +91,7 @@ class GlobalProviders implements PlaceholderProviderInterface
return $formatter->format($now); return $formatter->format($now);
} }
if ($placeholder === '%%TIME%%') { if ($placeholder === '[[TIME]]') {
$formatter = IntlDateFormatter::create( $formatter = IntlDateFormatter::create(
Locale::getDefault(), Locale::getDefault(),
IntlDateFormatter::NONE, IntlDateFormatter::NONE,

View file

@ -31,7 +31,7 @@ class NamedElementProvider implements PlaceholderProviderInterface
*/ */
public function replace(string $placeholder, object $label_target, array $options = []): ?string public function replace(string $placeholder, object $label_target, array $options = []): ?string
{ {
if ($label_target instanceof NamedElementInterface && $placeholder === '%%NAME%%') { if ($label_target instanceof NamedElementInterface && $placeholder === '[[NAME]]') {
return $label_target->getName(); return $label_target->getName();
} }

View file

@ -41,19 +41,19 @@ class PartLotProvider implements PlaceholderProviderInterface
public function replace(string $placeholder, object $label_target, array $options = []): ?string public function replace(string $placeholder, object $label_target, array $options = []): ?string
{ {
if ($label_target instanceof PartLot) { if ($label_target instanceof PartLot) {
if ($placeholder === '%%LOT_ID%%') { if ($placeholder === '[[LOT_ID]]') {
return $label_target->getID() ?? 'unknown'; return $label_target->getID() ?? 'unknown';
} }
if ($placeholder === '%%LOT_NAME%%') { if ($placeholder === '[[LOT_NAME]]') {
return $label_target->getName(); return $label_target->getName();
} }
if ($placeholder === '%%LOT_COMMENT%%') { if ($placeholder === '[[LOT_COMMENT]]') {
return $label_target->getComment(); return $label_target->getComment();
} }
if ($placeholder === '%%EXPIRATION_DATE%%') { if ($placeholder === '[[EXPIRATION_DATE]]') {
if ($label_target->getExpirationDate() === null) { if ($label_target->getExpirationDate() === null) {
return ''; return '';
} }
@ -67,18 +67,18 @@ class PartLotProvider implements PlaceholderProviderInterface
return $formatter->format($label_target->getExpirationDate()); return $formatter->format($label_target->getExpirationDate());
} }
if ($placeholder === '%%AMOUNT%%') { if ($placeholder === '[[AMOUNT]]') {
if ($label_target->isInstockUnknown()) { if ($label_target->isInstockUnknown()) {
return '?'; return '?';
} }
return $this->amountFormatter->format($label_target->getAmount(), $label_target->getPart()->getPartUnit()); return $this->amountFormatter->format($label_target->getAmount(), $label_target->getPart()->getPartUnit());
} }
if ($placeholder === '%%LOCATION%%') { if ($placeholder === '[[LOCATION]]') {
return $label_target->getStorageLocation() ? $label_target->getStorageLocation()->getName() : ''; return $label_target->getStorageLocation() ? $label_target->getStorageLocation()->getName() : '';
} }
if ($placeholder === '%%LOCATION_FULL%%') { if ($placeholder === '[[LOCATION_FULL]]') {
return $label_target->getStorageLocation() ? $label_target->getStorageLocation()->getFullPath() : ''; return $label_target->getStorageLocation() ? $label_target->getStorageLocation()->getFullPath() : '';
} }

View file

@ -46,43 +46,43 @@ class PartProvider implements PlaceholderProviderInterface
return null; return null;
} }
if ($placeholder === '%%CATEGORY%%') { if ($placeholder === '[[CATEGORY]]') {
return $part->getCategory() ? $part->getCategory()->getName() : ''; return $part->getCategory() ? $part->getCategory()->getName() : '';
} }
if ($placeholder === '%%CATEGORY_FULL%%') { if ($placeholder === '[[CATEGORY_FULL]]') {
return $part->getCategory() ? $part->getCategory()->getFullPath() : ''; return $part->getCategory() ? $part->getCategory()->getFullPath() : '';
} }
if ($placeholder === '%%MANUFACTURER%%') { if ($placeholder === '[[MANUFACTURER]]') {
return $part->getManufacturer() ? $part->getManufacturer()->getName() : ''; return $part->getManufacturer() ? $part->getManufacturer()->getName() : '';
} }
if ($placeholder === '%%MANUFACTURER_FULL%%') { if ($placeholder === '[[MANUFACTURER_FULL]]') {
return $part->getManufacturer() ? $part->getManufacturer()->getFullPath() : ''; return $part->getManufacturer() ? $part->getManufacturer()->getFullPath() : '';
} }
if ($placeholder === '%%FOOTPRINT%%') { if ($placeholder === '[[FOOTPRINT]]') {
return $part->getFootprint() ? $part->getFootprint()->getName() : ''; return $part->getFootprint() ? $part->getFootprint()->getName() : '';
} }
if ($placeholder === '%%FOOTPRINT_FULL%%') { if ($placeholder === '[[FOOTPRINT_FULL]]') {
return $part->getFootprint() ? $part->getFootprint()->getFullPath() : ''; return $part->getFootprint() ? $part->getFootprint()->getFullPath() : '';
} }
if ($placeholder === '%%MASS%%') { if ($placeholder === '[[MASS]]') {
return $part->getMass() ? $this->siFormatter->format($part->getMass(), 'g', 1) : ''; return $part->getMass() ? $this->siFormatter->format($part->getMass(), 'g', 1) : '';
} }
if ($placeholder === '%%MPN%%') { if ($placeholder === '[[MPN]]') {
return $part->getManufacturerProductNumber(); return $part->getManufacturerProductNumber();
} }
if ($placeholder === '%%TAGS%%') { if ($placeholder === '[[TAGS]]') {
return $part->getTags(); return $part->getTags();
} }
if ($placeholder === '%%M_STATUS%%') { if ($placeholder === '[[M_STATUS]]') {
if ($part->getManufacturingStatus() === '') { if ($part->getManufacturingStatus() === '') {
return ''; return '';
} }
@ -91,19 +91,19 @@ class PartProvider implements PlaceholderProviderInterface
$parsedown = new \Parsedown(); $parsedown = new \Parsedown();
if ($placeholder === '%%DESCRIPTION%%') { if ($placeholder === '[[DESCRIPTION]]') {
return $parsedown->line($part->getDescription()); return $parsedown->line($part->getDescription());
} }
if ($placeholder === '%%DESCRIPTION_T%%') { if ($placeholder === '[[DESCRIPTION_T]]') {
return strip_tags($parsedown->line($part->getDescription())); return strip_tags($parsedown->line($part->getDescription()));
} }
if ($placeholder === '%%COMMENT%%') { if ($placeholder === '[[COMMENT]]') {
return $parsedown->line($part->getComment()); return $parsedown->line($part->getComment());
} }
if ($placeholder === '%%COMMENT_T%%') { if ($placeholder === '[[COMMENT_T]]') {
return strip_tags($parsedown->line($part->getComment())); return strip_tags($parsedown->line($part->getComment()));
} }

View file

@ -33,11 +33,11 @@ class TimestampableElementProvider implements PlaceholderProviderInterface
public function replace(string $placeholder, object $label_target, array $options = []): ?string public function replace(string $placeholder, object $label_target, array $options = []): ?string
{ {
if ($label_target instanceof TimeStampableInterface) { if ($label_target instanceof TimeStampableInterface) {
if ($placeholder === '%%LAST_MODIFIED%%') { if ($placeholder === '[[LAST_MODIFIED]]') {
return IntlDateFormatter::formatObject($label_target->getLastModified() ?? new \DateTime(), IntlDateFormatter::SHORT, Locale::getDefault()); return IntlDateFormatter::formatObject($label_target->getLastModified() ?? new \DateTime(), IntlDateFormatter::SHORT, Locale::getDefault());
} }
if ($placeholder === '%%CREATION_DATE%%') { if ($placeholder === '[[CREATION_DATE]]') {
return IntlDateFormatter::formatObject($label_target->getAddedDate() ?? new \DateTime(), IntlDateFormatter::SHORT, Locale::getDefault()); return IntlDateFormatter::formatObject($label_target->getAddedDate() ?? new \DateTime(), IntlDateFormatter::SHORT, Locale::getDefault());
} }

View file

@ -55,28 +55,28 @@ class LabelTextReplacerTest extends WebTestCase
public function handlePlaceholderDataProvider(): array public function handlePlaceholderDataProvider(): array
{ {
return [ return [
['Part 1', '%%NAME%%'], ['Part 1', '[[NAME]]'],
['P Description', '%%DESCRIPTION%%'], ['P Description', '[[DESCRIPTION]]'],
['%%UNKNOWN%%', '%%UNKNOWN%%'], ['[[UNKNOWN]]', '[[UNKNOWN]]', '[[UNKNOWN]]'],
['%%INVALID', '%%INVALID'], ['[[INVALID', '[[INVALID'],
['%%', '%%'], ['[[', '[['],
['NAME', 'NAME'], ['NAME', 'NAME'],
['%%NAME', '%%NAME'], ['[[NAME', '[[NAME'],
['Test %%NAME%%', 'Test %%NAME%%'], ['Test [[NAME]]', 'Test [[NAME]]', 'Test [[NAME]]'],
]; ];
} }
public function replaceDataProvider(): array public function replaceDataProvider(): array
{ {
return [ return [
['Part 1', '%%NAME%%'], ['Part 1', '[[NAME]]'],
['TestPart 1', 'Test%%NAME%%'], ['TestPart 1', 'Test[[NAME]]'],
["P Description\nPart 1", "%%DESCRIPTION_T%%\n%%NAME%%"], ["P Description\nPart 1", "[[DESCRIPTION_T]]\n[[NAME]]"],
['Part 1 Part 1', '%%NAME%% %%NAME%%'], ['Part 1 Part 1', '[[NAME]] [[NAME]]'],
['%%UNKNOWN%% Test', '%%UNKNOWN%% Test'], ['[[UNKNOWN]] Test', '[[UNKNOWN]] Test'],
["%%NAME\n%% %%NAME %%", "%%NAME\n%% %%NAME %%"], ["[[NAME\n]] [[NAME ]]", "[[NAME\n]] [[NAME ]]"],
['%%%%', '%%%%'], ['[[]]', '[[]]'],
['TEST%% %%TEST', 'TEST%% %%TEST'] ['TEST[[ ]]TEST', 'TEST[[ ]]TEST']
]; ];
} }
@ -107,6 +107,6 @@ class LabelTextReplacerTest extends WebTestCase
$part->setName('Part'); $part->setName('Part');
$part_lot->setPart($part); $part_lot->setPart($part);
$this->assertSame('Part', $this->service->handlePlaceholder('%%NAME%%', $part_lot)); $this->assertSame('Part', $this->service->handlePlaceholder('[[NAME]]', $part_lot));
} }
} }

View file

@ -54,7 +54,7 @@ class AbstractElementProviderTest extends WebTestCase
public function dataProvider(): array public function dataProvider(): array
{ {
return [ return [
['123', '%%ID%%'], ['123', '[[ID]]'],
]; ];
} }

View file

@ -42,8 +42,8 @@ class GlobalProvidersTest extends WebTestCase
public function dataProvider(): array public function dataProvider(): array
{ {
return [ return [
['Part-DB', '%%INSTALL_NAME%%'], ['Part-DB', '[[INSTALL_NAME]]'],
['anonymous', '%%USERNAME%%'], ['anonymous', '[[USERNAME]]'],
]; ];
} }

View file

@ -53,7 +53,7 @@ class NamedElementProviderTest extends WebTestCase
public function dataProvider(): array public function dataProvider(): array
{ {
return [ return [
['This is my Name', '%%NAME%%'] ['This is my Name', '[[NAME]]']
]; ];
} }

View file

@ -60,16 +60,16 @@ class PartLotProviderTest extends WebTestCase
public function dataProvider(): array public function dataProvider(): array
{ {
return [ return [
['unknown', '%%LOT_ID%%'], ['unknown', '[[LOT_ID]]'],
['Lot description', '%%LOT_NAME%%'], ['Lot description', '[[LOT_NAME]]'],
['Lot comment', '%%LOT_COMMENT%%'], ['Lot comment', '[[LOT_COMMENT]]'],
['4/13/99', '%%EXPIRATION_DATE%%'], ['4/13/99', '[[EXPIRATION_DATE]]'],
['?', '%%AMOUNT%%'], ['?', '[[AMOUNT]]'],
['Location', '%%LOCATION%%'], ['Location', '[[LOCATION]]'],
['Parent → Location', '%%LOCATION_FULL%%'], ['Parent → Location', '[[LOCATION_FULL]]'],
//Test part inheritance //Test part inheritance
['Part', '%%NAME%%'], ['Part', '[[NAME]]'],
['Part description', '%%DESCRIPTION%%'], ['Part description', '[[DESCRIPTION]]'],
]; ];
} }

View file

@ -67,21 +67,21 @@ class PartProviderTest extends WebTestCase
public function dataProvider(): array public function dataProvider(): array
{ {
return [ return [
['Node 2.1', '%%CATEGORY%%'], ['Node 2.1', '[[CATEGORY]]'],
['Node 2 → Node 2.1', '%%CATEGORY_FULL%%'], ['Node 2 → Node 2.1', '[[CATEGORY_FULL]]'],
['Node 2.1', '%%FOOTPRINT%%'], ['Node 2.1', '[[FOOTPRINT]]'],
['Node 2 → Node 2.1', '%%FOOTPRINT_FULL%%'], ['Node 2 → Node 2.1', '[[FOOTPRINT_FULL]]'],
['', '%%MANUFACTURER%%'], ['', '[[MANUFACTURER]]'],
['', '%%MANUFACTURER_FULL%%'], ['', '[[MANUFACTURER_FULL]]'],
['1.2 kg', '%%MASS%%'], ['1.2 kg', '[[MASS]]'],
['MPN123', '%%MPN%%'], ['MPN123', '[[MPN]]'],
['SMD, Tag1, Tag2', '%%TAGS%%'], ['SMD, Tag1, Tag2', '[[TAGS]]'],
['Active', '%%M_STATUS%%'], ['Active', '[[M_STATUS]]'],
['<b>Bold</b> <em>Italic</em>', '%%DESCRIPTION%%'], ['<b>Bold</b> <em>Italic</em>', '[[DESCRIPTION]]'],
['Bold Italic', '%%DESCRIPTION_T%%'], ['Bold Italic', '[[DESCRIPTION_T]]'],
['<b>Bold</b> <em>Italic</em>', '%%COMMENT%%'], ['<b>Bold</b> <em>Italic</em>', '[[COMMENT]]'],
['Bold Italic', '%%COMMENT_T%%'], ['Bold Italic', '[[COMMENT_T]]'],
]; ];
} }

View file

@ -36,6 +36,7 @@ class TimestampableElementProviderTest extends WebTestCase
public function setUp(): void public function setUp(): void
{ {
self::bootKernel(); self::bootKernel();
\Locale::setDefault('en');
$this->service = self::$container->get(TimestampableElementProvider::class); $this->service = self::$container->get(TimestampableElementProvider::class);
$this->target = new class implements TimeStampableInterface { $this->target = new class implements TimeStampableInterface {
@ -59,11 +60,12 @@ class TimestampableElementProviderTest extends WebTestCase
public function dataProvider(): array public function dataProvider(): array
{ {
\Locale::setDefault('en');
$formatted = \IntlDateFormatter::formatObject(new \DateTime('2000-01-01'), \IntlDateFormatter::SHORT); $formatted = \IntlDateFormatter::formatObject(new \DateTime('2000-01-01'), \IntlDateFormatter::SHORT);
return [ return [
[$formatted, '%%LAST_MODIFIED%%'], [$formatted, '[[LAST_MODIFIED]]'],
[$formatted, '%%CREATION_DATE%%'], [$formatted, '[[CREATION_DATE]]'],
]; ];
} }