Skip to content

Fix attributes collecting#249

Merged
samdark merged 21 commits into
masterfrom
fix-attributes-collecting
Jul 19, 2022
Merged

Fix attributes collecting#249
samdark merged 21 commits into
masterfrom
fix-attributes-collecting

Conversation

@xepozz

@xepozz xepozz commented Jul 16, 2022

Copy link
Copy Markdown
Member
Q A
Is bugfix? ✔️
New feature? ✔️
Breaks BC? ✔️
Fixed issues #243
  • Each rules can be used as an attribute
  • Even multiple times
  • HasOne and HasMany were replaced with Embedded

I've added TODO about collecting attributes in lazy mode. It may be useful because of using Reflection to get rules collection.

@xepozz xepozz marked this pull request as ready for review July 16, 2022 04:50
@xepozz

xepozz commented Jul 16, 2022

Copy link
Copy Markdown
Member Author

BTW, could someone fix psalm issue? :)

@xepozz xepozz requested a review from a team July 16, 2022 05:09
@xepozz xepozz added the status:code review The pull request needs review. label Jul 16, 2022
@andrew-demb

Copy link
Copy Markdown
Contributor

@xepozz here it is

diff --git a/src/Attribute/Embedded.php b/src/Attribute/Embedded.php
index eb80e82..a568ad8 100644
--- a/src/Attribute/Embedded.php
+++ b/src/Attribute/Embedded.php
@@ -18,14 +18,14 @@ use Yiisoft\Validator\ValidationContext;
 #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
 final class Embedded extends GroupRule
 {
+    /**
+     * @psalm-param Closure(mixed, ValidationContext):bool|null $when
+     */
     public function __construct(
         private string $referenceClassName,
         string $message = 'This value is not a valid.',
         bool $skipOnEmpty = false,
         bool $skipOnError = false,
-        /**
-         * @psalm-param Closure(mixed, ValidationContext):bool|null
-         */
         ?Closure $when = null,
     ) {
         parent::__construct($message, $skipOnEmpty, $skipOnError, $when);
diff --git a/src/Rule/GroupRule.php b/src/Rule/GroupRule.php
index 46dfccb..31f4ea6 100644
--- a/src/Rule/GroupRule.php
+++ b/src/Rule/GroupRule.php
@@ -27,7 +27,7 @@ abstract class GroupRule implements ParametrizedRuleInterface, BeforeValidationI
         private bool $skipOnEmpty = false,
         private bool $skipOnError = false,
         /**
-         * @psalm-param Closure(mixed, ValidationContext):bool|null
+         * @var Closure(mixed, ValidationContext):bool|null
          */
         private ?Closure $when = null,
     ) {

return $this->collectAttributes($classMeta);
}

// TODO: use Generator to collect attributes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've mentioned it in the PR description

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samdark

samdark commented Jul 16, 2022

Copy link
Copy Markdown
Member

Looks way better than current HasOne and HasMany 👍

@samdark

samdark commented Jul 16, 2022

Copy link
Copy Markdown
Member

@samdark

samdark commented Jul 16, 2022

Copy link
Copy Markdown
Member

This one includes #244, right?

@samdark

samdark commented Jul 16, 2022

Copy link
Copy Markdown
Member

Also it seems it will solve #193, right?

@xepozz

xepozz commented Jul 16, 2022

Copy link
Copy Markdown
Member Author

Not sure about the last one, but the first has to be removed after the merge

@xepozz

xepozz commented Jul 16, 2022

Copy link
Copy Markdown
Member Author

Comment thread src/DataSet/AttributeDataSet.php
Comment thread src/Attribute/Embedded.php Outdated
@samdark

samdark commented Jul 16, 2022

Copy link
Copy Markdown
Member
  1. Need to mention PHP 8.0 limitations in readme.
  2. Need it to be usable as code, not only as an attribute.

@samdark

samdark commented Jul 17, 2022

Copy link
Copy Markdown
Member

PHP 8.1 specific tests should be separated and marked as such.

# Conflicts:
#	src/DataSet/AttributeDataSet.php
#	src/Rule/AtLeast.php
#	src/Rule/Boolean.php
#	src/Rule/CompareTo.php
#	src/Rule/Composite.php
#	src/Rule/Count.php
#	src/Rule/Each.php
#	src/Rule/Email.php
#	src/Rule/HasLength.php
#	src/Rule/InRange.php
#	src/Rule/Ip.php
#	src/Rule/Json.php
#	src/Rule/Nested.php
#	src/Rule/Number.php
#	src/Rule/Regex.php
#	src/Rule/Required.php
#	src/Rule/Subset.php
#	src/Rule/Url.php
@xepozz xepozz requested a review from a team July 19, 2022 00:29
return $this->collectAttributes($classMeta);
}

// TODO: use Generator to collect attributes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why if we return ruleset as array? We would not gain anything, I think.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validator may fail on one of any rules and not continue to check others.
As far as I know, it's still converts all iterables to array and after check them one by one, but we need to change it.

@xepozz xepozz mentioned this pull request Jul 19, 2022
@samdark samdark merged commit 75fd75e into master Jul 19, 2022
@samdark samdark deleted the fix-attributes-collecting branch July 19, 2022 15:12
@samdark

samdark commented Jul 19, 2022

Copy link
Copy Markdown
Member

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:code review The pull request needs review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants