|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Fwk |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2011-2014, Julien Ballestracci <[email protected]>. |
|
6
|
|
|
* All rights reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
12
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
13
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
14
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
15
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
16
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
17
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
18
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
19
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
20
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
21
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
22
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
23
|
|
|
* |
|
24
|
|
|
* PHP Version 5.3 |
|
25
|
|
|
* |
|
26
|
|
|
* @category Database |
|
27
|
|
|
* @package Fwk |
|
28
|
|
|
* @subpackage Db |
|
29
|
|
|
* @author Julien Ballestracci <[email protected]> |
|
30
|
|
|
* @copyright 2011-2014 Julien Ballestracci <[email protected]> |
|
31
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
32
|
|
|
* @link http://www.nitronet.org/fwk |
|
33
|
|
|
*/ |
|
34
|
|
|
namespace Fwk\Db\Listeners; |
|
35
|
|
|
|
|
36
|
|
|
use Fwk\Db\Accessor; |
|
37
|
|
|
use Fwk\Db\Events\BeforeSaveEvent; |
|
38
|
|
|
use Fwk\Db\Events\BeforeUpdateEvent; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Timestampable |
|
42
|
|
|
* |
|
43
|
|
|
* This listeners helps an Entity to trace creation and update times. |
|
44
|
|
|
* |
|
45
|
|
|
* @category Listeners |
|
46
|
|
|
* @package Fwk\Db |
|
47
|
|
|
* @author Julien Ballestracci <[email protected]> |
|
48
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
49
|
|
|
* @link http://www.nitronet.org/fwk |
|
50
|
|
|
*/ |
|
51
|
|
|
class Timestampable |
|
52
|
|
|
{ |
|
53
|
|
|
/** |
|
54
|
|
|
* Name of the creation column |
|
55
|
|
|
* |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $creationColumn = 'created_at'; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Name of the update column |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $updateColumn = 'updated_at'; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Date format |
|
69
|
|
|
* |
|
70
|
|
|
* @var string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $dateFormat = 'Y-m-d H:i:s'; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Constructor |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $creationColumn Name of the created_at column |
|
78
|
|
|
* @param string $updateColumn Name of the updated_at column |
|
79
|
|
|
* @param string $dateFormat Date format |
|
80
|
|
|
* |
|
81
|
|
|
* @return |
|
82
|
|
|
*/ |
|
83
|
|
|
public function __construct($creationColumn = 'created_at', |
|
84
|
|
|
$updateColumn = 'updated_at', $dateFormat = 'Y-m-d H:i:s' |
|
85
|
|
|
) { |
|
86
|
|
|
$this->creationColumn = $creationColumn; |
|
87
|
|
|
$this->updateColumn = $updateColumn; |
|
88
|
|
|
$this->dateFormat = $dateFormat; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Listener triggered when a new entity is saved |
|
93
|
|
|
* |
|
94
|
|
|
* @param BeforeSaveEvent $event The BeforeSave event |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
public function onBeforeSave(BeforeSaveEvent $event) |
|
99
|
|
|
{ |
|
100
|
|
|
$date = new \DateTime(); |
|
101
|
|
|
Accessor::factory( |
|
102
|
|
|
$event->getEntity() |
|
103
|
|
|
)->set($this->creationColumn, $date->format($this->dateFormat)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Listener triggered when an existing entity is updated |
|
108
|
|
|
* |
|
109
|
|
|
* @param BeforeUpdateEvent $event The BeforeUpdate event |
|
110
|
|
|
* |
|
111
|
|
|
* @return void |
|
112
|
|
|
*/ |
|
113
|
|
|
public function onBeforeUpdate(BeforeUpdateEvent $event) |
|
114
|
|
|
{ |
|
115
|
|
|
$date = new \DateTime(); |
|
116
|
|
|
Accessor::factory( |
|
117
|
|
|
$event->getEntity() |
|
118
|
|
|
)->set($this->updateColumn, $date->format($this->dateFormat)); |
|
119
|
|
|
} |
|
120
|
|
|
} |