-
Notifications
You must be signed in to change notification settings - Fork 1
/
SnapshottableBehaviorObjectBuilderModifier.php
100 lines (86 loc) · 3.24 KB
/
SnapshottableBehaviorObjectBuilderModifier.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace prgTW\SnapshottableBehavior;
use Propel\Generator\Builder\Om\ObjectBuilder;
use Propel\Generator\Model\Table;
class SnapshottableBehaviorObjectBuilderModifier
{
/** @var SnapshottableBehavior */
protected $behavior;
/** @var Table */
private $table;
/**
* @param SnapshottableBehavior $behavior
*/
public function __construct(SnapshottableBehavior $behavior)
{
$this->behavior = $behavior;
$this->table = $behavior->getTable();
}
/**
* @param ObjectBuilder $builder
*
* @return string
*/
public function objectAttributes(ObjectBuilder $builder)
{
if (!$this->behavior->hasSnapshotClass())
{
$snapshotTable = $this->behavior->getSnapshotTable();
$stubQueryBuilder = $builder->getNewStubQueryBuilder($snapshotTable);
$builder->declareClassFromBuilder($stubQueryBuilder);
}
$script = '';
return $script;
}
/**
* @param ObjectBuilder $builder
*
* @return string
*/
public function objectMethods(ObjectBuilder $builder)
{
if ($this->behavior->hasSnapshotClass())
{
$snapshotClass = $this->behavior->getParameter(SnapshottableBehavior::PARAMETER_SNAPSHOT_CLASS);
$builder->declareClass($snapshotClass);
}
else
{
$snapshotTable = $this->behavior->getSnapshotTable();
$stubObjectBuilder = $builder->getNewStubObjectBuilder($snapshotTable);
$builder->declareClassFromBuilder($stubObjectBuilder);
}
$script = '';
$script .= $this->addSnapshot($builder);
return $script;
}
/**
* @param ObjectBuilder $builder
*
* @return string
*/
public function addSnapshot(ObjectBuilder $builder)
{
$referenceColumnName = $this->behavior->getParameter(SnapshottableBehavior::PARAMETER_REFERENCE_COLUMN);
$snapshotTable = $this->behavior->getSnapshotTable();
$uniqueColumns = array_column($this->behavior->getUniqueColumns(), 'name', 'name');
$uniqueColumns = array_map(
function ($columnName) {
return $this->behavior->getSnapshotTable()->getColumn($columnName)->getPhpName();
},
$uniqueColumns
);
$referenceColumn = $this->behavior->getSnapshotTable()->getColumn($referenceColumnName);
$vars = [
'primaryKeyColumnPhpName' => $this->behavior->getTable()->getFirstPrimaryKeyColumn()->getPhpName(),
'snapshotTablePhpName' => $this->behavior->getSnapshotTablePhpName($builder),
'referenceColumnPhpName' => $referenceColumn->getPhpName(),
'primaryKeyColumn' => $this->behavior->getTable()->getFirstPrimaryKeyColumn(),
'snapshotAtColumn' => $this->behavior->getSnapshotAtColumn(),
'hasSnapshotClass' => $this->behavior->hasSnapshotClass(),
'queryClassName' => $builder->getClassNameFromBuilder($builder->getNewStubQueryBuilder($snapshotTable)),
'uniqueColumns' => $uniqueColumns,
];
return $this->behavior->renderTemplate('objectSnapshot', $vars);
}
}