-
Notifications
You must be signed in to change notification settings - Fork 67
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
Feature Request #56
Comments
Good ideas. What I did in my fork was modify the Rule constructor to accept an array of settings, similar to your example 1. So I can do: Aside from being visually different, I'm not understanding what would be gained over the above by adding fluent setter methods, as in your example 2. |
I'm in two minds about giving multiple syntaxes for the same configuration as it makes examples more confusing, that said I think Garrett's is the simplest solution. In theory the Rule class could be removed entirely and just allow an array config, but imho the class gives people a quick reference for what options are available and the types they store. I'm tempted, since we already have XML/JSON extensions to just add a new class that extends Rule class ShorthandRule extends Rule {
public function __construct(array $options) {
foreach ($options as $name => $value) $this->$name = $value;
}
}
use ShorthandRule as Rule;
use \Dice\Dice;
$dice = new Dice;
$dice->addRule('PDO'. new Rule(['shared' => true]); This has the advantage of not needing the Dice class to understand two formats (Array and object) which is better for performance and code clarity doesn't need extra validation, doesn't break BC and exists as an optional extra. Having said that, there's nothing to stop anyone adding the Shorthand class, or a class that implements chainable setters that works with Dice just by extending the rule class and adding whatever methods which is arguably better as it essentially allows developers to define their own syntax for their project. |
Just to put my two cents in I really like the |
@TomBZombie class Rule {
public function __construct(array $options = []) {
if ($options) foreach ($options as $name => $value) $this->$name = $value;
}
//...
} kind regards |
Hi @garrettw,
But as in domain models (with private/protected properties where setters/getters are needed as an interface) this would mean a really fatter class. best regards |
Having thought about this some more, I'm going to do a complete 180 here and suggest the potential of the removal of the Rule class entirely. This would be very BC Breaking however:
$rule = new \Dice\Rule; becomes: $rule = ['substitutions' => ['C' => ['instance' => '$MyC']]];
|
@TomBZombie |
@TomBZombie I'm for whatever you think is best. You always do reason things out very thoroughly. Just for argument's sake, here's how I implemented the changed Rule constructor:
|
As a step further and to even better support JSON, perhaps the addRule() method should be renamed addRules.. that way you could do: $dice->addRules(json_decode($json)); Where the JSON is something like: {
"class1": {
"shared": true
},
"MyClass2": {
"instanceOf": "class2"
}
} The only problem with this is you'd need to array_walk on the method calls to lowercase/trim the rule name but it might be simpler overall. |
@TomBZombie
...just my opinion. best regards |
Chiming in here: if the rules are specified as a nested set of arrays, Dice should properly validate it at spit out a nice error message if it finds something that doesn't make sense. I've seen too many developer hours being wasted because of one config array key having a small typo and configuration arrays not being validated. |
Agreed, I'd probably supply the validator as a separate class that isn't used by Dice, or a wrapper for the Dice instance, that way you'd gain performance in production and have validation during development. |
👍 |
And what about to create some simple interface on top of the Dice, something simmilar to Auryn.
|
That's almost as it was before, other than you're telling Dice about each option separately, I'm not sure that's really any more useful than the current method. It doesn't really help with this request for reducing the number of lines required for configuration. |
OK, maybe it can be just an additional package for dice ;) |
I've just released 2.0 which handles all the open issues and I've released a version for PHP5.4-5.5, see the relevant branch/release :) |
@TomBZombie
Hi Tom,
it would be nice to reduce the number of lines of code when implementing a rule.
A couple of simple ways to achieve that:
The text was updated successfully, but these errors were encountered: