Coverage for Doctrine_Data

Back to coverage report

1 <?php
2 /*
3  *  $Id: Data.php 2552 2007-09-19 19:33:00Z Jonathan.Wage $
4  *
5  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16  *
17  * This software consists of voluntary contributions made by many individuals
18  * and is licensed under the LGPL. For more information, see
19  * <http://www.phpdoctrine.org>.
20  */
21
22 /**
23  * Doctrine_Data
24  * 
25  * Base Doctrine_Data class for dumping and loading data to and from fixtures files.
26  * Support formats are based on what formats are available in Doctrine_Parser such as yaml, xml, json, etc.
27  *
28  * @package     Doctrine
29  * @subpackage  Data
30  * @author      Jonathan H. Wage <jwage@mac.com>
31  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
32  * @link        www.phpdoctrine.org
33  * @since       1.0
34  * @version     $Revision: 2552 $
35  */
36 class Doctrine_Data
37 {
38     /**
39      * formats
40      *
41      * array of formats data can be in
42      *
43      * @var string
44      */
45     protected $_formats = array('csv', 'yml', 'xml');
46
47     /**
48      * format
49      * 
50      * the default and current format we are working with
51      *
52      * @var string
53      */
54     protected $_format = 'yml';
55
56     /**
57      * directory
58      *
59      * array of directory/yml paths or single directory/yml file
60      *
61      * @var string
62      */
63     protected $_directory = null;
64
65     /**
66      * models
67      *
68      * specified array of models to use
69      *
70      * @var string
71      */
72     protected $_models = array();
73
74     /**
75      * _exportIndividualFiles
76      *
77      * whether or not to export data to individual files instead of 1
78      *
79      * @var string
80      */
81     protected $_exportIndividualFiles = false;
82
83     /**
84      * setFormat
85      *
86      * Set the current format we are working with
87      * 
88      * @param string $format 
89      * @return void
90      */
91     public function setFormat($format)
92     {
93         $this->_format = $format;
94     }
95
96     /**
97      * getFormat
98      *
99      * Get the current format we are working with
100      * 
101      * @return void
102      */
103     public function getFormat()
104     {
105         return $this->_format;
106     }
107
108     /**
109      * getFormats 
110      *
111      * Get array of available formats
112      * 
113      * @return void
114      */
115     public function getFormats()
116     {
117         return $this->_formats;
118     }
119
120     /**
121      * setDirectory
122      *
123      * Set the array/string of directories or yml file paths
124      * 
125      * @return void
126      */
127     public function setDirectory($directory)
128     {
129         $this->_directory = $directory;
130     }
131
132     /**
133      * getDirectory
134      *
135      * Get directory for dumping/loading data from and to
136      * 
137      * @return void
138      */
139     public function getDirectory()
140     {
141         return $this->_directory;
142     }
143
144     /**
145      * setModels
146      *
147      * Set the array of specified models to work with
148      * 
149      * @param string $models 
150      * @return void
151      */
152     public function setModels($models)
153     {
154         $this->_models = $models;
155     }
156
157     /**
158      * getModels
159      *
160      * Get the array of specified models to work with
161      *
162      * @return void
163      */
164     public function getModels()
165     {
166         return $this->_models;
167     }
168
169     /**
170      * _exportIndividualFiles 
171      *
172      * Set/Get whether or not to export individual files
173      * 
174      * @return bool $_exportIndividualFiles
175      */
176     public function exportIndividualFiles($bool = null)
177     {
178         if ($bool !== null) {
179             $this->_exportIndividualFiles = $bool;
180         }
181         
182         return $this->_exportIndividualFiles;
183     }
184
185     /**
186      * exportData
187      *
188      * Interface for exporting data to fixtures files from Doctrine models
189      *
190      * @param string $directory 
191      * @param string $format 
192      * @param string $models 
193      * @param string $_exportIndividualFiles 
194      * @return void
195      */
196     public function exportData($directory, $format = 'yml', $models = array(), $_exportIndividualFiles = false)
197     {
198         $export = new Doctrine_Data_Export($directory);
199         $export->setFormat($format);
200         $export->setModels($models);
201         $export->exportIndividualFiles($_exportIndividualFiles);
202         
203         return $export->doExport();
204     }
205
206     /**
207      * importData
208      *
209      * Interface for importing data from fixture files to Doctrine models
210      *
211      * @param string $directory 
212      * @param string $format 
213      * @param string $models 
214      * @return void
215      */
216     public function importData($directory, $format = 'yml', $models = array())
217     {
218         $import = new Doctrine_Data_Import($directory);
219         $import->setFormat($format);
220         $import->setModels($models);
221         
222         return $import->doImport();
223     }
224
225     /**
226      * importDummyData
227      *
228      * Interface for importing dummy data to models
229      * 
230      * @param string $num 
231      * @param string $models 
232      * @return void
233      */
234     public function importDummyData($num = 3, $models = array())
235     {
236         $import = new Doctrine_Data_Import();
237         $import->setModels($models);
238         
239         return $import->doImportDummyData($num);
240     }
241
242     /**
243      * isRelation
244      *
245      * Check if a fieldName on a Doctrine_Record is a relation, if it is we return that relationData
246      * 
247      * @param string $Doctrine_Record 
248      * @param string $fieldName 
249      * @return void
250      */
251     public function isRelation(Doctrine_Record $record, $fieldName)
252     {
253         $relations = $record->getTable()->getRelations();
254         
255         foreach ($relations as $relation) {
256             $relationData = $relation->toArray();
257             
258             if ($relationData['local'] === $fieldName) {
259                 return $relationData;
260             }
261             
262         }
263         
264         return false;
265     }
266
267     /**
268      * purge
269      * 
270      * Purge all data for loaded models or for the passed array of Doctrine_Records
271      *
272      * @param string $models 
273      * @return void
274      */
275     public function purge($models = null)
276     {
277         $models = Doctrine::filterInvalidModels($models);
278
279         foreach ($models as $model)
280         {
281             $model = new $model();
282
283             $model->getTable()->createQuery()->delete($model)->execute();
284         }
285     }
286 }