@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | array $headers = [] |
| 78 | 78 | ): ResponseInterface { |
| 79 | 79 | $r = $this->request($uri, 'GET', [], $headers, []); |
| 80 | - foreach ($attributes as $k => $v) { |
|
| 80 | + foreach ($attributes as $k => $v){ |
|
| 81 | 81 | $r = $r->withAttribute($k, $v); |
| 82 | 82 | } |
| 83 | 83 | |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | $factory = $this->app->getContainer()->get(ServerRequestFactoryInterface::class); |
| 111 | 111 | $request = $factory->createServerRequest($method, $uri); |
| 112 | 112 | |
| 113 | - foreach ($headers as $name => $value) { |
|
| 113 | + foreach ($headers as $name => $value){ |
|
| 114 | 114 | $request = $request->withAddedHeader($name, $value); |
| 115 | 115 | } |
| 116 | 116 | |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | public function fetchCookies(array $header) |
| 123 | 123 | { |
| 124 | 124 | $result = []; |
| 125 | - foreach ($header as $line) { |
|
| 125 | + foreach ($header as $line){ |
|
| 126 | 126 | $cookie = explode('=', $line); |
| 127 | 127 | $result[$cookie[0]] = rawurldecode(substr($cookie[1], 0, strpos($cookie[1], ';'))); |
| 128 | 128 | } |
@@ -77,7 +77,8 @@ discard block |
||
| 77 | 77 | array $headers = [] |
| 78 | 78 | ): ResponseInterface { |
| 79 | 79 | $r = $this->request($uri, 'GET', [], $headers, []); |
| 80 | - foreach ($attributes as $k => $v) { |
|
| 80 | + foreach ($attributes as $k => $v) |
|
| 81 | + { |
|
| 81 | 82 | $r = $r->withAttribute($k, $v); |
| 82 | 83 | } |
| 83 | 84 | |
@@ -110,7 +111,8 @@ discard block |
||
| 110 | 111 | $factory = $this->app->getContainer()->get(ServerRequestFactoryInterface::class); |
| 111 | 112 | $request = $factory->createServerRequest($method, $uri); |
| 112 | 113 | |
| 113 | - foreach ($headers as $name => $value) { |
|
| 114 | + foreach ($headers as $name => $value) |
|
| 115 | + { |
|
| 114 | 116 | $request = $request->withAddedHeader($name, $value); |
| 115 | 117 | } |
| 116 | 118 | |
@@ -122,7 +124,8 @@ discard block |
||
| 122 | 124 | public function fetchCookies(array $header) |
| 123 | 125 | { |
| 124 | 126 | $result = []; |
| 125 | - foreach ($header as $line) { |
|
| 127 | + foreach ($header as $line) |
|
| 128 | + { |
|
| 126 | 129 | $cookie = explode('=', $line); |
| 127 | 130 | $result[$cookie[0]] = rawurldecode(substr($cookie[1], 0, strpos($cookie[1], ';'))); |
| 128 | 131 | } |
@@ -32,22 +32,22 @@ |
||
| 32 | 32 | |
| 33 | 33 | public function __construct( |
| 34 | 34 | private readonly RulesInterface $repository |
| 35 | - ) { |
|
| 35 | + ){ |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | public function allows(ActorInterface $actor, string $permission, array $context): bool |
| 39 | 39 | { |
| 40 | 40 | $allowed = 0; |
| 41 | - foreach (static::RULES as $rule) { |
|
| 41 | + foreach (static::RULES as $rule){ |
|
| 42 | 42 | $rule = $this->repository->get($rule); |
| 43 | 43 | |
| 44 | - if ($rule->allows($actor, $permission, $context)) { |
|
| 45 | - if (static::BEHAVIOUR === self::AT_LEAST_ONE) { |
|
| 44 | + if ($rule->allows($actor, $permission, $context)){ |
|
| 45 | + if (static::BEHAVIOUR === self::AT_LEAST_ONE){ |
|
| 46 | 46 | return true; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | $allowed++; |
| 50 | - } elseif (static::BEHAVIOUR === self::ALL) { |
|
| 50 | + } elseif (static::BEHAVIOUR === self::ALL){ |
|
| 51 | 51 | return false; |
| 52 | 52 | } |
| 53 | 53 | } |
@@ -38,16 +38,21 @@ |
||
| 38 | 38 | public function allows(ActorInterface $actor, string $permission, array $context): bool |
| 39 | 39 | { |
| 40 | 40 | $allowed = 0; |
| 41 | - foreach (static::RULES as $rule) { |
|
| 41 | + foreach (static::RULES as $rule) |
|
| 42 | + { |
|
| 42 | 43 | $rule = $this->repository->get($rule); |
| 43 | 44 | |
| 44 | - if ($rule->allows($actor, $permission, $context)) { |
|
| 45 | - if (static::BEHAVIOUR === self::AT_LEAST_ONE) { |
|
| 45 | + if ($rule->allows($actor, $permission, $context)) |
|
| 46 | + { |
|
| 47 | + if (static::BEHAVIOUR === self::AT_LEAST_ONE) |
|
| 48 | + { |
|
| 46 | 49 | return true; |
| 47 | 50 | } |
| 48 | 51 | |
| 49 | 52 | $allowed++; |
| 50 | - } elseif (static::BEHAVIOUR === self::ALL) { |
|
| 53 | + } |
|
| 54 | + elseif (static::BEHAVIOUR === self::ALL) |
|
| 55 | + { |
|
| 51 | 56 | return false; |
| 52 | 57 | } |
| 53 | 58 | } |
@@ -21,6 +21,6 @@ |
||
| 21 | 21 | |
| 22 | 22 | public function allows(ActorInterface $actor, string $permission, array $context): bool |
| 23 | 23 | { |
| 24 | - return (bool) ($this->callable)($actor, $permission, $context); |
|
| 24 | + return (bool)($this->callable)($actor, $permission, $context); |
|
| 25 | 25 | } |
| 26 | 26 | } |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | public function __construct( |
| 33 | 33 | private readonly RulesInterface $rules, |
| 34 | 34 | private readonly string $defaultRule = ForbidRule::class |
| 35 | - ) { |
|
| 35 | + ){ |
|
| 36 | 36 | $this->matcher = new Matcher(); |
| 37 | 37 | } |
| 38 | 38 | |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | public function addRole(string $role): PermissionManager |
| 45 | 45 | { |
| 46 | - if ($this->hasRole($role)) { |
|
| 46 | + if ($this->hasRole($role)){ |
|
| 47 | 47 | throw new RoleException(\sprintf("Role '%s' already exists", $role)); |
| 48 | 48 | } |
| 49 | 49 | |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | |
| 57 | 57 | public function removeRole(string $role): PermissionManager |
| 58 | 58 | { |
| 59 | - if (!$this->hasRole($role)) { |
|
| 59 | + if (!$this->hasRole($role)){ |
|
| 60 | 60 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 61 | 61 | } |
| 62 | 62 | |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | |
| 73 | 73 | public function getPermissions(string $role): array |
| 74 | 74 | { |
| 75 | - if (!$this->hasRole($role)) { |
|
| 75 | + if (!$this->hasRole($role)){ |
|
| 76 | 76 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 77 | 77 | } |
| 78 | 78 | |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | |
| 82 | 82 | public function getRule(string $role, string $permission): RuleInterface |
| 83 | 83 | { |
| 84 | - if (!$this->hasRole($role)) { |
|
| 84 | + if (!$this->hasRole($role)){ |
|
| 85 | 85 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 86 | 86 | } |
| 87 | 87 | |
@@ -91,11 +91,11 @@ discard block |
||
| 91 | 91 | |
| 92 | 92 | public function associate(string $role, string $permission, string $rule = AllowRule::class): PermissionManager |
| 93 | 93 | { |
| 94 | - if (!$this->hasRole($role)) { |
|
| 94 | + if (!$this->hasRole($role)){ |
|
| 95 | 95 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - if (!$this->rules->has($rule)) { |
|
| 98 | + if (!$this->rules->has($rule)){ |
|
| 99 | 99 | throw new PermissionException(\sprintf("Undefined rule '%s'", $rule)); |
| 100 | 100 | } |
| 101 | 101 | |
@@ -121,14 +121,14 @@ discard block |
||
| 121 | 121 | */ |
| 122 | 122 | private function findRule(string $role, string $permission): string |
| 123 | 123 | { |
| 124 | - if (isset($this->permissions[$role][$permission])) { |
|
| 124 | + if (isset($this->permissions[$role][$permission])){ |
|
| 125 | 125 | //O(1) check |
| 126 | 126 | return $this->permissions[$role][$permission]; |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | //Matching using star syntax |
| 130 | - foreach ($this->permissions[$role] as $pattern => $rule) { |
|
| 131 | - if ($this->matcher->matches($permission, $pattern)) { |
|
| 130 | + foreach ($this->permissions[$role] as $pattern => $rule){ |
|
| 131 | + if ($this->matcher->matches($permission, $pattern)){ |
|
| 132 | 132 | return $rule; |
| 133 | 133 | } |
| 134 | 134 | } |
@@ -43,7 +43,8 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | public function addRole(string $role): PermissionManager |
| 45 | 45 | { |
| 46 | - if ($this->hasRole($role)) { |
|
| 46 | + if ($this->hasRole($role)) |
|
| 47 | + { |
|
| 47 | 48 | throw new RoleException(\sprintf("Role '%s' already exists", $role)); |
| 48 | 49 | } |
| 49 | 50 | |
@@ -56,7 +57,8 @@ discard block |
||
| 56 | 57 | |
| 57 | 58 | public function removeRole(string $role): PermissionManager |
| 58 | 59 | { |
| 59 | - if (!$this->hasRole($role)) { |
|
| 60 | + if (!$this->hasRole($role)) |
|
| 61 | + { |
|
| 60 | 62 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 61 | 63 | } |
| 62 | 64 | |
@@ -72,7 +74,8 @@ discard block |
||
| 72 | 74 | |
| 73 | 75 | public function getPermissions(string $role): array |
| 74 | 76 | { |
| 75 | - if (!$this->hasRole($role)) { |
|
| 77 | + if (!$this->hasRole($role)) |
|
| 78 | + { |
|
| 76 | 79 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 77 | 80 | } |
| 78 | 81 | |
@@ -81,7 +84,8 @@ discard block |
||
| 81 | 84 | |
| 82 | 85 | public function getRule(string $role, string $permission): RuleInterface |
| 83 | 86 | { |
| 84 | - if (!$this->hasRole($role)) { |
|
| 87 | + if (!$this->hasRole($role)) |
|
| 88 | + { |
|
| 85 | 89 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 86 | 90 | } |
| 87 | 91 | |
@@ -91,11 +95,13 @@ discard block |
||
| 91 | 95 | |
| 92 | 96 | public function associate(string $role, string $permission, string $rule = AllowRule::class): PermissionManager |
| 93 | 97 | { |
| 94 | - if (!$this->hasRole($role)) { |
|
| 98 | + if (!$this->hasRole($role)) |
|
| 99 | + { |
|
| 95 | 100 | throw new RoleException(\sprintf("Undefined role '%s'", $role)); |
| 96 | 101 | } |
| 97 | 102 | |
| 98 | - if (!$this->rules->has($rule)) { |
|
| 103 | + if (!$this->rules->has($rule)) |
|
| 104 | + { |
|
| 99 | 105 | throw new PermissionException(\sprintf("Undefined rule '%s'", $rule)); |
| 100 | 106 | } |
| 101 | 107 | |
@@ -121,14 +127,17 @@ discard block |
||
| 121 | 127 | */ |
| 122 | 128 | private function findRule(string $role, string $permission): string |
| 123 | 129 | { |
| 124 | - if (isset($this->permissions[$role][$permission])) { |
|
| 130 | + if (isset($this->permissions[$role][$permission])) |
|
| 131 | + { |
|
| 125 | 132 | //O(1) check |
| 126 | 133 | return $this->permissions[$role][$permission]; |
| 127 | 134 | } |
| 128 | 135 | |
| 129 | 136 | //Matching using star syntax |
| 130 | - foreach ($this->permissions[$role] as $pattern => $rule) { |
|
| 131 | - if ($this->matcher->matches($permission, $pattern)) { |
|
| 137 | + foreach ($this->permissions[$role] as $pattern => $rule) |
|
| 138 | + { |
|
| 139 | + if ($this->matcher->matches($permission, $pattern)) |
|
| 140 | + { |
|
| 132 | 141 | return $rule; |
| 133 | 142 | } |
| 134 | 143 | } |
@@ -13,7 +13,7 @@ |
||
| 13 | 13 | { |
| 14 | 14 | public function __construct( |
| 15 | 15 | private readonly array $roles |
| 16 | - ) { |
|
| 16 | + ){ |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | public function getRoles(): array |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | public function getGuard(): GuardInterface |
| 21 | 21 | { |
| 22 | 22 | $container = ContainerScope::getContainer(); |
| 23 | - if (empty($container) || !$container->has(GuardInterface::class)) { |
|
| 23 | + if (empty($container) || !$container->has(GuardInterface::class)){ |
|
| 24 | 24 | throw new ScopeException( |
| 25 | 25 | 'Unable to get `GuardInterface`, binding is missing or container scope is not set' |
| 26 | 26 | ); |
@@ -44,9 +44,9 @@ discard block |
||
| 44 | 44 | */ |
| 45 | 45 | protected function resolvePermission(string $permission): string |
| 46 | 46 | { |
| 47 | - if (\defined('static::GUARD_NAMESPACE')) { |
|
| 47 | + if (\defined('static::GUARD_NAMESPACE')){ |
|
| 48 | 48 | // Yay! Isolation |
| 49 | - $permission = \constant(static::class . '::' . 'GUARD_NAMESPACE') . '.' . $permission; |
|
| 49 | + $permission = \constant(static::class.'::'.'GUARD_NAMESPACE').'.'.$permission; |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | return $permission; |
@@ -20,7 +20,8 @@ discard block |
||
| 20 | 20 | public function getGuard(): GuardInterface |
| 21 | 21 | { |
| 22 | 22 | $container = ContainerScope::getContainer(); |
| 23 | - if (empty($container) || !$container->has(GuardInterface::class)) { |
|
| 23 | + if (empty($container) || !$container->has(GuardInterface::class)) |
|
| 24 | + { |
|
| 24 | 25 | throw new ScopeException( |
| 25 | 26 | 'Unable to get `GuardInterface`, binding is missing or container scope is not set' |
| 26 | 27 | ); |
@@ -44,7 +45,8 @@ discard block |
||
| 44 | 45 | */ |
| 45 | 46 | protected function resolvePermission(string $permission): string |
| 46 | 47 | { |
| 47 | - if (\defined('static::GUARD_NAMESPACE')) { |
|
| 48 | + if (\defined('static::GUARD_NAMESPACE')) |
|
| 49 | + { |
|
| 48 | 50 | // Yay! Isolation |
| 49 | 51 | $permission = \constant(static::class . '::' . 'GUARD_NAMESPACE') . '.' . $permission; |
| 50 | 52 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | |
| 38 | 38 | public function __construct( |
| 39 | 39 | protected ResolverInterface $resolver |
| 40 | - ) { |
|
| 40 | + ){ |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
@@ -48,20 +48,20 @@ discard block |
||
| 48 | 48 | $parameters = \compact('actor', 'permission', 'context') + $context; |
| 49 | 49 | |
| 50 | 50 | //Mounting aliases |
| 51 | - foreach (static::ALIASES as $target => $alias) { |
|
| 51 | + foreach (static::ALIASES as $target => $alias){ |
|
| 52 | 52 | $parameters[$target] = $parameters[$alias]; |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - try { |
|
| 55 | + try{ |
|
| 56 | 56 | $method = new \ReflectionMethod($this, static::CHECK_METHOD); |
| 57 | - } catch (\ReflectionException $e) { |
|
| 57 | + }catch (\ReflectionException $e){ |
|
| 58 | 58 | throw new RuleException($e->getMessage(), $e->getCode(), $e); |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - try { |
|
| 61 | + try{ |
|
| 62 | 62 | return $method->invokeArgs($this, $this->resolver->resolveArguments($method, $parameters)); |
| 63 | - } catch (\Throwable $e) { |
|
| 64 | - throw new RuleException(\sprintf('[%s] %s', $this::class, $e->getMessage()), (int) $e->getCode(), $e); |
|
| 63 | + }catch (\Throwable $e){ |
|
| 64 | + throw new RuleException(\sprintf('[%s] %s', $this::class, $e->getMessage()), (int)$e->getCode(), $e); |
|
| 65 | 65 | } |
| 66 | 66 | } |
| 67 | 67 | } |
@@ -48,19 +48,26 @@ |
||
| 48 | 48 | $parameters = \compact('actor', 'permission', 'context') + $context; |
| 49 | 49 | |
| 50 | 50 | //Mounting aliases |
| 51 | - foreach (static::ALIASES as $target => $alias) { |
|
| 51 | + foreach (static::ALIASES as $target => $alias) |
|
| 52 | + { |
|
| 52 | 53 | $parameters[$target] = $parameters[$alias]; |
| 53 | 54 | } |
| 54 | 55 | |
| 55 | - try { |
|
| 56 | + try |
|
| 57 | + { |
|
| 56 | 58 | $method = new \ReflectionMethod($this, static::CHECK_METHOD); |
| 57 | - } catch (\ReflectionException $e) { |
|
| 59 | + } |
|
| 60 | + catch (\ReflectionException $e) |
|
| 61 | + { |
|
| 58 | 62 | throw new RuleException($e->getMessage(), $e->getCode(), $e); |
| 59 | 63 | } |
| 60 | 64 | |
| 61 | - try { |
|
| 65 | + try |
|
| 66 | + { |
|
| 62 | 67 | return $method->invokeArgs($this, $this->resolver->resolveArguments($method, $parameters)); |
| 63 | - } catch (\Throwable $e) { |
|
| 68 | + } |
|
| 69 | + catch (\Throwable $e) |
|
| 70 | + { |
|
| 64 | 71 | throw new RuleException(\sprintf('[%s] %s', $this::class, $e->getMessage()), (int) $e->getCode(), $e); |
| 65 | 72 | } |
| 66 | 73 | } |
@@ -28,16 +28,16 @@ discard block |
||
| 28 | 28 | { |
| 29 | 29 | $role = $this->argument('role'); |
| 30 | 30 | |
| 31 | - if ($role !== null && !$rbac->hasRole($role)) { |
|
| 31 | + if ($role !== null && !$rbac->hasRole($role)){ |
|
| 32 | 32 | throw new CommandException('Unknown role provided'); |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | - if ($role !== null) { |
|
| 35 | + if ($role !== null){ |
|
| 36 | 36 | $rows = $this->getRolePermissions($role, $rbac); |
| 37 | - } else { |
|
| 37 | + }else{ |
|
| 38 | 38 | $rows = []; |
| 39 | 39 | |
| 40 | - foreach ($rbac->getRoles() as $role) { |
|
| 40 | + foreach ($rbac->getRoles() as $role){ |
|
| 41 | 41 | /** @noinspection SlowArrayOperationsInLoopInspection */ |
| 42 | 42 | $rows = \array_merge( |
| 43 | 43 | $this->getRolePermissions($role, $rbac), |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | { |
| 64 | 64 | $permissions = []; |
| 65 | 65 | |
| 66 | - foreach ($rbac->getPermissions($role) as $permission => $rule) { |
|
| 66 | + foreach ($rbac->getPermissions($role) as $permission => $rule){ |
|
| 67 | 67 | $permissions[] = [ |
| 68 | 68 | 'role' => $role, |
| 69 | 69 | 'permission' => $permission, |
@@ -28,16 +28,21 @@ discard block |
||
| 28 | 28 | { |
| 29 | 29 | $role = $this->argument('role'); |
| 30 | 30 | |
| 31 | - if ($role !== null && !$rbac->hasRole($role)) { |
|
| 31 | + if ($role !== null && !$rbac->hasRole($role)) |
|
| 32 | + { |
|
| 32 | 33 | throw new CommandException('Unknown role provided'); |
| 33 | 34 | } |
| 34 | 35 | |
| 35 | - if ($role !== null) { |
|
| 36 | + if ($role !== null) |
|
| 37 | + { |
|
| 36 | 38 | $rows = $this->getRolePermissions($role, $rbac); |
| 37 | - } else { |
|
| 39 | + } |
|
| 40 | + else |
|
| 41 | + { |
|
| 38 | 42 | $rows = []; |
| 39 | 43 | |
| 40 | - foreach ($rbac->getRoles() as $role) { |
|
| 44 | + foreach ($rbac->getRoles() as $role) |
|
| 45 | + { |
|
| 41 | 46 | /** @noinspection SlowArrayOperationsInLoopInspection */ |
| 42 | 47 | $rows = \array_merge( |
| 43 | 48 | $this->getRolePermissions($role, $rbac), |
@@ -63,7 +68,8 @@ discard block |
||
| 63 | 68 | { |
| 64 | 69 | $permissions = []; |
| 65 | 70 | |
| 66 | - foreach ($rbac->getPermissions($role) as $permission => $rule) { |
|
| 71 | + foreach ($rbac->getPermissions($role) as $permission => $rule) |
|
| 72 | + { |
|
| 67 | 73 | $permissions[] = [ |
| 68 | 74 | 'role' => $role, |
| 69 | 75 | 'permission' => $permission, |
@@ -18,16 +18,16 @@ discard block |
||
| 18 | 18 | |
| 19 | 19 | public function __construct( |
| 20 | 20 | private readonly ContainerInterface $container |
| 21 | - ) { |
|
| 21 | + ){ |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | public function set(string $name, mixed $rule = null): RuleManager |
| 25 | 25 | { |
| 26 | - if (empty($rule)) { |
|
| 26 | + if (empty($rule)){ |
|
| 27 | 27 | $rule = $name; |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | - if (!$this->validateRule($rule)) { |
|
| 30 | + if (!$this->validateRule($rule)){ |
|
| 31 | 31 | throw new RuleException("Unable to set rule '{$name}', invalid rule body"); |
| 32 | 32 | } |
| 33 | 33 | |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | |
| 39 | 39 | public function remove(string $name): RuleManager |
| 40 | 40 | { |
| 41 | - if (!$this->has($name)) { |
|
| 41 | + if (!$this->has($name)){ |
|
| 42 | 42 | throw new RuleException("Undefined rule '{$name}'"); |
| 43 | 43 | } |
| 44 | 44 | |
@@ -60,26 +60,26 @@ discard block |
||
| 60 | 60 | |
| 61 | 61 | public function get(string $name): RuleInterface |
| 62 | 62 | { |
| 63 | - if (!$this->has($name)) { |
|
| 63 | + if (!$this->has($name)){ |
|
| 64 | 64 | throw new RuleException(\sprintf("Undefined rule '%s'", $name)); |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - if (!isset($this->rules[$name])) { |
|
| 67 | + if (!isset($this->rules[$name])){ |
|
| 68 | 68 | //Rule represented as class name |
| 69 | 69 | $rule = $name; |
| 70 | - } else { |
|
| 70 | + }else{ |
|
| 71 | 71 | $rule = $this->rules[$name]; |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - if ($rule instanceof RuleInterface) { |
|
| 74 | + if ($rule instanceof RuleInterface){ |
|
| 75 | 75 | return $rule; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - if (\is_string($rule)) { |
|
| 78 | + if (\is_string($rule)){ |
|
| 79 | 79 | //We are expecting that rule points to |
| 80 | 80 | $rule = $this->container->get($rule); |
| 81 | 81 | |
| 82 | - if (!$rule instanceof RuleInterface) { |
|
| 82 | + if (!$rule instanceof RuleInterface){ |
|
| 83 | 83 | throw new RuleException(\sprintf( |
| 84 | 84 | "Rule '%s' must point to RuleInterface, '%s' given", |
| 85 | 85 | $name, |
@@ -99,18 +99,18 @@ discard block |
||
| 99 | 99 | */ |
| 100 | 100 | private function validateRule(mixed $rule): bool |
| 101 | 101 | { |
| 102 | - if ($rule instanceof \Closure || $rule instanceof RuleInterface) { |
|
| 102 | + if ($rule instanceof \Closure || $rule instanceof RuleInterface){ |
|
| 103 | 103 | return true; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | - if (\is_array($rule)) { |
|
| 106 | + if (\is_array($rule)){ |
|
| 107 | 107 | return \is_callable($rule, true); |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - if (\is_string($rule) && \class_exists($rule)) { |
|
| 111 | - try { |
|
| 110 | + if (\is_string($rule) && \class_exists($rule)){ |
|
| 111 | + try{ |
|
| 112 | 112 | $reflection = new \ReflectionClass($rule); |
| 113 | - } catch (\ReflectionException) { |
|
| 113 | + }catch (\ReflectionException){ |
|
| 114 | 114 | return false; |
| 115 | 115 | } |
| 116 | 116 | |
@@ -23,11 +23,13 @@ discard block |
||
| 23 | 23 | |
| 24 | 24 | public function set(string $name, mixed $rule = null): RuleManager |
| 25 | 25 | { |
| 26 | - if (empty($rule)) { |
|
| 26 | + if (empty($rule)) |
|
| 27 | + { |
|
| 27 | 28 | $rule = $name; |
| 28 | 29 | } |
| 29 | 30 | |
| 30 | - if (!$this->validateRule($rule)) { |
|
| 31 | + if (!$this->validateRule($rule)) |
|
| 32 | + { |
|
| 31 | 33 | throw new RuleException("Unable to set rule '{$name}', invalid rule body"); |
| 32 | 34 | } |
| 33 | 35 | |
@@ -38,7 +40,8 @@ discard block |
||
| 38 | 40 | |
| 39 | 41 | public function remove(string $name): RuleManager |
| 40 | 42 | { |
| 41 | - if (!$this->has($name)) { |
|
| 43 | + if (!$this->has($name)) |
|
| 44 | + { |
|
| 42 | 45 | throw new RuleException("Undefined rule '{$name}'"); |
| 43 | 46 | } |
| 44 | 47 | |
@@ -60,26 +63,33 @@ discard block |
||
| 60 | 63 | |
| 61 | 64 | public function get(string $name): RuleInterface |
| 62 | 65 | { |
| 63 | - if (!$this->has($name)) { |
|
| 66 | + if (!$this->has($name)) |
|
| 67 | + { |
|
| 64 | 68 | throw new RuleException(\sprintf("Undefined rule '%s'", $name)); |
| 65 | 69 | } |
| 66 | 70 | |
| 67 | - if (!isset($this->rules[$name])) { |
|
| 71 | + if (!isset($this->rules[$name])) |
|
| 72 | + { |
|
| 68 | 73 | //Rule represented as class name |
| 69 | 74 | $rule = $name; |
| 70 | - } else { |
|
| 75 | + } |
|
| 76 | + else |
|
| 77 | + { |
|
| 71 | 78 | $rule = $this->rules[$name]; |
| 72 | 79 | } |
| 73 | 80 | |
| 74 | - if ($rule instanceof RuleInterface) { |
|
| 81 | + if ($rule instanceof RuleInterface) |
|
| 82 | + { |
|
| 75 | 83 | return $rule; |
| 76 | 84 | } |
| 77 | 85 | |
| 78 | - if (\is_string($rule)) { |
|
| 86 | + if (\is_string($rule)) |
|
| 87 | + { |
|
| 79 | 88 | //We are expecting that rule points to |
| 80 | 89 | $rule = $this->container->get($rule); |
| 81 | 90 | |
| 82 | - if (!$rule instanceof RuleInterface) { |
|
| 91 | + if (!$rule instanceof RuleInterface) |
|
| 92 | + { |
|
| 83 | 93 | throw new RuleException(\sprintf( |
| 84 | 94 | "Rule '%s' must point to RuleInterface, '%s' given", |
| 85 | 95 | $name, |
@@ -99,18 +109,24 @@ discard block |
||
| 99 | 109 | */ |
| 100 | 110 | private function validateRule(mixed $rule): bool |
| 101 | 111 | { |
| 102 | - if ($rule instanceof \Closure || $rule instanceof RuleInterface) { |
|
| 112 | + if ($rule instanceof \Closure || $rule instanceof RuleInterface) |
|
| 113 | + { |
|
| 103 | 114 | return true; |
| 104 | 115 | } |
| 105 | 116 | |
| 106 | - if (\is_array($rule)) { |
|
| 117 | + if (\is_array($rule)) |
|
| 118 | + { |
|
| 107 | 119 | return \is_callable($rule, true); |
| 108 | 120 | } |
| 109 | 121 | |
| 110 | - if (\is_string($rule) && \class_exists($rule)) { |
|
| 111 | - try { |
|
| 122 | + if (\is_string($rule) && \class_exists($rule)) |
|
| 123 | + { |
|
| 124 | + try |
|
| 125 | + { |
|
| 112 | 126 | $reflection = new \ReflectionClass($rule); |
| 113 | - } catch (\ReflectionException) { |
|
| 127 | + } |
|
| 128 | + catch (\ReflectionException) |
|
| 129 | + { |
|
| 114 | 130 | return false; |
| 115 | 131 | } |
| 116 | 132 | |