-
Notifications
You must be signed in to change notification settings - Fork 1
/
BranchList.php
executable file
·156 lines (127 loc) · 4.99 KB
/
BranchList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace Overblog\MediaWiki;
/**
* File Branch
*
* @package Content
* @subpackage Article content
* @version 1.0
* @author Yannick Le Guédart
*/
use Overblog\MediaWiki\Branch;
use Overblog\MediaWiki\BranchOrderedList;
use Overblog\MediaWiki\BranchUnorderedList;
/**
* Class Branch
*
* This class handles content of text section objects.
*
* @package Content
* @subpackage Article content
* @author Yannick Le Guédart
*/
class BranchList extends Branch
{
/**
* Class constuctor
*
* Nested lists are handled quite curiously in MediaWiki in that they are
* in no way nested, and that the type of list in the
*
* @param mixed $data json StdClass, json string or HTML string
*/
public function __construct($data)
{
if (is_object($data))
{
if (isset($data->children) and is_array($data->children))
{
$previousType = null; // Previous top-level list type
$currentType = null; // Current top-level list type
$currentList = null; // Current top-level list
$previousLevel = null; // previous element sub-level
$currentSubList = null; // Current sub list
foreach ($data->children as $c)
{
// Détermination du type de la première liste
if(!isset($c->attributes) or !isset($c->attributes->styles))
{
$c->attributes = new \StdClass();
$c->attributes->styles = array();
$c->attributes->styles[] = 'bullet';
}
$currentType = $c->attributes->styles[0];
// Si la nouvelle liste est d'un type différent, on change
if ($previousType !== $currentType)
{
if (! is_null($currentList))
{
$class =
Node::getClassFromNode($currentList->type);
$this->_nodes[] = new $class($currentList);
}
$currentList =
json_decode(
'{"type":"' .
(
($currentType === 'number')
?
'listOrdered'
:
'listUnordered'
) .
'","children":[]}'
);
$previousType = $currentType;
}
if (count($c->attributes->styles) > 1)
{
// ce n'est pas le même niveau que le précédent.
// On crée donc un objet de liste dans lequel on
// insère le listItem actuel en supprimant un niveau
if ($previousLevel !== $c->attributes->styles)
{
$currentSubList =
json_decode(
'{"type":"listItem",' .
'"children":[{"type":"list"}]}'
);
$previousLevel = $c->attributes->styles;
array_pop($c->attributes->styles);
$currentSubList->children[0]->children = array(clone($c));
}
// C'est le même niveau que le précédent, on ne fait
// que rajouter le listItem à la liste
else
{
array_pop($c->attributes->styles);
$currentSubList->children[0]->children[] = clone($c);
}
}
else // element de niveau top-level
{
if (! is_null($currentSubList))
{
$currentList->children[] = $currentSubList;
}
$currentList->children[] = clone($c);
$currentSubList = null;
}
}
if (! is_null($currentSubList))
{
$currentList->children[] = $currentSubList;
}
if (! is_null($currentList))
{
$class = Node::getClassFromNode($currentList->type);
$this->_nodes[] = new $class($currentList);
}
}
if (isset($data->attributes))
{
$this->_attributes = $data->attributes;
}
}
}
}