Skip to content

Commit

Permalink
Merge pull request #2 from divengine/concepts
Browse files Browse the repository at this point in the history
Change concept from constant to immutable value
  • Loading branch information
rafageist authored Aug 14, 2024
2 parents be8ba20 + 0e4af7f commit c83e29e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
20 changes: 10 additions & 10 deletions src/laze.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* [[]] Div PHP Laze
*
* A PHP library for defining lazy constants. Values are set as closures
* A PHP library for defining lazy inmutable values. Values are set as closures
* and only materialize upon first access, ensuring efficient and controlled
* initialization.
*
Expand Down Expand Up @@ -41,7 +41,7 @@ class laze
private static string $__version = '1.1.0';

/**
* Store for lazy constants.
* Store for lazy immutable values.
* @var array<string, mixed>
*/
private static array $store = [];
Expand All @@ -53,7 +53,7 @@ class laze
private static array $constraints = [];

/**
* Map of evaluated lazy constants.
* Map of evaluated lazy immutable values.
* @var array<bool>
*/
private static array $evaluated = [];
Expand All @@ -69,7 +69,7 @@ public static function getVersion()
}

/**
* Check if a lazy constant is defined.
* Check if a lazy immutable value is defined.
*
* @param string $key
* @return bool
Expand All @@ -80,20 +80,20 @@ public static function defined(string $key): bool
}

/**
* Check if a lazy constant has been evaluated.
* Check if a lazy immutable value has been evaluated.
*
* @param string $key
* @return bool
*/
public static function evaluated(string $key): bool
{
self::defined($key) or throw new \Exception("Undefined lazy constant: $key");
self::defined($key) or throw new \Exception("Undefined lazy immutable value: $key");

return self::$evaluated[$key];
}

/**
* Define a constraint for a lazy constant.
* Define a constraint for a lazy immutable value.
*
* @param string $name
* @param callable $checker
Expand All @@ -105,7 +105,7 @@ public static function constraint($name, callable $checker): void
}

/**
* Define a lazy constant as a closure.
* Define a lazy immutable value as a closure.
*
* @param string $key
* @param callable $value
Expand All @@ -120,7 +120,7 @@ public static function define(string $key, callable $value): void
}

/**
* Read the value of a lazy constant, evaluating the closure if needed.
* Read the value of a lazy immutable value, evaluating the closure if needed.
*
* @param string $key
* @return mixed
Expand All @@ -133,7 +133,7 @@ public static function read(string $key): mixed
foreach (self::$constraints as $constraint) {
$pass = $constraint[1]($key, $value);
if (!$pass) {
throw new \Exception("Constraint '{$constraint[0]}' failed for lazy constant: $key");
throw new \Exception("Constraint '{$constraint[0]}' failed for lazy immutable value: $key");
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class BasicTest extends TestCase
{
public function testScalarConstant(): void
public function testScalarImmutable(): void
{
laze::define('FOO', fn() => 42);

Expand Down Expand Up @@ -88,7 +88,7 @@ public function testConstraintFailure(): void
laze::define('QUUX', fn() => 42);

$this->expectException(\Exception::class);
$this->expectExceptionMessage("Constraint 'Must be a string' failed for lazy constant: QUUX");
$this->expectExceptionMessage("Constraint 'Must be a string' failed for lazy immutable value: QUUX");

laze::read('QUUX');
}
Expand Down

0 comments on commit c83e29e

Please sign in to comment.