Skip to content

Commit 6f01a4d

Browse files
feat: Enhance Active Record models and queries with detailed documentation and strict validation rules for nested sets behavior. (#79)
1 parent bab0185 commit 6f01a4d

File tree

7 files changed

+187
-14
lines changed

7 files changed

+187
-14
lines changed

tests/support/model/ExtendableMultipleTree.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,38 @@
88
use yii2\extensions\nestedsets\tests\support\stub\ExtendableNestedSetsBehavior;
99

1010
/**
11+
* Active Record model for testing multiple tree support with nested sets behavior extension.
12+
*
13+
* This class demonstrates a scenario where a model attaches the {@see ExtendableNestedSetsBehavior} to enable
14+
* hierarchical data structure operations with support for multiple trees.
15+
*
16+
* The model provides tree structure functionality through the behavior properties and methods, allowing for nested sets
17+
* model implementation on Active Record instances with a tree attribute.
18+
*
19+
* The model is designed for testing property resolution and behavior integration in scenarios involving multiple tree
20+
* columns.
21+
*
22+
* It ensures proper type inference and property access when behaviors are attached to Yii Active Record models with a
23+
* tree context.
24+
*
25+
* Key features:
26+
* - Behavior attachment for hierarchical data structures with multiple tree support.
27+
* - Nested sets model functionality for tree operations.
28+
* - Property resolution testing for behavior integration.
29+
* - Static analysis validation for behavior property access.
30+
* - Table mapping with the `multiple_tree` table.
31+
*
1132
* @phpstan-property int $depth
1233
* @phpstan-property int $id
1334
* @phpstan-property int $lft
1435
* @phpstan-property int $rgt
1536
* @phpstan-property int $tree
1637
* @phpstan-property string $name
38+
*
39+
* @copyright Copyright (C) 2023 Terabytesoftw.
40+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
1741
*/
18-
class ExtendableMultipleTree extends ActiveRecord
42+
final class ExtendableMultipleTree extends ActiveRecord
1943
{
2044
public function behaviors(): array
2145
{
@@ -28,11 +52,11 @@ public function behaviors(): array
2852
}
2953

3054
/**
31-
* @phpstan-return ExtendableMultipleTreeQuery<static>
55+
* @phpstan-return ExtendableMultipleTreeQuery<self>
3256
*/
3357
public static function find(): ExtendableMultipleTreeQuery
3458
{
35-
return new ExtendableMultipleTreeQuery(static::class);
59+
return new ExtendableMultipleTreeQuery(self::class);
3660
}
3761

3862
public function rules(): array

tests/support/model/ExtendableMultipleTreeQuery.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,28 @@
88
use yii2\extensions\nestedsets\NestedSetsQueryBehavior;
99

1010
/**
11+
* Active Query class for {@see ExtendableMultipleTree} with nested sets query behavior support.
12+
*
13+
* Provides an Active Query implementation tailored for the {@see ExtendableMultipleTree} model, enabling integration
14+
* with the {@see NestedSetsQueryBehavior} for hierarchical data operations in multiple tree scenarios.
15+
*
16+
* This class attaches the nested sets query behavior to facilitate tree traversal and structure queries on models
17+
* representing multiple tree hierarchies.
18+
*
19+
* Key features:
20+
* - Attaches {@see NestedSetsQueryBehavior} for nested sets operations.
21+
* - Designed for use with {@see ExtendableMultipleTree} in test environments.
22+
* - Supports hierarchical queries for models with multiple tree columns.
23+
*
1124
* @template T of ExtendableMultipleTree
1225
*
1326
* @extends ActiveQuery<T>
27+
*
28+
* @copyright Copyright (C) 2023 Terabytesoftw.
29+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
1430
*/
1531
final class ExtendableMultipleTreeQuery extends ActiveQuery
1632
{
17-
/**
18-
* @phpstan-param class-string<T> $modelClass
19-
* @phpstan-param array<string, mixed> $config
20-
*/
21-
public function __construct(string $modelClass, array $config = [])
22-
{
23-
parent::__construct($modelClass, $config);
24-
}
25-
2633
public function behaviors(): array
2734
{
2835
return [

tests/support/model/MultipleTree.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,36 @@
88
use yii2\extensions\nestedsets\NestedSetsBehavior;
99

1010
/**
11+
* Active Record model for testing multiple tree support with nested sets behavior.
12+
*
13+
* This class attaches the {@see NestedSetsBehavior} to enable hierarchical data structure operations with support for
14+
* multiple trees.
15+
*
16+
* The model provides tree structure functionality through the behavior properties and methods, allowing for nested sets
17+
* model implementation on Active Record instances with a tree attribute.
18+
*
19+
* The model is designed for testing property resolution and behavior integration in scenarios involving multiple tree
20+
* columns.
21+
*
22+
* It ensures proper type inference and property access when behaviors are attached to Yii Active Record models with a
23+
* tree context.
24+
*
25+
* Key features:
26+
* - Behavior attachment for hierarchical data structures with multiple tree support.
27+
* - Nested sets model functionality for tree operations.
28+
* - Property resolution testing for behavior integration.
29+
* - Static analysis validation for behavior property access.
30+
* - Table mapping with the `multiple_tree` table.
31+
*
1132
* @phpstan-property int $depth
1233
* @phpstan-property int $id
1334
* @phpstan-property int $lft
1435
* @phpstan-property int $rgt
1536
* @phpstan-property int $tree
1637
* @phpstan-property string $name
38+
*
39+
* @copyright Copyright (C) 2023 Terabytesoftw.
40+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
1741
*/
1842
class MultipleTree extends ActiveRecord
1943
{

tests/support/model/MultipleTreeQuery.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,25 @@
88
use yii2\extensions\nestedsets\NestedSetsQueryBehavior;
99

1010
/**
11+
* Active Query class for {@see MultipleTree} with nested sets query behavior support.
12+
*
13+
* Provides an Active Query implementation tailored for the {@see MultipleTree} model, enabling integration with the
14+
* {@see NestedSetsQueryBehavior} for hierarchical data operations in multiple tree scenarios.
15+
*
16+
* This class attaches the nested sets query behavior to facilitate tree traversal and structure queries on models
17+
* representing multiple tree hierarchies.
18+
*
19+
* Key features:
20+
* - Attaches {@see NestedSetsQueryBehavior} for nested sets operations.
21+
* - Designed for use with {@see MultipleTree} in test environments.
22+
* - Supports hierarchical queries for models with multiple tree columns.
23+
*
1124
* @template T of MultipleTree
1225
*
1326
* @extends ActiveQuery<T>
27+
*
28+
* @copyright Copyright (C) 2023 Terabytesoftw.
29+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
1430
*/
1531
final class MultipleTreeQuery extends ActiveQuery
1632
{

tests/support/model/Tree.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,32 @@
88
use yii2\extensions\nestedsets\NestedSetsBehavior;
99

1010
/**
11+
* Active Record model for testing single tree support with nested sets behavior.
12+
*
13+
* This class attaches the {@see NestedSetsBehavior} to enable hierarchical data structure operations for a single tree.
14+
*
15+
* The model provides tree structure functionality through the behavior properties and methods, allowing for nested sets
16+
* model implementation on Active Record instances.
17+
*
18+
* The model is designed for testing property resolution and behavior integration in scenarios involving a single tree.
19+
*
20+
* It ensures proper type inference and property access when behaviors are attached to Yii Active Record models.
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+
* - Table mapping with the `tree` table.
28+
*
1129
* @phpstan-property int $depth
1230
* @phpstan-property int $id
1331
* @phpstan-property int $lft
1432
* @phpstan-property int $rgt
1533
* @phpstan-property string $name
34+
*
35+
* @copyright Copyright (C) 2023 Terabytesoftw.
36+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
1637
*/
1738
class Tree extends ActiveRecord
1839
{

tests/support/model/TreeQuery.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,26 @@
88
use yii2\extensions\nestedsets\NestedSetsQueryBehavior;
99

1010
/**
11-
* @template T of Tree
11+
* Active Query class for {@see Tree} and {@see TreeWithStrictValidation} with nested sets query behavior support.
12+
*
13+
* Provides an Active Query implementation tailored for the {@see Tree} and {@see TreeWithStrictValidation} models,
14+
* enabling integration with the {@see NestedSetsQueryBehavior} for hierarchical data operations in single tree
15+
* scenarios.
16+
*
17+
* This class attaches the nested sets query behavior to facilitate tree traversal and structure queries on models
18+
* representing single tree hierarchies.
19+
*
20+
* Key features:
21+
* - Attaches {@see NestedSetsQueryBehavior} for nested sets operations.
22+
* - Designed for use with {@see Tree} and {@see TreeWithStrictValidation} in test environments.
23+
* - Supports hierarchical queries for models with single tree columns.
24+
*
25+
* @template T of Tree|TreeWithStrictValidation
1226
*
1327
* @extends ActiveQuery<T>
28+
*
29+
* @copyright Copyright (C) 2023 Terabytesoftw.
30+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
1431
*/
1532
final class TreeQuery extends ActiveQuery
1633
{

tests/support/model/TreeWithStrictValidation.php

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,64 @@
44

55
namespace 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

Comments
 (0)