Skip to content

Commit

Permalink
minor fabbot changes
Browse files Browse the repository at this point in the history
  • Loading branch information
YSaxon committed Oct 30, 2023
1 parent c7b2c14 commit 2ee0fad
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/Sandbox/MemberMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* - Class can be specified as wildcard `* => [...]` in order to allow those methods/properties for all classes.
* - Method/property can be specified as wildcard eg. `\DateTime => '*'` in order to allow all methods/properties for that class.
* - Method/property can also be specified with a trailing wildcard to allow all methods/properties with a certain prefix, eg. `\DateTime => ['get*', ...]` in order to allow all methods/properties that start with `get`.
*
*
* @author Yaakov Saxon <ysaxon@gmail.com>
*/
final class MemberMatcher
Expand All @@ -28,17 +28,17 @@ public function __construct(array $allowedMembers)
{
$normalizedMembers = [];
foreach ($allowedMembers as $class => $members) {
if (!is_array($members)) {
if (!\is_array($members)) {
$normalizedMembers[$class][] = strtolower($members);
}
else foreach ($members as $index => $member) {
$normalizedMembers[$class][$index] = strtolower($member);
} else {
foreach ($members as $index => $member) {
$normalizedMembers[$class][$index] = strtolower($member);
}
}
}
$this->allowedMembers = $normalizedMembers;
}


public function isAllowed($obj, string $member): bool
{
$cacheKey = get_class($obj) . "::" . $member;
Expand All @@ -51,19 +51,22 @@ public function isAllowed($obj, string $member): bool
$member = strtolower($member); // normalize member name

foreach ($this->allowedMembers as $class => $members) {
if ($class === '*' || $obj instanceof $class) {
if ('*' === $class || $obj instanceof $class) {
foreach ($members as $allowedMember) {
if ($allowedMember === '*') {
if ('*' === $allowedMember) {
$this->cache[$cacheKey] = true;

return true;
}
if ($allowedMember === $member) {
$this->cache[$cacheKey] = true;

return true;
}
//if allowedMember ends with a *, check if the member starts with the allowedMember
if (substr($allowedMember, -1) === '*' && substr($member, 0, strlen($allowedMember) - 1) === rtrim($allowedMember, '*')) {
// if allowedMember ends with a *, check if the member starts with the allowedMember
if ('*' === substr($allowedMember, -1) && substr($member, 0, \strlen($allowedMember) - 1) === rtrim($allowedMember, '*')) {
$this->cache[$cacheKey] = true;

return true;
}
}
Expand Down

0 comments on commit 2ee0fad

Please sign in to comment.