Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| AbstractCustomFieldsEncoder | |
0.00% |
0 / 24 |
|
0.00% |
0 / 10 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| encode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
0 | |||
| decode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
0 | |||
| getType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
0 | |||
| getBlock | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| upsertBlock | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| removeBlock | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getBlockStart | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getBlockEnd | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| buildPregMatch | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Satag\AmicronEntityBundle\Util\CustomFields; |
| 4 | |
| 5 | abstract class AbstractCustomFieldsEncoder |
| 6 | { |
| 7 | public function __construct( |
| 8 | protected string $dataIdentifier, |
| 9 | protected bool $old = false |
| 10 | ) { |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * @param array<string, mixed> $dataToEmbed |
| 15 | */ |
| 16 | abstract public function encode(string $originalString, array $dataToEmbed): string; |
| 17 | |
| 18 | /** |
| 19 | * @return array<string, mixed> |
| 20 | */ |
| 21 | abstract public function decode(string $rawString): array; |
| 22 | |
| 23 | abstract public function getType(): string; |
| 24 | |
| 25 | public function getBlock(string $string): string |
| 26 | { |
| 27 | preg_match($this->buildPregMatch(), $string, $matches); |
| 28 | |
| 29 | return $matches[1] ?? ''; |
| 30 | } |
| 31 | |
| 32 | public function upsertBlock(string $blockContents, string $originalString): string |
| 33 | { |
| 34 | // https://stackoverflow.com/questions/7836632/how-to-replace-different-newline-styles-in-php-the-smartest-way |
| 35 | $blockContents = (string) preg_replace('~(*BSR_ANYCRLF)\R~', "\r\n", $blockContents); |
| 36 | $blockStart = $this->getBlockStart(); |
| 37 | $blockEnd = $this->getBlockEnd(); |
| 38 | if (str_contains($originalString, (string) $blockStart) && str_contains($originalString, (string) $blockEnd)) { |
| 39 | return (string) preg_replace($this->buildPregMatch(), $blockContents, $originalString, 1); |
| 40 | } |
| 41 | |
| 42 | return $originalString . "\r\n" . $blockStart . $blockContents . $blockEnd; |
| 43 | } |
| 44 | |
| 45 | public function removeBlock(string $originalString): string |
| 46 | { |
| 47 | $pattern = '/(\\r\\n)?' . $this->getBlockStart() . '(.*)' . $this->getBlockEnd() . '/sm'; |
| 48 | |
| 49 | return (string) preg_replace($pattern, '', $originalString, 1); |
| 50 | } |
| 51 | |
| 52 | protected function getBlockStart(): string |
| 53 | { |
| 54 | if ($this->old === true) { |
| 55 | return \sprintf("# %s START\n", $this->dataIdentifier); |
| 56 | } |
| 57 | |
| 58 | return \sprintf("# Plattform %s %s START\r\n", $this->dataIdentifier, $this->getType()); |
| 59 | } |
| 60 | |
| 61 | protected function getBlockEnd(): string |
| 62 | { |
| 63 | if ($this->old === true) { |
| 64 | return \sprintf("\n# %s ENDE", $this->dataIdentifier); |
| 65 | } |
| 66 | |
| 67 | return \sprintf("\r\n# Plattform %s %s ENDE", $this->dataIdentifier, $this->getType()); |
| 68 | } |
| 69 | |
| 70 | protected function buildPregMatch(): string |
| 71 | { |
| 72 | /* |
| 73 | * Look behind (?<=# BLOCKNAME START)((?s).*)(?=# BLOCKNAME ENDE) and Lookahead |
| 74 | */ |
| 75 | $lookbehind = '(?<=' . $this->getBlockStart() . ')'; |
| 76 | $match = '(.*)'; |
| 77 | $lookahead = '(?=' . $this->getBlockEnd() . ')'; |
| 78 | |
| 79 | return '/' . $lookbehind . $match . $lookahead . '/sm'; |
| 80 | } |
| 81 | } |