Coverage for Doctrine_Template_Listener_Timestampable

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.com>.
20  */
21
22 /**
23  * Doctrine_Template_Listener_Timestampable
24  *
25  * @package     Doctrine
26  * @subpackage  Template
27  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28  * @link        www.phpdoctrine.com
29  * @since       1.0
30  * @version     $Revision$
31  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
32  */
33 class Doctrine_Template_Listener_Timestampable extends Doctrine_Record_Listener
34 {
35     /**
36      * Array of timestampable options
37      *
38      * @var string
39      */
40     protected $_options = array();
41     
42     /**
43      * __construct
44      *
45      * @param string $options 
46      * @return void
47      */
48     public function __construct(array $options)
49     {
50         $this->_options = $options;
51     }
52     
53     /**
54      * preInsert
55      *
56      * @param object $Doctrine_Event 
57      * @return void
58      */
59     public function preInsert(Doctrine_Event $event)
60     {
61         $createdName = $this->_options['created']['name'];
62         $updatedName = $this->_options['updated']['name'];
63         
64         $event->getInvoker()->$createdName = $this->getTimestamp('created');
65         $event->getInvoker()->$updatedName = $this->getTimestamp('updated');
66     }
67     
68     /**
69      * preUpdate
70      *
71      * @param object $Doctrine_Event 
72      * @return void
73      */
74     public function preUpdate(Doctrine_Event $event)
75     {
76         $updatedName = $this->_options['updated']['name'];
77         
78         $event->getInvoker()->$updatedName = $this->getTimestamp('updated');
79     }
80     
81     /**
82      * getTimestamp
83      *
84      * Gets the timestamp in the correct format
85      *
86      * @param string $type 
87      * @return void
88      */
89     public function getTimestamp($type)
90     {
91         $options = $this->_options[$type];
92         
93         if ($options['type'] == 'date') {
94             return date($options['format'], time());
95         } else if ($options['type'] == 'timestamp') {
96             return date($options['format'], time());
97         } else {
98             return time();
99         }
100     }
101 }