Skip to content

Commit

Permalink
Reformat codebase using prettier (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
cseufert authored Oct 3, 2022
1 parent 08dce70 commit 0abde11
Show file tree
Hide file tree
Showing 24 changed files with 1,344 additions and 1,062 deletions.
115 changes: 76 additions & 39 deletions Extra/RuleValidator.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,88 @@
<?php
namespace Dice\Extra;
class RuleValidator {
private $dice;
class RuleValidator
{
private $dice;

public function __construct(\Dice\Dice $dice) {
$this->dice = $dice;
}
public function __construct(\Dice\Dice $dice)
{
$this->dice = $dice;
}

public function addRule($name, array $rule) {
$this->checkValidKeys($rule);
$this->checkBoolean($rule, 'inherit');
$this->checkBoolean($rule, 'shared');
$this->checkNumericArray($rule, 'constructParams');
$this->checkNumericArray($rule, 'shareInstances');
$this->checkNumericArray($rule, 'call');
$this->dice->addRule($name, $rule);
}
public function addRule($name, array $rule)
{
$this->checkValidKeys($rule);
$this->checkBoolean($rule, "inherit");
$this->checkBoolean($rule, "shared");
$this->checkNumericArray($rule, "constructParams");
$this->checkNumericArray($rule, "shareInstances");
$this->checkNumericArray($rule, "call");
$this->dice->addRule($name, $rule);
}

private function checkValidKeys($rule) {
$validKeys = ['call', 'shared', 'substitutions', 'instanceOf', 'inherit', 'shareInstances', 'constructParams'];
foreach ($rule as $name => $value) {
if (!in_array($name, $validKeys)) throw new \InvalidArgumentException('Invalid rule option: '. $name);
}
}
private function checkValidKeys($rule)
{
$validKeys = [
"call",
"shared",
"substitutions",
"instanceOf",
"inherit",
"shareInstances",
"constructParams",
];
foreach ($rule as $name => $value) {
if (!in_array($name, $validKeys)) {
throw new \InvalidArgumentException(
"Invalid rule option: " . $name
);
}
}
}

public function create($name, array $args = [], array $share = []) {
return $this->dice->create($name, $args, $share);
}
public function create($name, array $args = [], array $share = [])
{
return $this->dice->create($name, $args, $share);
}

public function checkBoolean($rule, $key) {
if (!isset($rule[$key])) return;
public function checkBoolean($rule, $key)
{
if (!isset($rule[$key])) {
return;
}

if (!is_bool($rule[$key])) throw new \InvalidArgumentException('Rule option ' . $key . ' must be true or false');
}
if (!is_bool($rule[$key])) {
throw new \InvalidArgumentException(
"Rule option " . $key . " must be true or false"
);
}
}

public function checkNumericArray($rule, $key) {
if (!isset($rule[$key])) return;
public function checkNumericArray($rule, $key)
{
if (!isset($rule[$key])) {
return;
}

if (count(array_filter(array_keys($rule[$key]), 'is_string')) > 0) throw new \InvalidArgumentException('Rule option ' . $key . ' must be a seqential array not an associative array');
if (count(array_filter(array_keys($rule[$key]), "is_string")) > 0) {
throw new \InvalidArgumentException(
"Rule option " .
$key .
" must be a seqential array not an associative array"
);
}
}

}
public function checkAssocArray($rule, $key)
{
if (!isset($rule[$key])) {
return;
}

public function checkAssocArray($rule, $key) {
if (!isset($rule[$key])) return;

if (count(array_filter(array_keys($rule[$key]), 'is_string')) === 0) throw new \InvalidArgumentException('Rule option ' . $key . ' must be a an associative array');

}

}
if (count(array_filter(array_keys($rule[$key]), "is_string")) === 0) {
throw new \InvalidArgumentException(
"Rule option " . $key . " must be a an associative array"
);
}
}
}
209 changes: 141 additions & 68 deletions Loader/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,158 @@
* @author Tom Butler [email protected] *
* @copyright 2012-2018 Tom Butler <[email protected]> | https:// r.je/dice.html *
* @license http:// www.opensource.org/licenses/bsd-license.php BSD License *
* @version 3.0 */
*/
namespace Dice\Loader;
class Xml {
private function getComponent(\SimpleXmlElement $element, $forceInstance = false) {
if ($forceInstance) return [\Dice\Dice::INSTANCE => (string) $element];
else if ($element->instance) return [\Dice\Dice::INSTANCE => (string) $element->instance];
else return (string) $element;
}
class Xml
{
private function getComponent(
\SimpleXmlElement $element,
$forceInstance = false
) {
if ($forceInstance) {
return [\Dice\Dice::INSTANCE => (string) $element];
} elseif ($element->instance) {
return [\Dice\Dice::INSTANCE => (string) $element->instance];
} else {
return (string) $element;
}
}

private function loadV1(\SimpleXmlElement $xml, \Dice\Dice $dice) {
$rules = [];
private function loadV1(\SimpleXmlElement $xml, \Dice\Dice $dice)
{
$rules = [];

foreach ($xml as $key => $value) {
$rule = $dice->getRule((string) $value->name);
foreach ($xml as $key => $value) {
$rule = $dice->getRule((string) $value->name);

if (isset($value->shared)) $rule['shared'] = ((string) $value->shared === 'true');
if (isset($value->shared)) {
$rule["shared"] = (string) $value->shared === "true";
}

if (isset($value->inherit)) $rule['inherit'] = ($value->inherit == 'false') ? false : true;
if ($value->call) {
foreach ($value->call as $name => $call) {
$callArgs = [];
if ($call->params) foreach ($call->params->children() as $key => $param) $callArgs[] = $this->getComponent($param);
$rule['call'][] = [(string) $call->method, $callArgs];
}
}
if ($value->instanceOf) $rule['instanceOf'] = (string) $value->instanceOf;
if ($value->newInstances) foreach ($value->newInstances as $ni) $rule['newInstances'][] = (string) $ni;
if ($value->substitutions) foreach ($value->substitutions as $use) $rule['substitutions'][(string) $use->as] = $this->getComponent($use->use, true);
if ($value->constructParams) foreach ($value->constructParams->children() as $child) $rule['constructParams'][] = $this->getComponent($child);
if ($value->shareInstances) foreach ($value->shareInstances as $share) $rule['shareInstances'][] = $this->getComponent($share);
$rules[$value['name']] = $rule;
$dice->addRule((string) $value->name, $rule);
}
return $rules;
}
if (isset($value->inherit)) {
$rule["inherit"] = $value->inherit == "false" ? false : true;
}
if ($value->call) {
foreach ($value->call as $name => $call) {
$callArgs = [];
if ($call->params) {
foreach ($call->params->children() as $key => $param) {
$callArgs[] = $this->getComponent($param);
}
}
$rule["call"][] = [(string) $call->method, $callArgs];
}
}
if ($value->instanceOf) {
$rule["instanceOf"] = (string) $value->instanceOf;
}
if ($value->newInstances) {
foreach ($value->newInstances as $ni) {
$rule["newInstances"][] = (string) $ni;
}
}
if ($value->substitutions) {
foreach ($value->substitutions as $use) {
$rule["substitutions"][
(string) $use->as
] = $this->getComponent($use->use, true);
}
}
if ($value->constructParams) {
foreach ($value->constructParams->children() as $child) {
$rule["constructParams"][] = $this->getComponent($child);
}
}
if ($value->shareInstances) {
foreach ($value->shareInstances as $share) {
$rule["shareInstances"][] = $this->getComponent($share);
}
}
$rules[$value["name"]] = $rule;
$dice->addRule((string) $value->name, $rule);
}
return $rules;
}

private function loadV2(\SimpleXmlElement $xml, \Dice\Dice $dice) {
$rules = [];
private function loadV2(\SimpleXmlElement $xml, \Dice\Dice $dice)
{
$rules = [];

foreach ($xml as $key => $value) {
$rule = $dice->getRule((string) $value->name);
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);
$rules[$value['name']] = $rule;
$dice->addRule((string) $value['name'], $rule);
}
return $rules;
}
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);
}
}
$rules[$value["name"]] = $rule;
$dice->addRule((string) $value["name"], $rule);
}
return $rules;
}

public function load($xml, \Dice\Dice $dice = null, $displayWarning = true) {
public function load($xml, \Dice\Dice $dice = null, $displayWarning = true)
{
if ($displayWarning) {
trigger_error(
'Deprecated: The XML loader is being removed in the next version of Dice please use $xmlLoader->convert(\'' .
$xml .
'\', \'path/to/rules.json\'); to convert the rules to JSON format',
E_USER_WARNING
);
}

if ($displayWarning) {
trigger_error('Deprecated: The XML loader is being removed in the next version of Dice please use $xmlLoader->convert(\'' . $xml . '\', \'path/to/rules.json\'); to convert the rules to JSON format', E_USER_WARNING);
}
if ($dice === null) {
$dice = new \Dice\Dice();
}
if (!($xml instanceof \SimpleXmlElement)) {
$xml = simplexml_load_file($xml);
}
$ns = $xml->getNamespaces();
$nsName = isset($ns[""]) ? $ns[""] : "";

if ($dice === null) $dice = new \Dice\Dice;
if (!($xml instanceof \SimpleXmlElement)) $xml = simplexml_load_file($xml);
$ns = $xml->getNamespaces();
$nsName = (isset($ns[''])) ? $ns[''] : '';
if ($nsName == "https://r.je/dice/2.0") {
return $this->loadV2($xml, $dice);
} else {
return $this->loadV1($xml, $dice);
}
}

if ($nsName == 'https://r.je/dice/2.0') return $this->loadV2($xml, $dice);
else return $this->loadV1($xml, $dice);
}
public function convert($xml, $outputJson)
{
$rules = $this->load($xml);

public function convert($xml, $outputJson) {
$rules = $this->load($xml);

file_put_contents($outputJson, json_encode($rules, JSON_PRETTY_PRINT));
}
file_put_contents($outputJson, json_encode($rules, JSON_PRETTY_PRINT));
}
}
20 changes: 13 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
"description": "A minimalist Dependency injection container (DIC) for PHP 7.1+ and PHP 8.0+.",
"license": "BSD-2-Clause",
"homepage": "https://github.com/moddengine/Dice",
"keywords": ["Dependency injection", "ioc", "Dependency injection container", "DI"],
"keywords": [
"Dependency injection",
"ioc",
"Dependency injection container",
"DI"
],
"authors": [
{
"name": "Tom Butler",
"email": "[email protected]"
}
{
"name": "Tom Butler",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.1.0"
"php": ">=7.1.0",
"ext-json": "*"
},
"autoload": {
"psr-4": {
"psr-4": {
"Dice\\": "./src/"
}
},
Expand Down
Loading

0 comments on commit 0abde11

Please sign in to comment.