Added hasContext to StateEntity.

This commit is contained in:
Abdulmhsen B. A. A.
2024-06-16 14:19:36 +03:00
parent 53d89713bd
commit 3bdd4f2080
3 changed files with 25 additions and 0 deletions

View File

@@ -646,6 +646,14 @@ final class StateEntity implements iState
return ag($this->context, $key, $default);
}
/**
* @inheritdoc
*/
public function hasContext(string $key): bool
{
return ag_exists($this->context, $key);
}
/**
* Checks if the value of a given key in the entity object is equal to the corresponding value in the current object.
* Some keys are special and require special logic to compare. For example, the updated and watched keys are special

View File

@@ -401,4 +401,13 @@ interface StateInterface extends LoggerAwareInterface
* @return mixed
*/
public function getContext(string|null $key = null, mixed $default = null): mixed;
/**
* Check if entity has contextual data.
*
* @param string $key key
*
* @return bool Return true if the entity has contextual data related to the key.
*/
public function hasContext(string $key): bool;
}

View File

@@ -884,5 +884,13 @@ class StateEntityTest extends TestCase
$entity->getContext(),
'When getContext() is called with no parameters, all context data is returned'
);
$this->assertTrue(
$entity->hasContext('test'),
'When hasContext() is called with existing key, it returns true'
);
$this->assertFalse(
$entity->hasContext('not_set'),
'When hasContext() is called with non-existing key, it returns false'
);
}
}