Skip to content

Commit

Permalink
Improving the local and global parameters' management<br>
Browse files Browse the repository at this point in the history
Add new method `getAnyParam(string $name)` that will return first the local value of the parameter if defined
or the global value instead or throw finally an exception if the parameter is unknown<br>
Offers the possibility to unset any local or global parameter using `unsetParam(string $name)` or `unsetGlobalParam(string $name)`<br>
You can now check if a parameter is defined either in the local or global array using `hasAnyParam(string $name)`<br>
Test files are updated
  • Loading branch information
rawsrc committed Mar 12, 2023
1 parent e4a2367 commit 2102898
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 6 deletions.
60 changes: 57 additions & 3 deletions PhpEcho.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public static function useSpaceNotation(): bool
/**
* @param string $id
*/
public function setId(string $id)
public function setId(string $id): void
{
$this->id = $id;
}
Expand All @@ -288,11 +288,24 @@ public function getId(): string
* @param string $name
* @param mixed $value
*/
public function setParam(string $name, mixed $value)
public function setParam(string $name, mixed $value): void
{
$this->params[$name] = $value;
}

/**
* @param string $name
* @return void
*/
public function unsetParam(string $name): void
{
if (array_key_exists($name, $this->params)) {
unset ($this->params[$name]);
} else {
throw new InvalidArgumentException("unknown.parameter.{$name}");
}
}

/**
* Get the value of a local parameter
* The value of the parameter is never escaped
Expand All @@ -310,6 +323,21 @@ public function getParam(string $name): mixed
: throw new InvalidArgumentException("unknown.parameter.{$name}");
}

/**
* Return a parameter value from the local parameters array if found or from the
* global parameters array if found or throw an exception
*
* @param string $name
* @return mixed
* @throws InvalidArgumentException
*/
public function getAnyParam(string $name): mixed
{
return array_key_exists($name, $this->params)
? $this->params[$name]
: self::getGlobalParam($name);
}

/**
* @param string $name
* @return bool
Expand All @@ -325,11 +353,26 @@ public function hasParam(string $name): bool
* @param string $name
* @param mixed $value
*/
public static function setGlobalParam(string $name, mixed $value)
public static function setGlobalParam(string $name, mixed $value): void
{
self::$global_params[$name] = $value;
}

/**
* @param string $name
* @throws InvalidArgumentException
*/
public static function unsetGlobalParam(string $name): void
{
if (array_key_exists($name, self::$global_params)) {
unset (self::$global_params[$name]);
} elseif (array_key_exists($name, self::$global_params)) {
unset (self::$global_params[$name]);
} else {
throw new InvalidArgumentException("unknown.parameter.{$name}");
}
}

/**
* @param string $name
* @return mixed
Expand All @@ -351,6 +394,17 @@ public function hasGlobalParam(string $name): bool
return array_key_exists($name, self::$global_params);
}

/**
* Check if the parameter is defined either in the local array storage or in the global one
*
* @param string $name
* @return bool
*/
public function hasAnyParam(string $name): bool
{
return $this->hasParam($name) || self::hasGlobalParam($name);
}

/**
* Generate a unique execution id based on random_bytes()
* Always start with a letter
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# **PhpEcho**

`2023-01-29` `PHP 8.0+` `v.5.2.0`
`2023-03-12` `PHP 8.0+` `v.5.2.1`

## **A native PHP template engine : One class to rule them all**
## **VERSION 5.X IS ONLY FOR PHP 8 AND ABOVE**
Expand Down Expand Up @@ -35,6 +35,13 @@ The class will manage :
```bash
composer require rawsrc/phpecho
```
**Changelog v5.2.1:**<br>
1. Improving the local and global parameters' management<br>
Add new method `getAnyParam(string $name)` that will return first the local value of the parameter if defined
or the global value instead or throw finally an exception if the parameter is unknown<br>
Offers the possibility to unset any local or global parameter using `unsetParam(string $name)` or `unsetGlobalParam(string $name)`<br>
You can now check if a parameter is defined either in the local or global array using `hasAnyParam(string $name)`<br>
Test files are updated

**Changelog v5.2.0:**<br>
1. Space is not used as directory separator anymore, the only admitted directory separator is now / (slash)
Expand Down
Binary file modified tests/global_tests_result.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 95 additions & 1 deletion tests/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,101 @@
$pilot->run(
id: 'param_050',
test: fn() => PhpEcho::getGlobalParam('param_a') === 'def' && $block->getParam('param_a') === 'abc',
description: "check there's no collision between param and global param"
description: "check there's no collision between local param and global param"
);
$pilot->assertIsBool();
$pilot->assertEqual(true);

$block->setParam('to_be_unset', 'klm123');
$pilot->run(
id: 'param_051',
test: fn() => $block->hasAnyParam('to_be_unset') === true,
description: "once a parameter is defined locally, hasAnyParam() must return true"
);
$pilot->assertIsBool();
$pilot->assertEqual(true);

$pilot->run(
id: 'param_052',
test: fn() => $block->hasAnyParam('unknown_param'),
description: "check hasAnyParam with an unknown parameter"
);
$pilot->assertIsBool();
$pilot->assertEqual(false);

$pilot->run(
id: 'param_053',
test: fn() => $block->getParam('to_be_unset'),
description: "check local parameter value storage"
);
$pilot->assertIsString();
$pilot->assertEqual('klm123');

$block->unsetParam('to_be_unset');
$pilot->run(
id: 'param_054',
test: fn() => $block->hasParam('to_be_unset'),
description: 'unset a local parameter and check if it was removed from the local parameters array'
);
$pilot->assertIsBool();
$pilot->assertEqual(false);

$pilot->run(
id: 'param_055',
test: fn() => $block->unsetParam('unknown_param_name'),
description: 'try to unset an unknown local parameter'
);
$pilot->assertException(InvalidArgumentException::class);

PhpEcho::setGlobalParam('to_be_unset', 'klm123');
$pilot->run(
id: 'param_056',
test: fn() => PhpEcho::getGlobalParam('to_be_unset'),
description: "check global parameter value storage"
);
$pilot->assertIsString();
$pilot->assertEqual('klm123');

$pilot->run(
id: 'param_057',
test: fn() => $block->hasAnyParam('to_be_unset') === true,
description: "once a parameter is defined globally, hasAnyParam() must return true"
);
$pilot->assertIsBool();
$pilot->assertEqual(true);

PhpEcho::unsetGlobalParam('to_be_unset');
$pilot->run(
id: 'param_058',
test: fn() => $block->hasGlobalParam('to_be_unset'),
description: 'unset a global parameter and check if it was removed from the global parameters array'
);
$pilot->assertIsBool();
$pilot->assertEqual(false);

$pilot->run(
id: 'param_059',
test: fn() => PhpEcho::unsetGlobalParam('unknown_param_name'),
description: 'try to unset an unknown global parameter'
);
$pilot->assertException(InvalidArgumentException::class);

$block->setParam('any_param', 'local_param_value');
PhpEcho::setGlobalParam('any_param', 'global_param_value');
$pilot->run(
id: 'param_060',
test: fn() => $block->getAnyParam('any_param') === 'local_param_value',
description: "check getAnyParam returns the local parameter value first"
);
$pilot->assertIsBool();
$pilot->assertEqual(true);

$block->unsetParam('any_param');
$pilot->run(
id: 'param_061',
test: fn() => $block->getAnyParam('any_param') === 'global_param_value',
description: "unset the local parameter value and check if getAnyParam returns the global parameter value instead"
);
$pilot->assertIsBool();
$pilot->assertEqual(true);

2 changes: 1 addition & 1 deletion tests/tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Exacodis\Pilot;

$pilot = new Pilot('PhpEcho - A native PHP template engine - v.5.2.0');
$pilot = new Pilot('PhpEcho - A native PHP template engine - v.5.2.1');
$pilot->injectStandardHelpers();

include 'filepath.php';
Expand Down

0 comments on commit 2102898

Please sign in to comment.