Dice\Dice::addRule PHP Method

addRule() public method

Add a rule $rule to the class $name
public addRule ( string $name, array $rule )
$name string The name of the class to add the rule for
$rule array The container can be fully configured using rules provided by associative arrays. See {@link https://r.je/dice.html#example3} for a description of the rules.
    public function addRule($name, array $rule)
    {
        if (isset($rule['instanceOf']) && (!array_key_exists('inherit', $rule) || $rule['inherit'] === true)) {
            $rule = array_merge_recursive($this->getRule($rule['instanceOf']), $rule);
        }
        $this->rules[ltrim(strtolower($name), '\\')] = array_merge_recursive($this->getRule($name), $rule);
    }

Usage Example

示例#1
0
文件: Xml.php 项目: samilesma/Dice
 private function loadV2(\SimpleXmlElement $xml, \Dice\Dice $dice)
 {
     foreach ($xml as $key => $value) {
         $rule = $dice->getRule((string) $value->name);
         if ($value->call) {
             foreach ($value->call as $name => $call) {
                 $callArgs = [];
                 foreach ($call->children() as $key => $param) {
                     $callArgs[] = $this->getComponent($param);
                 }
                 $rule['call'][] = [(string) $call['method'], $callArgs];
             }
         }
         if (isset($value['inherit'])) {
             $rule['inherit'] = $value['inherit'] == 'false' ? false : true;
         }
         if ($value['instanceOf']) {
             $rule['instanceOf'] = (string) $value['instanceOf'];
         }
         if (isset($value['shared'])) {
             $rule['shared'] = (string) $value['shared'] === 'true';
         }
         if ($value->constructParams) {
             foreach ($value->constructParams->children() as $child) {
                 $rule['constructParams'][] = $this->getComponent($child);
             }
         }
         if ($value->substitute) {
             foreach ($value->substitute as $use) {
                 $rule['substitutions'][(string) $use['as']] = $this->getComponent($use['use'], true);
             }
         }
         if ($value->shareInstances) {
             foreach ($value->shareInstances->children() as $share) {
                 $rule['shareInstances'][] = $this->getComponent($share);
             }
         }
         $dice->addRule((string) $value['name'], $rule);
     }
 }
All Usage Examples Of Dice\Dice::addRule