From e0768a3835454162f356a75b8190b9f16d8e97ad Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 10 Jun 2016 12:08:27 -0400 Subject: [PATCH] Fix issue with getting rules If the default rule was set or the class was set and you used a named instance, the defaults from the class and default would not be carried down. --- Dice.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Dice.php b/Dice.php index 9f18535..19ff24c 100644 --- a/Dice.php +++ b/Dice.php @@ -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); } /**