After many refactorings, no rules use the previous validation engine.
That means we can remove the unused code from the repository and switch
from the previous to the new validation engine everywhere.
This commit will also soft deprecate the methods "validate()", and
"check()" in all the rules and the "assert()" in all rules but the
Validator itself. That means using those methods will still be allowed,
but static analysis tools might complain.
This is a big step toward releasing the next major version, as the code
is pretty much the way it should be when I release the next version.
There's some documentation to be updated, and I would like to change the
behavior of a couple of rules.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Although helpful, the changes in the Min, Max, and Length rules made
using those rules more verbose. This commit will simplify their use by
allowing users to use them as prefixes.
Because I was creating prefixes for those rules, I made other cool
prefixes. Doing that is scary because it will generate more code to
support, and I would have liked to avoid that. However, that's a
valuable addition, and it's worth the risk.
I might reconsider that in the future, but for now, that looks like a
good idea.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Having a named constructor with a name that clarifies that we don't
expect to execute the stub will make it easier to read the tests.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Creating a specific exception for each rule adds a painful overhead. If
you want to make a custom message for your rule, you will need to create
an exception and then register that exception namespace to be able to
use it—all that is just for customizing the message of your rule.
Having different namespaces also implies that you need to fetch the
exception of the rule from another directory to change it. As Uncle Bob
said, "Classes that change together belong together. Classes that are
not reused together should not be grouped."
This commit will drastically change this library, moving all the
templates from the exceptions to the rules. Consequently, the Factory
becomes much simpler, and the library gets a bit smaller, too.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Currently, we convert the properties of a rule into parameters and pass
them to the exceptions. That complicates things for a few reasons:
1. The exception knows too much: there's a lot of information in an
object, and the exception would only need a few parameters to work
correctly.
2. Any variable change becomes a backward compatibility break: if we
change the name of the variable type in a rule, even if it's a
private one, we may need to change the template, which is a backward
compatibility break.
3. The factory is bloated because of introspection tricks: it reads the
properties from the class, even from the parent, and then passes it
to the exception.
Of course, that means we introduce another method to `Validatable`, but
in most cases, extending `AbstractRule` is enough to create a new rule.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This change will bring many breaking changes. The good thing is that we
can finally use more modern resources available in PHP.
I can imagine that's not a popular change since it will bring many
breaking changes to users, but we shouldn't be stuck in time because of
that. Using some of those features will make it easier to contribute to
the project. At least, I hope so.
There are still some useless doc-blocks, and we're not using "readonly"
properties when we could. I aim to send those changes soon.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
After the refactoring on the Factory class [1], to throw exceptions of a
specific rule, it is necessary to add the exception namespace of that
rule. That change makes sense when someone wants to create rules from
the Validator class, but when using rules as classes, it's not as handy.
This commit will auto-resolve exception based on the rule namespace,
just as it used to be.
[1]: 1f217dda66
Co-authored-by: Casey McLaughlin <caseyamcl@gmail.com>
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
The constructor of "Factory" has three arguments and, even though none
of them are needed, they are all required. Those arguments allow users
to customize the namespaces of rules and exceptions, and also to define
a callable that will translate the template messages.
This commit will remove those parameters from the constructor of
"Factory," and create methods that will allow users to customize the
namespaces and the translator.
The methods that this commit will create will not change the state of
"Factory," but they will create a clone with the customizations. It is
imperative that the "Factory" is immutable. Since the "Factory" is a
Singleton, allowing it to change could cause unexpected behaviors.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Due to the current status of the development of the library, it seems
like we will be supporting version 1.1 for a long time. Even when we
release version 2.0 we will still give support for version 1.1 for a
while.
This commit will make sure that version 1.1 is fully supported for PHP
7.2 and 7.3. Also, it will remove the support for HHVM since it will not
keep the compatibility with PHP anymore [1].
In order to make that happen, this commit will create a TestCase from
Validation so we can use the same API to create mocks in both PHPUnit
versions 4.0 and 5.0.
During the development of this commit, I noticed that PHPUnit 4.0 had
issues to mock "SplFileInfo" and for that reason, this commit will also
replace those mocks by "SplFileInfo" instances.
[1]: https://hhvm.com/blog/2018/09/12/end-of-php-support-future-of-hack.html
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit will make sure that every class, interface, or trait will
have the "@author" annotation in it.
In order to create a list of authors, I used the "git blame" command,
which means that if someone changed or even created the file but does
not have any remaining line will not be shown in the list; it's a
trade-off worth but it is worth it. The other way to do it would be
carefully checking each file.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Make the ValidationException a little bit less mutable than before. All
its dependencies are now passed into the constructor.
This commit also make the Factory pass the translator to the exceptions
allowing to define the translator before the exception gets created.
This change is not the ideal one, later I would like to not need the
Singleton from the Factory to do that, but for now it seems like a good
approach.
One more thing that this commit does is to introduce the "id" for
Exceptions. Key can be either the defined "name" or the name of the rule
that throwed the exception. This method will be handy to identify
exceptions better.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
Before this change every create rule must have an exception. This commit
allows to create rules without specific exceptions, so when the
exception of the rule is not found Validation uses ValidationException
instead.
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
With this code the Factory class will be used also to create Exceptions.
In order to do that, the AbstractRule::reportError() was changed, so the
tests of the AbstractRule class.
What this commit also does:
- Port code to PHP 7;
- Do not keep the default instance of the Factory in the Validator
class;
- Make Factory final.
Appending a prefix to search new rules under required that the namespace
(prefix) being added always ended with a trailing namespace character so
rules could successfully be found under it. This ensures that the
separator is always present.
Changes a test for a rule which does not implement Respect's interface
to an actual class so we don't need to declare one to use as a stub.