1 | <?php namespace Arcanedev\LaravelNestedSet\Utilities; |
||
13 | class TreeHelper |
||
14 | { |
||
15 | /* ------------------------------------------------------------------------------------------------ |
||
16 | | Main Functions |
||
17 | | ------------------------------------------------------------------------------------------------ |
||
18 | */ |
||
19 | /** |
||
20 | * @param array $data |
||
21 | * @param array $existing |
||
22 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $model |
||
23 | * @param bool $delete |
||
24 | * |
||
25 | * @return int |
||
26 | */ |
||
27 | 12 | public static function rebuild(array $data, $existing, Nodeable $model, $delete = false) |
|
28 | { |
||
29 | 12 | $dictionary = []; |
|
30 | |||
31 | 12 | self::rebuildDictionary($dictionary, $data, $existing, $model); |
|
32 | |||
33 | 8 | if ( ! empty($existing)) { |
|
34 | 8 | self::cleaningDictionary($dictionary, $existing, $model, $delete); |
|
35 | 6 | } |
|
36 | |||
37 | 8 | return self::fixNodes($dictionary); |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * Fix nodes. |
||
42 | * |
||
43 | * @param array $dictionary |
||
44 | * |
||
45 | * @return int |
||
46 | */ |
||
47 | 12 | public static function fixNodes(array &$dictionary) |
|
62 | |||
63 | /* ------------------------------------------------------------------------------------------------ |
||
64 | | Other Functions |
||
65 | | ------------------------------------------------------------------------------------------------ |
||
66 | */ |
||
67 | /** |
||
68 | * Rebuild the dictionary. |
||
69 | * |
||
70 | * @param array $dictionary |
||
71 | * @param array $data |
||
72 | * @param array $existing |
||
73 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $model |
||
74 | * @param mixed $parentId |
||
75 | * |
||
76 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
77 | */ |
||
78 | 12 | protected static function rebuildDictionary( |
|
79 | array &$dictionary, |
||
80 | array $data, |
||
81 | array &$existing, |
||
82 | Nodeable &$model, |
||
83 | $parentId = null |
||
84 | ) { |
||
85 | 12 | $keyName = $model->getKeyName(); |
|
86 | |||
87 | 12 | foreach ($data as $itemData) { |
|
88 | 12 | $node = self::retrieveNode($existing, $model, $parentId, $itemData, $keyName); |
|
89 | |||
90 | 8 | $node->fill($itemData)->save(); |
|
91 | 8 | $dictionary[$parentId][] = $node; |
|
92 | |||
93 | 8 | if (isset($itemData['children'])) { |
|
94 | 5 | self::rebuildDictionary($dictionary, $itemData['children'], $existing, $model, $node->getKey()); |
|
95 | 3 | } |
|
96 | 6 | } |
|
97 | 8 | } |
|
98 | |||
99 | /** |
||
100 | * @param array $existing |
||
101 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $model |
||
102 | * @param mixed $parentId |
||
103 | * @param array $itemData |
||
104 | * @param mixed $keyName |
||
105 | * |
||
106 | * @return \Arcanedev\LaravelNestedSet\Contracts\Nodeable |
||
107 | */ |
||
108 | 12 | protected static function retrieveNode(array &$existing, Nodeable &$model, $parentId, $itemData, $keyName) |
|
124 | |||
125 | /** |
||
126 | * Reorder nodes. |
||
127 | * |
||
128 | * @param array $dictionary |
||
129 | * @param int $fixed |
||
130 | * @param int|null $parentId |
||
131 | * @param int $cut |
||
132 | * |
||
133 | * @return int |
||
134 | */ |
||
135 | 12 | protected static function reorderNodes( |
|
163 | |||
164 | /** |
||
165 | * Cleaning existing nodes. |
||
166 | * |
||
167 | * @param array $dictionary |
||
168 | * @param array $existing |
||
169 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $model |
||
170 | * @param bool $delete |
||
171 | */ |
||
172 | 8 | private static function cleaningDictionary(&$dictionary, $existing, Nodeable $model, $delete) |
|
187 | } |
||
188 |