4
4
5
5
namespace yii2 \extensions \nestedsets \tests \support \model ;
6
6
7
+ use yii \db \ActiveRecord ;
8
+ use yii2 \extensions \nestedsets \NestedSetsBehavior ;
9
+
7
10
/**
11
+ * Active Record model for testing strict validation rules with nested sets behavior.
12
+ *
13
+ * This class attaches the {@see NestedSetsBehavior} to enable hierarchical data structure operations for a single tree,
14
+ * with additional strict validation rules applied to the name attribute.
15
+ *
16
+ * The model is designed for testing property resolution, behavior integration, and validation scenarios involving a
17
+ * single tree with custom validation requirements.
18
+ *
19
+ * It ensures proper type inference and property access when behaviors are attached to Yii Active Record models, and
20
+ * enforces strict validation for the name property, including required, minimum length, and pattern constraints.
21
+ *
22
+ * Key features:
23
+ * - Behavior attachment for hierarchical data structures with single tree support.
24
+ * - Nested sets model functionality for tree operations.
25
+ * - Property resolution testing for behavior integration.
26
+ * - Static analysis validation for behavior property access.
27
+ * - Strict validation rules for the name attribute (required, minimum length, pattern).
28
+ * - Table mapping with the `tree` table.
29
+ *
8
30
* @phpstan-property int $depth
9
31
* @phpstan-property int $id
10
32
* @phpstan-property int $lft
11
33
* @phpstan-property int $rgt
12
34
* @phpstan-property string $name
35
+ *
36
+ * @copyright Copyright (C) 2023 Terabytesoftw.
37
+ * @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
13
38
*/
14
- final class TreeWithStrictValidation extends Tree
39
+ final class TreeWithStrictValidation extends ActiveRecord
15
40
{
41
+ public function behaviors (): array
42
+ {
43
+ return [
44
+ 'nestedSetsBehavior ' => NestedSetsBehavior::class,
45
+ ];
46
+ }
47
+
48
+ /**
49
+ * @phpstan-return TreeQuery<self>
50
+ */
51
+ public static function find (): TreeQuery
52
+ {
53
+ return new TreeQuery (self ::class);
54
+ }
55
+
56
+ public function isTransactional ($ operation ): bool
57
+ {
58
+ if ($ operation === ActiveRecord::OP_DELETE ) {
59
+ return false ;
60
+ }
61
+
62
+ return parent ::isTransactional ($ operation );
63
+ }
64
+
16
65
public function rules (): array
17
66
{
18
67
return [
@@ -21,4 +70,19 @@ public function rules(): array
21
70
['name ' , 'match ' , 'pattern ' => '/^[A-Z]/ ' , 'message ' => 'Name must start with an uppercase letter. ' ],
22
71
];
23
72
}
73
+
74
+ public static function tableName (): string
75
+ {
76
+ return '{{%tree}} ' ;
77
+ }
78
+
79
+ /**
80
+ * @phpstan-return array<string, int>
81
+ */
82
+ public function transactions (): array
83
+ {
84
+ return [
85
+ self ::SCENARIO_DEFAULT => self ::OP_ALL ,
86
+ ];
87
+ }
24
88
}
0 commit comments