Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
16 / 16
CRAP
100.00% covered (success)
100.00%
1 / 1
PhpProject
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
16 / 16
26
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getProperties
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setProperties
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getInformations
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setInformations
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 createResource
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getResourceCount
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAllResources
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActiveResource
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getResourceFromIndex
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 createTask
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getTaskCount
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAllTasks
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActiveTask
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getTaskFromIndex
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
6
 removeTaskByIndex
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
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;
22
23/**
24 * PHPProject
25 *
26 * @category   PHPProject
27 * @package    PHPProject
28 * @copyright  Copyright (c) 2006 - 2012 PHPProject (https://github.com/PHPOffice/PHPProject)
29 */
30class PhpProject
31{
32    /**
33     * Document properties
34     *
35     * @var DocumentProperties
36     */
37    private $properties;
38
39    /**
40     * Document informations
41     *
42     * @var DocumentInformations
43     */
44    private $informations;
45    
46    /**
47     * Collection of task objects
48     *
49     * @var Task[]
50     */
51    private $taskCollection = array();
52    
53    /**
54     * Collection of resource objects
55     *
56     * @var Resource[]
57     */
58    private $resourceCollection = array();
59
60    /**
61     * Create a new PHPProject
62     */
63    public function __construct()
64    {
65        // Create document properties
66        $this->properties = new DocumentProperties();
67        // Create document informations
68        $this->informations = new DocumentInformations();
69    }
70
71    //===============================================
72    // Document Properties
73    //===============================================
74    /**
75     * Get properties
76     *
77     * @return DocumentProperties
78     */
79    public function getProperties(): DocumentProperties
80    {
81        return $this->properties;
82    }
83
84    /**
85     * Set properties
86     *
87     * @param DocumentProperties $pValue
88     */
89    public function setProperties(DocumentProperties $pValue): self
90    {
91        $this->properties = $pValue;
92        return $this;
93    }
94
95    //===============================================
96    // Document Informations
97    //===============================================
98    /**
99     * Get informations
100     * 
101     * @return DocumentInformations
102     */
103    public function getInformations(): DocumentInformations
104    {
105        return $this->informations;
106    }
107    
108    /**
109     * Set informations
110     *
111     * @param DocumentInformations $pValue
112     */
113    public function setInformations(DocumentInformations $pValue): self
114    {
115        $this->informations = $pValue;
116        return $this;
117    }
118    
119    //===============================================
120    // Resources
121    //===============================================
122    /**
123     * Create a resource
124     *
125     * @return Resource
126     * @throws \Exception
127     */
128    public function createResource(): Resource
129    {
130        $newRessource = new Resource();
131        $this->resourceCollection[] = $newRessource;
132        return $newRessource;
133    }
134
135    /**
136     * Get resource count
137     *
138     * @return int
139     */
140    public function getResourceCount(): int
141    {
142        return count($this->resourceCollection);
143    }
144    
145    /**
146     * Get all resources
147     *
148     * @return \PhpOffice\PhpProject\Resource[]
149     */
150    public function getAllResources(): array
151    {
152        return $this->resourceCollection;
153    }
154    
155    /**
156     * Get active resource
157     *
158     * @return Resource|null
159     */
160    public function getActiveResource(): ?Resource
161    {
162        if (!empty($this->resourceCollection)) {
163            return end($this->resourceCollection);
164        }
165        return null;
166    }
167    
168    /**
169     * Get resource from index
170     *
171     * @param int|string $pIndex
172     * @return Resource|null
173     */
174    public function getResourceFromIndex($pIndex): ?Resource
175    {
176        foreach ($this->resourceCollection as $oResource) {
177            if ($oResource->getIndex() == $pIndex) {
178                return $oResource;
179            }
180        }
181        return null;
182    }
183    
184    //===============================================
185    // Tasks
186    //===============================================
187    /**
188     * Create a task
189     *
190     * @return Task
191     * @throws \Exception
192     */
193    public function createTask(): Task
194    {
195        $newTask = new Task();
196        $this->taskCollection[] = $newTask;
197        return $newTask;
198    }
199    
200    /**
201     * Get task count
202     *
203     * @return int
204     */
205    public function getTaskCount(): int
206    {
207        return count($this->taskCollection);
208    }
209    
210    /**
211     * Get all tasks
212     *
213     * @return Task[]
214     */
215    public function getAllTasks(): array
216    {
217        return $this->taskCollection;
218    }
219    
220    /**
221     * Get active task
222     *
223     * @return Task|null
224     */
225    public function getActiveTask(): ?Task
226    {
227        if (!empty($this->taskCollection)) {
228            return end($this->taskCollection);
229        }
230        return null;
231    }
232    
233    /**
234     * Get task from index
235     *
236     * @param int|string $pIndex
237     * @return Task|null
238     */
239    public function getTaskFromIndex($pIndex, ?Task $oTaskParent = null): ?Task
240    {
241        if (is_null($oTaskParent)) {
242            $arrayTask = $this->taskCollection;
243        } else {
244            $arrayTask = $oTaskParent->getTasks();
245        }
246        foreach ($arrayTask as $oTask) {
247            if ($oTask->getIndex() == $pIndex) {
248                return $oTask;
249            } else {
250                if ($oTask->getTaskCount() > 0) {
251                    $return = $this->getTaskFromIndex($pIndex, $oTask);
252                    if ($return instanceof Task) {
253                        return $return;
254                    }
255                }
256            }
257        }
258        return null;
259    }
260
261    /**
262     * Remove task by index
263     *
264     * @param int $pIndex Active task index
265     * @throws \Exception
266     */
267    public function removeTaskByIndex(int $pIndex = 0): void
268    {
269        if (!isset($this->taskCollection[$pIndex])) {
270            throw new \Exception('Task index is out of bounds.');
271        } else {
272            array_splice($this->taskCollection, $pIndex, 1);
273        }
274    }
275}