Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
68 / 68
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
GnomePlanner
100.00% covered (success)
100.00%
68 / 68
100.00% covered (success)
100.00%
9 / 9
39
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 canRead
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 load
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
7
 readNodeResources
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 readNodeResource
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 readNodeTasks
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 readNodeTask
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
10
 readNodeAllocations
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 readNodeAllocation
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * This file is part of PHPProject - A pure PHP library for reading and writing
5 * presentations documents.
6 *
7 * PHPProject is free software distributed under the terms of the GNU Lesser
8 * General Public License version 3 as published by the Free Software Foundation.
9 *
10 * For the full copyright and license information, please read the LICENSE
11 * file that was distributed with this source code. For the full list of
12 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13 *
14 * @link        https://github.com/PHPOffice/PHPProject
15 * @copyright   2009-2014 PHPProject contributors
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19declare(strict_types=1);
20
21namespace PhpOffice\PhpProject\Reader;
22
23use PhpOffice\PhpProject\PhpProject;
24use PhpOffice\PhpProject\Resource;
25use PhpOffice\PhpProject\Shared\XMLReader;
26use PhpOffice\PhpProject\Task;
27
28/**
29 * GnomePlanner
30 */
31class GnomePlanner implements ReaderInterface
32{
33    /**
34     * PHPProject object
35     *
36     * @var PhpProject
37     */
38    protected $phpProject;
39
40    /**
41     * Create a new GnomePlanner
42     */
43    public function __construct()
44    {
45        $this->phpProject = new PhpProject();
46    }
47
48    /**
49     *
50     * @param string $pFilename
51     * @return bool
52     */
53    public function canRead(string $pFilename): bool
54    {
55        if (file_exists($pFilename) && is_readable($pFilename)) {
56            return true;
57        }
58        return false;
59    }
60
61    /**
62     *
63     * @param string $pFilename
64     * @throws \Exception
65     * @return PhpProject
66     */
67    public function load(string $pFilename): PhpProject
68    {
69        if (!$this->canRead($pFilename)) {
70            throw new \Exception('The file is not accessible.');
71        }
72        $content = file_get_contents($pFilename);
73        $xml = new XMLReader();
74        $xml->getDomFromString($content);
75
76        $nodes = $xml->getElements('*');
77        if ($nodes->length > 0) {
78            foreach ($nodes as $node) {
79                switch ($node->nodeName) {
80                    case 'resources':
81                        $this->readNodeResources($xml, $node);
82                        break;
83                    case 'tasks':
84                        $this->readNodeTasks($xml, $node);
85                        break;
86                    case 'allocations':
87                        $this->readNodeAllocations($xml, $node);
88                        break;
89                }
90            }
91        }
92
93        return $this->phpProject;
94    }
95
96    /**
97     * Node "Resources"
98     * @param XMLReader $xml
99     * @param \DOMElement $domNode
100     */
101    protected function readNodeResources(XMLReader $xml, \DOMElement $domNode): void
102    {
103        $nodes = $xml->getElements('*', $domNode);
104        if ($nodes->length > 0) {
105            foreach ($nodes as $node) {
106                if ($node->nodeName == 'resource') {
107                    $resource = $this->phpProject->createResource();
108                    $this->readNodeResource($node, $resource);
109                }
110            }
111        }
112    }
113
114    /**
115     * Node "Resource"
116     * @param \DOMElement $domNode
117     * @param Resource $resource
118     */
119    protected function readNodeResource(\DOMElement $domNode, Resource $resource): void
120    {
121        // Attributes
122        if ($domNode->hasAttribute('id')) {
123            $resource->setIndex($domNode->getAttribute('id'));
124        }
125        if ($domNode->hasAttribute('name')) {
126            $resource->setTitle($domNode->getAttribute('name'));
127        }
128    }
129
130    /**
131     * Node "Tasks"
132     * @param XMLReader $xml
133     * @param \DOMElement $domNode
134     */
135    protected function readNodeTasks(XMLReader $xml, \DOMElement $domNode): void
136    {
137        $nodes = $xml->getElements('*', $domNode);
138        if ($nodes->length > 0) {
139            foreach ($nodes as $node) {
140                if ($node->nodeName == 'task') {
141                    $task = $this->phpProject->createTask();
142                    $this->readNodeTask($xml, $node, $task);
143                }
144            }
145        }
146    }
147
148    /**
149     * Node "Task"
150     * @param XMLReader $xml
151     * @param \DOMElement $domNode
152     */
153    protected function readNodeTask(XMLReader $xml, \DOMElement $domNode, Task $task): void
154    {
155        // Attributes
156        if ($domNode->hasAttribute('id')) {
157            $task->setIndex($domNode->getAttribute('id'));
158        }
159        if ($domNode->hasAttribute('name')) {
160            $task->setName($domNode->getAttribute('name'));
161        }
162        if ($domNode->hasAttribute('start')) {
163            $task->setStartDate($domNode->getAttribute('start'));
164        }
165        if ($domNode->hasAttribute('end')) {
166            $task->setEndDate($domNode->getAttribute('end'));
167        }
168        if ($domNode->hasAttribute('work')) {
169            $task->setDuration($domNode->getAttribute('work'));
170        }
171        if ($domNode->hasAttribute('percent-complete')) {
172            $task->setProgress($domNode->getAttribute('percent-complete'));
173        }
174
175        // SubNodes
176        $nodes = $xml->getElements('*', $domNode);
177        if ($nodes->length > 0) {
178            foreach ($nodes as $node) {
179                if ($node->nodeName == 'task') {
180                    $taskChild = $task->createTask();
181                    $this->readNodeTask($xml, $node, $taskChild);
182                }
183            }
184        }
185    }
186
187    /**
188     * Node "Allocations"
189     * @param XMLReader $xml
190     * @param \DOMElement $domNode
191     */
192    protected function readNodeAllocations(XMLReader $xml, \DOMElement $domNode): void
193    {
194        $nodes = $xml->getElements('*', $domNode);
195        if ($nodes->length > 0) {
196            foreach ($nodes as $node) {
197                if ($node->nodeName == 'allocation') {
198                    $this->readNodeAllocation($node);
199                }
200            }
201        }
202    }
203
204    /**
205     * Node "Allocation"
206     * @param \DOMElement $domNode
207     */
208    protected function readNodeAllocation(\DOMElement $domNode): void
209    {
210        // Attributes
211        $idTask = $domNode->getAttribute('task-id');
212        $idResource = $domNode->getAttribute('resource-id');
213
214        $resource = $this->phpProject->getResourceFromIndex($idResource);
215        $task = $this->phpProject->getTaskFromIndex($idTask);
216
217        if ($resource instanceof Resource && $task instanceof Task) {
218            $task->addResource($resource);
219        }
220    }
221}
222