Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with getting rules #110

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions Dice.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ public function addRule($name, array $rule) {
*/
public function getRule($name) {
$lcName = strtolower(ltrim($name, '\\'));
if (isset($this->rules[$lcName])) return $this->rules[$lcName];
if (isset($this->rules[$lcName])) $main_rule = $this->rules[$lcName];
else $main_rule = [];

foreach ($this->rules as $key => $rule) { // Find a rule which matches the class described in $name where:
if (empty($rule['instanceOf']) // It's not a named instance, the rule is applied to a class name
&& $key !== '*' // It's not the default rule
&& is_subclass_of($name, $key) // The rule is applied to a parent class
&& (!array_key_exists('inherit', $rule) || $rule['inherit'] === true )) // And that rule should be inherited to subclasses
return $rule;
&& (!array_key_exists('inherit', $rule) || $rule['inherit'] === true )) { // And that rule should be inherited to subclasses
$second_rule = $rule;
break;
}
}
// No rule has matched, return the default rule if it's set
return isset($this->rules['*']) ? $this->rules['*'] : [];
// Merge in the default rule if it's set
$default = isset($this->rules['*']) ? $this->rules['*'] : [];
return array_merge_recursive($default, $second_rule, $main_rule);
}

/**
Expand Down