Skip to content

extension support & some extensions #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Extensions/ArrayManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Le\SMPLang\Extensions;

class ArrayManager
{
public function __construct(public \Le\SMPLang\SMPLang $instance)
{
// ...
}

public function variables(): array
{
$phpFunctions = [
'array_key_exists',
'array_map',
'array_sum',
'array_keys',
'array_merge',
'array_filter',
'array_search',
'array_is_list',
];

$vars = [];

foreach ($phpFunctions as $phpFunction) {
$vars[$phpFunction] = fn (...$args) => $phpFunction(...$args);
}

return $vars;
}
}
31 changes: 31 additions & 0 deletions src/Extensions/Files.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Le\SMPLang\Extensions;

class Files
{
public function __construct(public \Le\SMPLang\SMPLang $instance)
{
// ...
}

public function variables(): array
{
$phpFunctions = [
'is_file',
'is_dir',
'file_exists',
'file_get_contents',
'file_put_contents',
'unlink',
];

$vars = [];

foreach ($phpFunctions as $phpFunction) {
$vars[$phpFunction] = fn (...$args) => $phpFunction(...$args);
}

return $vars;
}
}
23 changes: 23 additions & 0 deletions src/Extensions/VariableManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Le\SMPLang\Extensions;

class VariableManager
{
public function __construct(public \Le\SMPLang\SMPLang $instance)
{
// ...
}

public function variables(): array
{
return [
"isset" => function ($variable): bool {
return isset($this->instance->vars[$variable]);
},
"unset" => function ($variable) {
unset($this->instance->vars[$variable]);
},
];
}
}
12 changes: 11 additions & 1 deletion src/SMPLang.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

class SMPLang
{
protected const EXTENSIONS = [
Extensions\VariableManager::class,
Extensions\ArrayManager::class,
Extensions\Files::class,
];

protected const backtickToDoubleQuotes = [
'"' => '\\"', // escape all double quotes
"\\`" => "[%{BACKTICK}%]", // to be unescaped
Expand All @@ -19,8 +25,12 @@ class SMPLang
];

public function __construct(
protected array $vars = []
public array $vars = []
) {
foreach (static::EXTENSIONS as $extension) {
$extension = new $extension($this);
$this->vars = array_merge($this->vars, $extension->variables());
}
}

public function evaluate(string $expression, array $vars = []): mixed
Expand Down
85 changes: 85 additions & 0 deletions tests/ArrayManagerExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Le\SMPLang\Tests;

use Le\SMPLang\SMPLang;
use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertFalse;
use function PHPUnit\Framework\assertTrue;

test('array_key_exists', function () {
$smpl = new SMPLang([
'array' => ['hello' => 'world'],
]);

assertTrue($smpl->evaluate('array_key_exists("hello", array)'));
assertFalse($smpl->evaluate('array_key_exists("world", array)'));
});

test('array_map', function () {
$smpl = new SMPLang([
'array' => ['foo', 'bar', 'baz'],
'map' => fn ($item) => "hello, $item!",
]);

assertEquals(
$smpl->evaluate('array_map(map, array)'),
['hello, foo!', 'hello, bar!', 'hello, baz!']
);
});

test('array_sum', function () {
$smpl = new SMPLang([
'array' => [1, 2, 3],
]);

assertEquals($smpl->evaluate('array_sum(array)'), 6);
});

test('array_keys', function () {
$smpl = new SMPLang([
'array' => ['a' => 1, 'b' => 2, 'c' => 3],
]);

assertEquals($smpl->evaluate('array_keys(array)'), ['a', 'b' ,'c']);
});

test('array_merge', function () {
$smpl = new SMPLang([
'first' => [1, 2, 'duplicate' => 3],
'second' => [2, 4, 'duplicate' => 6],
]);

assertEquals(
$smpl->evaluate('array_merge(first, second)'),
[0 => 1, 1 => 2, 'duplicate' => 6, 2 => 2, 3 => 4]
);
});

test('array_filter', function () {
$smpl = new SMPLang([
'array' => [1, 2, 3, 4, 5],
'filter' => fn ($item) => $item > 3,
]);

assertEquals($smpl->evaluate('array_filter(array, filter)'), [3 => 4, 4 => 5]);
});

test('array_search', function () {
$smpl = new SMPLang([
'array' => [1, 2, 3, 4, 5],
]);

assertEquals($smpl->evaluate('array_search(3, array)'), 2);
assertFalse($smpl->evaluate('array_search(6, array)'));
});

test('array_is_list', function () {
$smpl = new SMPLang([
'array1' => [1, 2, 3, 4, 5],
'array2' => [1, 'a' => 2, 3, 4, 5],
]);

assertTrue($smpl->evaluate('array_is_list(array1)'));
assertFalse($smpl->evaluate('array_is_list(array2)'));
});
39 changes: 39 additions & 0 deletions tests/FilesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Le\SMPLang\Tests;

use Le\SMPLang\SMPLang;
use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertTrue;

test('is_file', function () {
$smpl = new SMPLang([
'path' => __FILE__,
]);

assertTrue($smpl->evaluate('is_file(path)'));
});

test('is_dir', function () {
$smpl = new SMPLang([
'path' => __DIR__,
]);

assertTrue($smpl->evaluate('is_dir(path)'));
});

test('file_exists', function () {
$smpl = new SMPLang([
'path' => __FILE__,
]);

assertTrue($smpl->evaluate('file_exists(path)'));
});

test('file_get_contents', function () {
$smpl = new SMPLang([
'path' => __FILE__,
]);

assertEquals($smpl->evaluate('file_get_contents(path)'), file_get_contents(__FILE__));
});
26 changes: 26 additions & 0 deletions tests/VariableManagerExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Le\SMPLang\Tests;

use Le\SMPLang\SMPLang;
use function PHPUnit\Framework\assertFalse;
use function PHPUnit\Framework\assertTrue;

test('isset', function () {
$smpl = new SMPLang([
'hello' => 'world',
]);

assertTrue($smpl->evaluate('isset("hello")'));
assertFalse($smpl->evaluate('isset("world")'));
});

test('unset', function () {
$smpl = new SMPLang([
'hello' => 'world',
]);

$smpl->evaluate('unset("world")');
$smpl->evaluate('unset("hello")');
assertFalse($smpl->evaluate('isset("hello")'));
});