Skip to content

Commit bb02983

Browse files
committed
Refactor tableMapFilter.
1 parent ac5260a commit bb02983

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/EncryptionBehavior.php

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,15 @@ public function tableMapFilter(&$script) {
4646

4747
public function objectFilter(&$script) {
4848
$table = $this->getTable();
49-
$aggregateColumn = $table->getColumn($this->getParameter('column_name'));
50-
$columnPhpName = $aggregateColumn->getPhpName();
5149

52-
// Modify the setter to include encryption
53-
$setterLocation = strpos($script, "set$columnPhpName");
54-
55-
$start = strpos($script, "(", $setterLocation) + 1;
56-
$length = strpos($script, ")", $setterLocation) - $start;
57-
$variableName = substr($script, $start, $length);
58-
59-
$insertionStart = strpos($script, "{", $setterLocation) + 1;
60-
$script = substr_replace($script, $this->encryptVariable($variableName), $insertionStart, 0);
61-
62-
// Modify the getter to include decryption
63-
$getterLocation = strpos($script, "get$columnPhpName");
50+
foreach ($this->getEncryptedColumnNames() as $columnName) {
51+
$aggregateColumn = $table->getColumn($columnName);
52+
$columnPhpName = $aggregateColumn->getPhpName();
6453

65-
$start = strpos($script, "return", $getterLocation) + 7;
66-
$length = strpos($script, ";", $getterLocation) - $start;
67-
$variableName = substr($script, $start, $length);
54+
$this->modifySetterWithEncryption($script, $columnPhpName);
55+
$this->modifyGetterWithDecryption($script, $columnPhpName);
6856

69-
$insertionStart = strpos($script, "return", $getterLocation);
70-
$insertionLength = strpos($script, ";", $insertionStart) - $insertionStart + 1;
71-
$script = substr_replace($script, $this->decryptVariable($variableName), $insertionStart, $insertionLength);
57+
}
7258
}
7359

7460
protected function getEncryptedColumnNames() {
@@ -86,6 +72,17 @@ protected function makeEncryptedColumnsDeclaration($columnPhpName) {
8672
EOT;
8773
}
8874

75+
protected function modifySetterWithEncryption(&$script, $columnPhpName) {
76+
$setterLocation = strpos($script, "set$columnPhpName");
77+
78+
$start = strpos($script, "(", $setterLocation) + 1;
79+
$length = strpos($script, ")", $setterLocation) - $start;
80+
$variableName = substr($script, $start, $length);
81+
82+
$insertionStart = strpos($script, "{", $setterLocation) + 1;
83+
$script = substr_replace($script, $this->encryptVariable($variableName), $insertionStart, 0);
84+
}
85+
8986
protected function encryptVariable($variableName) {
9087
return <<<EOT
9188
@@ -95,6 +92,18 @@ protected function encryptVariable($variableName) {
9592
EOT;
9693
}
9794

95+
protected function modifyGetterWithDecryption(&$script, $columnPhpName) {
96+
$getterLocation = strpos($script, "get$columnPhpName");
97+
98+
$start = strpos($script, "return", $getterLocation) + 7;
99+
$length = strpos($script, ";", $getterLocation) - $start;
100+
$variableName = substr($script, $start, $length);
101+
102+
$insertionStart = strpos($script, "return", $getterLocation);
103+
$insertionLength = strpos($script, ";", $insertionStart) - $insertionStart + 1;
104+
$script = substr_replace($script, $this->decryptVariable($variableName), $insertionStart, $insertionLength);
105+
}
106+
98107
protected function decryptVariable($variableName) {
99108
return <<<EOT
100109
// Decrypt the variable, per \UWDOEM\Encryption\EncryptionBehavior.

0 commit comments

Comments
 (0)