Skip to content

Commit d2c7d91

Browse files
committed
filters
1 parent a816586 commit d2c7d91

6 files changed

Lines changed: 187 additions & 21 deletions

File tree

coverage.html

Lines changed: 4 additions & 4 deletions
Large diffs are not rendered by default.

src/Parsem/Filters.php

Lines changed: 99 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Matronator\Parsem;
66

7+
use Symfony\Component\Yaml\Yaml;
8+
79
class Filters
810
{
911
public const ENCODING = 'UTF-8';
@@ -15,8 +17,10 @@ class Filters
1517
'camelCase', 'snakeCase', 'kebabCase', 'pascalCase', 'titleCase',
1618
'length',
1719
'reverse', 'random', 'shuffle',
18-
'truncate',
19-
'escape', 'unescape', 'hash'];
20+
'truncate', 'trim',
21+
'url', 'stripTags', 'nl2br',
22+
'escape', 'unescape', 'hash', 'rot13', 'encode', 'decode',
23+
];
2024

2125
public static function upper(string $string): string
2226
{
@@ -40,14 +44,20 @@ public static function lowerFirst(string $string): string
4044
return $fc . mb_substr($string, 1, null, static::ENCODING);
4145
}
4246

43-
public static function first(string $string): string
47+
public static function first(string|array $value): mixed
4448
{
45-
return mb_substr($string, 0, 1, static::ENCODING);
49+
if (is_array($value)) {
50+
return reset($value);
51+
}
52+
return mb_substr($value, 0, 1, static::ENCODING);
4653
}
4754

48-
public static function last(string $string): string
55+
public static function last(string|array $value): mixed
4956
{
50-
return mb_substr($string, -1, 1, static::ENCODING);
57+
if (is_array($value)) {
58+
return end($value);
59+
}
60+
return mb_substr($value, -1, 1, static::ENCODING);
5161
}
5262

5363
public static function camelCase(string $string): string
@@ -87,8 +97,17 @@ public static function pascalCase(string $string): string
8797

8898
public static function titleCase(string $string): string
8999
{
90-
$string = str_replace(['-', '_'], ' ', $string);
100+
// $string = str_replace(['-', '_'], ' ', $string);
101+
// $split = preg_split('/(?=[A-Z])/', $string);
102+
// $string = implode(' ', $split);
103+
// $string = ucwords($string);
104+
105+
// $string = static::camelCase($string);
106+
// $string = preg_replace('/([a-z])([A-Z])/', '$1 $2', $string);
107+
$string = str_replace(['_'], ' ', $string);
108+
$string = str_replace(['-'], ' 🜛 ', $string);
91109
$string = ucwords($string);
110+
$string = str_replace([' 🜛 '], '-', $string);
92111
return $string;
93112
}
94113

@@ -102,7 +121,7 @@ public static function reverse(array|string $value): array|string
102121
return is_string($value) ? strrev($value) : array_reverse($value);
103122
}
104123

105-
public static function random(array|string $value): array|string
124+
public static function random(array|string $value): mixed
106125
{
107126
if (is_string($value)) {
108127
return $value[rand(0, strlen($value) - 1)];
@@ -113,10 +132,12 @@ public static function random(array|string $value): array|string
113132

114133
public static function shuffle(array|string $value): array|string
115134
{
116-
$array = is_string($value) ? str_split($value) : $value;
135+
if (is_string($value)) {
136+
return str_shuffle($value);
137+
}
138+
$array = clone $value;
117139
shuffle($array);
118-
119-
return $value;
140+
return $array;
120141
}
121142

122143
public static function truncate(string $string, int $length, string $ending = '...'): string
@@ -127,4 +148,71 @@ public static function truncate(string $string, int $length, string $ending = '.
127148

128149
return mb_substr($string, 0, $length, static::ENCODING) . $ending;
129150
}
151+
152+
public static function trim(string $string, string $side = 'both', string $characters = " \n\r\t\v\0"): string
153+
{
154+
if ($side === 'left') {
155+
return ltrim($string, $characters, );
156+
} else if ($side === 'right') {
157+
return rtrim($string, $characters);
158+
}
159+
160+
return trim($string, $characters);
161+
}
162+
163+
public static function url(string $string): string
164+
{
165+
return rawurlencode($string);
166+
}
167+
168+
public static function stripTags(string $string): string
169+
{
170+
return strip_tags($string);
171+
}
172+
173+
public static function nl2br(string $string, bool $xhtmlSyntax = false): string
174+
{
175+
return nl2br($string, $xhtmlSyntax);
176+
}
177+
178+
public static function escape(string $string): string
179+
{
180+
return htmlspecialchars($string, ENT_QUOTES, static::ENCODING);
181+
}
182+
183+
public static function unescape(string $string): string
184+
{
185+
return html_entity_decode($string, ENT_QUOTES, static::ENCODING);
186+
}
187+
188+
public static function hash(string $string, string $algorithm = 'md5', string|null $secret = null): string
189+
{
190+
if ($secret) {
191+
return hash_hmac($algorithm, $string, $secret);
192+
}
193+
return hash($algorithm, $string);
194+
}
195+
196+
public static function rot13(string $string): string
197+
{
198+
return str_rot13($string);
199+
}
200+
201+
public static function encode(string $string, string $encoding = 'base64'): string
202+
{
203+
switch ($encoding) {
204+
case 'base64':
205+
return base64_encode($string);
206+
case 'hex':
207+
return bin2hex($string);
208+
case 'url':
209+
return rawurlencode($string);
210+
case 'json':
211+
return json_encode($string);
212+
case 'yaml':
213+
return Yaml::dump($string);
214+
default:
215+
return $string;
216+
}
217+
}
130218
}

src/Parsem/Parser.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,13 @@ public static function parseConditions(string $string, array $arguments = [], in
158158

159159
if ($tag === 'if') {
160160
$nestedIfCount++;
161-
} elseif ($tag === 'endif') {
161+
} else if ($tag === 'endif') {
162162
if ($nestedIfCount === 0) {
163163
$conditionEnd = $tagStart;
164164
break;
165165
}
166166
$nestedIfCount--;
167-
} elseif ($tag === 'else' && $nestedIfCount === 0) {
167+
} else if ($tag === 'else' && $nestedIfCount === 0) {
168168
$hasElse = true;
169169
$elseStart = $tagStart;
170170
$elseMatch = $tagMatches[0][0];
@@ -415,6 +415,9 @@ public static function applyFilters(array $matches, array $arguments): array
415415
}, $args);
416416
array_unshift($args, $arg);
417417
} else {
418+
if ($arg === null) {
419+
$arg = '';
420+
}
418421
$args = [$arg];
419422
}
420423

tests/Parsem/Parser.complex.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ParserComplexTest extends TestCase
3030
"tokenSymbol" => "ASD",
3131
"tokenSupply" => 8,
3232
"tokenDecimals" => 3,
33-
"tokenURI" => "",
33+
"tokenUri" => "",
3434
"mintable" => false,
3535
"burnable" => false,
3636
"initialAmount" => 0,
@@ -45,7 +45,7 @@ class ParserComplexTest extends TestCase
4545
"tokenSymbol" => "ASD",
4646
"tokenSupply" => 0,
4747
"tokenDecimals" => 3,
48-
"tokenURI" => "",
48+
"tokenUri" => "",
4949
"mintable" => true,
5050
"burnable" => false,
5151
"initialAmount" => 0,

tests/Parsem/Parser.filters.phpt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/**
4+
* TEST: Test parsing filters
5+
* @testCase
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
use Matronator\Parsem\Parser;
11+
use Tester\Assert;
12+
use Tester\TestCase;
13+
14+
require __DIR__ . '/../bootstrap.php';
15+
16+
class ParserFiltersTest extends TestCase
17+
{
18+
public function testPascalCaseFilter()
19+
{
20+
$string = '<% var|pascalCase %>';
21+
$args1 = ['var' => 'hello world'];
22+
$args2 = ['var' => 'hello-world'];
23+
$args3 = ['var' => 'hello_world. And HTML idk.'];
24+
25+
$parsed = Parser::parseString($string, $args1);
26+
$parsed2 = Parser::parseString($string, $args2);
27+
$parsed3 = Parser::parseString($string, $args3);
28+
29+
Assert::equal('HelloWorld', $parsed, 'Default filter parsed correctly.');
30+
Assert::equal('HelloWorld', $parsed2, 'Default filter parsed correctly.');
31+
Assert::equal('HelloWorld.AndHTMLIdk.', $parsed3, 'Default filter parsed correctly.');
32+
}
33+
34+
public function testTitleCaseFilter()
35+
{
36+
$string = '<% var|titleCase %>';
37+
$args1 = ['var' => 'hello world'];
38+
$args2 = ['var' => 'hello-world'];
39+
$args3 = ['var' => 'hello_world. And HTML idk.'];
40+
41+
$parsed = Parser::parseString($string, $args1);
42+
$parsed2 = Parser::parseString($string, $args2);
43+
$parsed3 = Parser::parseString($string, $args3);
44+
45+
Assert::equal('Hello World', $parsed, 'Default filter parsed correctly.');
46+
Assert::equal('Hello-World', $parsed2, 'Default filter parsed correctly.');
47+
Assert::equal('Hello World. And HTML Idk.', $parsed3, 'Default filter parsed correctly.');
48+
}
49+
50+
public function testTruncateFilter()
51+
{
52+
$string1 = '<% var|truncate:10 %>';
53+
$string2 = '<% var|truncate:10,"-" %>';
54+
$args1 = ['var' => 'hello world'];
55+
$args2 = ['var' => 'hello'];
56+
$args3 = ['var' => 'hello_world. And HTML idk.'];
57+
58+
$parsed1 = Parser::parseString($string1, $args1);
59+
$parsed2 = Parser::parseString($string1, $args2);
60+
$parsed3 = Parser::parseString($string1, $args3);
61+
62+
$parsed4 = Parser::parseString($string2, $args1);
63+
$parsed5 = Parser::parseString($string2, $args2);
64+
$parsed6 = Parser::parseString($string2, $args3);
65+
66+
Assert::equal('hello worl...', $parsed1, 'Truncate filter parsed correctly.');
67+
Assert::equal('hello', $parsed2, 'Truncate filter parsed correctly.');
68+
Assert::equal('hello_worl...', $parsed3, 'Truncate filter parsed correctly.');
69+
Assert::equal('hello worl-', $parsed4, 'Truncate filter parsed correctly.');
70+
Assert::equal('hello', $parsed5, 'Truncate filter parsed correctly.');
71+
Assert::equal('hello_worl-', $parsed6, 'Truncate filter parsed correctly.');
72+
}
73+
}
74+
75+
(new ParserFiltersTest())->run();

tests/Parsem/Parser.variables.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ class ParserVariablesTest extends TestCase
8585
$string = 'Hello <% var= %>!';
8686
$string2 = 'Hello <% var=|upper %>!';
8787

88-
$parsed = Parser::parseString($string, []);
89-
$parsed2 = Parser::parseString($string2, []);
88+
$parsed = Parser::parseString($string, [], false);
89+
$parsed2 = Parser::parseString($string2, [], false);
9090

9191
Assert::equal('Hello !', $parsed, 'Empty default value parsed correctly.');
9292
Assert::equal('Hello !', $parsed2, 'Empty default value with filter parsed correctly.');

0 commit comments

Comments
 (0)