Skip to content

Commit 2a334b3

Browse files
committed
feat: implement comprehensive transformer demo
This commit demonstrates the usage of various transformers in the KaririCode Framework. The demo showcases different transformation capabilities: - Data Formats: * Date format conversion (d/m/Y to Y-m-d) * Number formatting with locale-specific separators * JSON handling with pretty print - String Manipulation: * Phone number masking ((##) #####-####) * Case transformations (camelCase, snake_case) * URL slug generation * Template rendering with variable substitution - Array Operations: * Array key case transformation * Array flattening with dot notation * Array grouping by field
1 parent 7c7bfd1 commit 2a334b3

File tree

7 files changed

+339
-46
lines changed

7 files changed

+339
-46
lines changed

src/Contract/Transformer.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Processor/Data/JsonTransformer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ class JsonTransformer extends AbstractTransformerProcessor implements Configurab
1212
private bool $assoc = true;
1313
private int $depth = 512;
1414
private int $encodeOptions = 0;
15+
private bool $returnString = false;
1516

1617
public function configure(array $options): void
1718
{
1819
$this->assoc = $options['assoc'] ?? $this->assoc;
1920
$this->depth = $options['depth'] ?? $this->depth;
2021
$this->encodeOptions = $options['encodeOptions'] ?? $this->encodeOptions;
22+
$this->returnString = $options['returnString'] ?? $this->returnString;
2123
}
2224

2325
public function process(mixed $input): mixed
@@ -26,7 +28,11 @@ public function process(mixed $input): mixed
2628
return $this->decode($input);
2729
}
2830

29-
return $this->encode($input);
31+
if (is_array($input) && $this->returnString) {
32+
return $this->encode($input);
33+
}
34+
35+
return $input;
3036
}
3137

3238
private function decode(string $input): mixed

src/Processor/Data/NumberTransformer.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class NumberTransformer extends AbstractTransformerProcessor implements Configur
1414
private string $thousandsSeparator = '';
1515
private ?float $multiplier = null;
1616
private bool $roundUp = false;
17+
private bool $formatAsString = false;
1718

1819
public function configure(array $options): void
1920
{
@@ -22,14 +23,15 @@ public function configure(array $options): void
2223
$this->thousandsSeparator = $options['thousandsSeparator'] ?? $this->thousandsSeparator;
2324
$this->multiplier = $options['multiplier'] ?? $this->multiplier;
2425
$this->roundUp = $options['roundUp'] ?? $this->roundUp;
26+
$this->formatAsString = $options['formatAsString'] ?? $this->formatAsString;
2527
}
2628

27-
public function process(mixed $input): string
29+
public function process(mixed $input): float|string
2830
{
2931
if (!is_numeric($input)) {
3032
$this->setInvalid('notNumeric');
3133

32-
return '';
34+
return $this->formatAsString ? '' : 0.0;
3335
}
3436

3537
$number = (float) $input;
@@ -42,11 +44,15 @@ public function process(mixed $input): string
4244
$number = ceil($number * (10 ** $this->decimals)) / (10 ** $this->decimals);
4345
}
4446

45-
return number_format(
46-
$number,
47-
$this->decimals,
48-
$this->decimalPoint,
49-
$this->thousandsSeparator
50-
);
47+
if ($this->formatAsString) {
48+
return number_format(
49+
$number,
50+
$this->decimals,
51+
$this->decimalPoint,
52+
$this->thousandsSeparator
53+
);
54+
}
55+
56+
return round($number, $this->decimals);
5157
}
5258
}

src/Processor/String/SlugTransformer.php

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ class SlugTransformer extends AbstractTransformerProcessor implements Configurab
1515
private string $separator = '-';
1616
private bool $lowercase = true;
1717
private array $replacements = [];
18-
private string $transliterationLocale = 'en';
1918

2019
public function configure(array $options): void
2120
{
2221
$this->separator = $options['separator'] ?? $this->separator;
2322
$this->lowercase = $options['lowercase'] ?? $this->lowercase;
24-
$this->replacements = array_merge($this->replacements, $options['replacements'] ?? []);
25-
$this->transliterationLocale = $options['transliterationLocale'] ?? $this->transliterationLocale;
23+
$this->replacements = array_merge($this->getDefaultReplacements(), $options['replacements'] ?? []);
2624
}
2725

2826
public function process(mixed $input): string
@@ -35,46 +33,76 @@ public function process(mixed $input): string
3533

3634
$slug = $this->createSlug($input);
3735

38-
return $this->finalizeSlug($slug);
36+
if (empty($slug)) {
37+
$this->setInvalid('emptySlug');
38+
39+
return '';
40+
}
41+
42+
return $slug;
3943
}
4044

4145
private function createSlug(string $input): string
4246
{
43-
// Apply custom replacements
47+
// Apply custom replacements first
4448
$text = str_replace(
4549
array_keys($this->replacements),
4650
array_values($this->replacements),
4751
$input
4852
);
4953

50-
// Transliterate
51-
$text = transliterator_transliterate(
52-
"Any-{$this->transliterationLocale}; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFC",
53-
$text
54-
);
54+
// Convert accented characters to ASCII
55+
$text = $this->convertAccentsToAscii($text);
5556

5657
// Convert to lowercase if needed
5758
if ($this->lowercase) {
58-
$text = strtolower($text);
59+
$text = mb_strtolower($text);
5960
}
6061

6162
// Replace non-alphanumeric characters with separator
62-
$text = preg_replace('/[^\p{L}\p{N}]+/u', $this->separator, $text);
63+
$text = preg_replace('/[^a-zA-Z0-9\-_]/', $this->separator, $text);
6364

64-
// Remove duplicate separators
65+
// Replace multiple separators with a single one
6566
$text = preg_replace('/' . preg_quote($this->separator, '/') . '+/', $this->separator, $text);
6667

6768
return trim($text, $this->separator);
6869
}
6970

70-
private function finalizeSlug(string $slug): string
71+
private function getDefaultReplacements(): array
7172
{
72-
if (empty($slug)) {
73-
$this->setInvalid('emptySlug');
74-
75-
return '';
76-
}
73+
return [
74+
' ' => $this->separator,
75+
'&' => 'and',
76+
'@' => 'at',
77+
];
78+
}
7779

78-
return $slug;
80+
private function convertAccentsToAscii(string $string): string
81+
{
82+
$chars = [
83+
// Latin
84+
'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE',
85+
'Ç' => 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I',
86+
'Î' => 'I', 'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O',
87+
'Õ' => 'O', 'Ö' => 'O', 'Ő' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U',
88+
'Ü' => 'U', 'Ű' => 'U', 'Ý' => 'Y', 'Þ' => 'TH', 'ß' => 'ss', 'à' => 'a', 'á' => 'a',
89+
'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e',
90+
'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
91+
'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o',
92+
'ő' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ű' => 'u',
93+
'ý' => 'y', 'þ' => 'th', 'ÿ' => 'y',
94+
// Latin symbols
95+
'©' => '(c)',
96+
// Greek
97+
'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H',
98+
'Θ' => '8', 'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => '3',
99+
'Ο' => 'O', 'Π' => 'P', 'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F',
100+
'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W', 'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd',
101+
'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => '8', 'ι' => 'i', 'κ' => 'k', 'λ' => 'l',
102+
'μ' => 'm', 'ν' => 'n', 'ξ' => '3', 'ο' => 'o', 'π' => 'p', 'ρ' => 'r', 'σ' => 's',
103+
'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w',
104+
];
105+
106+
return strtr($string, $chars);
79107
}
80108
}

src/Processor/String/TemplateTransformer.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class TemplateTransformer extends AbstractTransformerProcessor implements Config
2020
private mixed $missingValueHandler = null;
2121

2222
private bool $removeUnmatchedTags = false;
23+
private bool $preserveData = true;
2324

2425
public function configure(array $options): void
2526
{
@@ -28,20 +29,27 @@ public function configure(array $options): void
2829
$this->closeTag = $options['closeTag'] ?? $this->closeTag;
2930
$this->missingValueHandler = $options['missingValueHandler'] ?? $this->missingValueHandler;
3031
$this->removeUnmatchedTags = $options['removeUnmatchedTags'] ?? $this->removeUnmatchedTags;
32+
$this->preserveData = $options['preserveData'] ?? $this->preserveData;
3133
}
3234

33-
public function process(mixed $input): string
35+
public function process(mixed $input): mixed
3436
{
3537
if (!is_array($input)) {
3638
$this->setInvalid('notArray');
3739

38-
return $this->template;
40+
return $input;
3941
}
4042

4143
if (empty($this->template)) {
4244
$this->setInvalid('noTemplate');
4345

44-
return '';
46+
return $input;
47+
}
48+
49+
if ($this->preserveData) {
50+
$input['_rendered'] = $this->replacePlaceholders($input);
51+
52+
return $input;
4553
}
4654

4755
return $this->replacePlaceholders($input);

src/Transformer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(
2727

2828
public function transform(mixed $object): TransformationResult
2929
{
30-
$handler = new ProcessorAttributeHandler(
30+
$attributeHandler = new ProcessorAttributeHandler(
3131
self::IDENTIFIER,
3232
$this->builder
3333
);
@@ -37,7 +37,7 @@ public function transform(mixed $object): TransformationResult
3737
);
3838

3939
/** @var ProcessorAttributeHandler */
40-
$handler = $propertyInspector->inspect($object, $handler);
40+
$handler = $propertyInspector->inspect($object, $attributeHandler);
4141
$handler->applyChanges($object);
4242

4343
return new TransformationResult(

0 commit comments

Comments
 (0)