Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add possibility to parse xmind2021 files #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 121 additions & 93 deletions lib/Chub/XMindPHP/Package.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php
namespace Chub\XMindPHP;
use PhpOption\Option;

/**
* XmindPackage.php
Expand All @@ -9,9 +8,9 @@
*/
class Package
{
private $file;
/** @var Sheet[] */
private $sheets;
private $file;
private $rootTopic;
private $isJson = false;

private $initialized = false;

Expand All @@ -20,52 +19,55 @@ public function __construct($file)
$this->file = $file;
}

public function getSheet($index)
{
$this->init();

return isset($this->sheets[$index])?$this->sheets[$index]:null;
}

/**
* @return \ArrayIterator
*/
public function getSheetsIterator()
{
$this->init();
public function getRootTopic()
{
$this->init();

return new \ArrayIterator($this->sheets);
}
return $this->rootTopic;
}

private function init()
{
if ($this->initialized) {
return;
}

try {
$xml = file_get_contents('zip://' . $this->file . '#content.xml');
if (empty($xml)) {
throw new RuntimeException('No content.xml found!');
}
} catch (\Exception $e) {
throw new RuntimeException($e->getMessage());
}

$this->parse($xml);
$this->initialized = true;
try {
try {
/** check if file is 2021 xmind -> use json instead of xml */
$data = file_get_contents('zip://' . $this->file . '#content.json');
$this->isJson = true;
} catch (\Exception $e) {
$data = file_get_contents('zip://' . $this->file . '#content.xml');
$this->isJson = false;
}
if (empty($data)) {
throw new RuntimeException('No content.xml found!');
}
} catch (\Exception $e) {
throw new RuntimeException($e->getMessage());
}

$this->parse($data);
}

private function parse($xml)
{
$xml = simplexml_load_string($xml);
/** @var \SimpleXmlElement $sheet */
foreach ($xml->children() as $sheet) {
if ($sheet->getName() == 'sheet') {
$this->parseSheet($sheet);
}
}
}
private function parse($data)
{
if ($this->isJson) {
$data = json_decode($data);
if (!$data[0]->rootTopic) {
throw new RuntimeException('No root topic found');
} else {
$topic = $data[0]->rootTopic;
}
} else {
$data = simplexml_load_string($data);
if (!$data->sheet->topic) {
throw new RuntimeException('No root topic found');
} else {
$topic = $data->sheet->topic;
}
}
$this->rootTopic = new RootTopic();

$this->parseTopic($topic, $this->rootTopic);
}

private function generateException($message)
{
Expand All @@ -74,56 +76,82 @@ private function generateException($message)
};
}

private function parseTopic(\SimpleXMLElement $xml, Topic $topic)
{
$topic->setNote($xml->notes->plain);
$topic->setId((string)$xml->attributes()['id']);
$topic->setTitle((string)$xml->title);
$topic->setLabel((string)$xml->labels->label);

if ($xml->children->count()) {
/** @var \SimpleXMLElement $topics */
foreach ($xml->children->children() as $topics) {
if ($topics->getName() == 'topics') {
$this->parseChildren($topics, $topic);
}
}
}
private function parseTopic($data, Topic $topic)
{

if ($this->isJson) {
$note = isset($data->notes) ? $data->notes->plain : null;
!isset($note) ? : $topic->setNote($note);

$topic->setId((string)$data->id);
$topic->setTitle((string)$data->title);

$label = isset($data->labels) ? (string) $data->labels->label : null;
!isset($note) ? : $topic->setLabel($label);

if (isset($data->children) && count((array)$data->children) > 0) {
/** children can be attached or detached */
foreach ($data->children as $typeOfChildren => $topics) {
foreach ($topics as $t) {
$t->type=$typeOfChildren;
}
$this->parseChildren($topics, $topic);
}
}
} else {
$topic->setNote($data->notes->plain);
$topic->setId((string)$data->attributes()['id']);
$topic->setTitle((string)$data->title);
$topic->setLabel((string)$data->labels->label);

if ($data->children->count()) {
/** @var \SimpleXMLElement $topics */
foreach ($data->children->children() as $topics) {
if ($topics->getName() == 'topics') {
$this->parseChildren($topics, $topic);
}
}
}
}
}

private function parseChildren(\SimpleXMLElement $topics, Topic $topic)
private function parseChildren($topics, Topic $topic)
{
$type = (string)$topics->attributes()['type'];
if (!in_array($type, ['attached', 'detached'])) {
return;
}

$children = [];
/** @var \SimpleXMLElement $child */
foreach ($topics->children() as $child) {
$childTopic = new Topic();
$childTopic->setParent($topic);
$children[] = $childTopic;
$this->parseTopic($child, $childTopic);
}

if ($type == 'attached') {
$topic->setTopics($children);
} else {
/** @var RootTopic $topic */
$topic->setDetachedTopics($children);
}

}

private function parseSheet(\SimpleXMLElement $xml)
{
$rt = new RootTopic();
$sheet = new Sheet();
$sheet->setRootTopic($rt)->setTitle((string)($xml->title))->setId((string)($xml->attributes()['id']));
$topic = Option::fromValue($xml->topic)->getOrCall($this->generateException('No root topic found'));
$this->parseTopic($topic, $rt);

$this->sheets[] = $sheet;
}
if ($this->isJson) {
$children = [];
foreach ($topics as $child) {
$type = $child->type;
if (!in_array($type, ['attached', 'detached'])) {
return;
}
$childTopic = new Topic();
$childTopic->setParent($topic);
$children[] = $childTopic;
$this->parseTopic($child, $childTopic);
}

} else {

$type = (string)$topics->attributes()['type'];
if (!in_array($type, ['attached', 'detached'])) {
return;
}

$children = [];
/** @var \SimpleXMLElement $child */
foreach ($topics->children() as $child) {
$childTopic = new Topic();
$childTopic->setParent($topic);
$children[] = $childTopic;
$this->parseTopic($child, $childTopic);
}
}

if ($type == 'attached') {
$topic->setTopics($children);
} else {
/** @var RootTopic $topic */
$topic->setDetachedTopics($children);
}
}
}