44
55namespace yii2 \extensions \nestedsets \tests \support \model ;
66
7+ use yii \db \ActiveRecord ;
8+ use yii2 \extensions \nestedsets \NestedSetsBehavior ;
9+
710/**
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+ *
830 * @phpstan-property int $depth
931 * @phpstan-property int $id
1032 * @phpstan-property int $lft
1133 * @phpstan-property int $rgt
1234 * @phpstan-property string $name
35+ *
36+ * @copyright Copyright (C) 2023 Terabytesoftw.
37+ * @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
1338 */
14- final class TreeWithStrictValidation extends Tree
39+ final class TreeWithStrictValidation extends ActiveRecord
1540{
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+
1665 public function rules (): array
1766 {
1867 return [
@@ -21,4 +70,19 @@ public function rules(): array
2170 ['name ' , 'match ' , 'pattern ' => '/^[A-Z]/ ' , 'message ' => 'Name must start with an uppercase letter. ' ],
2271 ];
2372 }
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+ }
2488}
0 commit comments