use standard SplObserver
[m6w6/pq-gateway] / lib / pq / Gateway / Table / OptimisticLock.php
1 <?php
2
3 namespace pq\Gateway\Table;
4
5 use \pq\Gateway\Row;
6
7 /**
8 * An optimistic row lock implementation using a versioning column
9 */
10 class OptimisticLock implements \SplObserver
11 {
12 /**
13 * The name of the versioning column
14 * @var string
15 */
16 protected $column;
17
18 /**
19 * @param string $column
20 */
21 function __construct($column = "version") {
22 $this->column = $column;
23 }
24
25 /**
26 * @param \pq\Gateway\Table $table
27 * @param \pq\Gateway\Row $row
28 * @param string $event create/update/delete
29 * @param array $where reference to the criteria
30 */
31 function update(\SplSubject $table, Row $row = null, $event = null, array &$where = null) {
32 if ($event === "update") {
33 $where["{$this->column}="] = $row->getData()[$this->column];
34 $row->{$this->column}->mod(+1);
35 }
36 }
37 }