Skip to content

Commit ed0191b

Browse files
committed
Support for encrypting multiple columns.
1 parent ba7cfd0 commit ed0191b

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

src/EncryptionBehavior.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@ public function allowMultiple() {
1919
);
2020

2121
public function tableMapFilter(&$script) {
22-
$table = $this->getTable();
2322

24-
foreach ($this->getEncryptedColumnNames() as $columnName) {
25-
$column = $table->getColumn($columnName);
26-
$columnPhpName = $column->getPhpName();
23+
foreach ($this->getEncryptedColumnPhpNames() as $columnPhpName) {
2724

2825
$encryptedColumnsDeclarationLocation = strpos($script, "ENCRYPTED_COLUMNS");
2926

@@ -45,26 +42,24 @@ public function tableMapFilter(&$script) {
4542
}
4643

4744
public function objectFilter(&$script) {
48-
$table = $this->getTable();
49-
50-
foreach ($this->getEncryptedColumnNames() as $columnName) {
51-
$aggregateColumn = $table->getColumn($columnName);
52-
$columnPhpName = $aggregateColumn->getPhpName();
5345

46+
foreach ($this->getEncryptedColumnPhpNames() as $columnPhpName) {
5447
$this->modifySetterWithEncryption($script, $columnPhpName);
5548
$this->modifyGetterWithDecryption($script, $columnPhpName);
56-
5749
}
5850
}
5951

60-
protected function getEncryptedColumnNames() {
61-
$encryptedColumnNames = [];
62-
foreach ($this->getParameters() as $key => $parameter) {
63-
if (strpos($key, "column_name") !== false) {
64-
$encryptedColumnNames[] = $parameter;
52+
protected function getEncryptedColumnPhpNames() {
53+
$table = $this->getTable();
54+
55+
$encryptedColumnPhpNames = [];
56+
foreach ($this->getParameters() as $key => $columnName) {
57+
if (strpos($key, "column_name") !== false && $columnName) {
58+
$column = $table->getColumn($columnName);
59+
$encryptedColumnPhpNames[] = $column->getPhpName();
6560
}
6661
}
67-
return $encryptedColumnNames;
62+
return $encryptedColumnPhpNames;
6863
}
6964

7065
protected function makeEncryptedColumnsDeclaration($columnPhpName) {

tests/BehaviorTest.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,24 @@ public function getTable() {
149149

150150
class BehaviorTest extends PHPUnit_Framework_TestCase {
151151

152+
protected function normalizeWhitespace($string) {
153+
$string = trim($string);
154+
$string = str_replace("\r", "", $string);
155+
156+
return $string;
157+
}
158+
152159
public function testObjectFilter() {
153160
global $objectFilterInput, $objectFilterExpected;
154161

155162
$behavior = new MockEncryptionBehavior();
156163

157164
$behavior->objectFilter($objectFilterInput);
158165

159-
$this->assertEquals($objectFilterExpected, $objectFilterInput);
166+
$this->assertEquals(
167+
$this->normalizeWhitespace($objectFilterExpected),
168+
$this->normalizeWhitespace($objectFilterInput)
169+
);
160170
}
161171

162172
public function testMapFilter() {
@@ -166,11 +176,17 @@ public function testMapFilter() {
166176

167177
// Run table map filter once, and an encrypted columns declaration is created
168178
$behavior->tableMapFilter($mapFilterInput);
169-
$this->assertEquals(trim($mapFilterExpected), trim($mapFilterInput));
179+
$this->assertEquals(
180+
$this->normalizeWhitespace($mapFilterExpected),
181+
$this->normalizeWhitespace($mapFilterInput)
182+
);
170183

171184
// Run it twice, and the new column name is inserted beside the old
172185
$behavior->tableMapFilter($mapFilterInput);
173-
$this->assertEquals(trim($mapFilterExpectedSecond), trim($mapFilterInput));
186+
$this->assertEquals(
187+
$this->normalizeWhitespace($mapFilterExpectedSecond),
188+
$this->normalizeWhitespace($mapFilterInput)
189+
);
174190
}
175191

176192
}

0 commit comments

Comments
 (0)