Coverage for Doctrine_AuditLog

Back to coverage report

1 <?php
2 /*
3  *  $Id$
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 Doctrine::autoload('Doctrine_Record_Generator');
22 /**
23  * Doctrine_AuditLog
24  *
25  * @package     Doctrine
26  * @subpackage  AuditLog
27  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28  * @link        www.phpdoctrine.org
29  * @since       1.0
30  * @version     $Revision$
31  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
32  */
33 class Doctrine_AuditLog extends Doctrine_Record_Generator
34 {
35     protected $_options = array(
36                             'className'     => '%CLASS%Version',
37                             'versionColumn' => 'version',
38                             'generateFiles' => false,
39                             'table'         => false,
40                             'pluginTable'   => false,
41                             'children'      => array(),
42                             );
43
44     /**
45      * Create a new auditlog_ 
46      * 
47      * @param array $options An array of options
48      * @return void
49      */
50     public function __construct(array $options = array())
51     {
52         $this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options);
53     }
54
55     /**
56      * Get the version 
57      * 
58      * @param Doctrine_Record $record 
59      * @param mixed $version 
60      * @return array An array with version information
61      */
62     public function getVersion(Doctrine_Record $record, $version)
63     {           
64         $className = $this->_options['className'];
65
66         $q = new Doctrine_Query();
67
68         $values = array();
69         foreach ((array) $this->_options['table']->getIdentifier() as $id) {
70             $conditions[] = $className . '.' . $id . ' = ?';
71             $values[] = $record->get($id);
72         }
73         $where = implode(' AND ', $conditions) . ' AND ' . $className . '.' . $this->_options['versionColumn'] . ' = ?';
74         
75         $values[] = $version;
76
77         $q->from($className)
78           ->where($where);
79
80         return $q->execute($values, Doctrine::HYDRATE_ARRAY);
81     }
82
83     /**
84      * buildDefinition for a table 
85      * 
86      * @param Doctrine_Table $table 
87      * @return boolean true on success otherwise false.
88      */
89     public function setTableDefinition()
90     {
91         $name = $this->_options['table']->getComponentName();
92
93         $columns = $this->_options['table']->getColumns();
94
95         // remove all sequence, autoincrement and unique constraint definitions
96         foreach ($columns as $column => $definition) {
97             unset($columns[$column]['autoincrement']);
98             unset($columns[$column]['sequence']);
99             unset($columns[$column]['unique']);
100         }
101
102         $this->hasColumns($columns);
103
104         // the version column should be part of the primary key definition
105         $this->hasColumn($this->_options['versionColumn'], 'integer', 8, array('primary' => true));
106     }
107 }