webnet-fr /
database-anonymizer
| 1 | <?php |
||
| 2 | |||
| 3 | namespace WebnetFr\DatabaseAnonymizer; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Target table to anonymize. |
||
| 7 | * Contains the primary key's name and an array of target fields to anonymize. |
||
| 8 | * |
||
| 9 | * @author Vlad Riabchenko <[email protected]> |
||
| 10 | */ |
||
| 11 | class TargetTable |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Name of the table. |
||
| 15 | * |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | private $name; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Primary key field names. |
||
| 22 | * |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | private $primaryKey; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Array of target fields to anonymize. |
||
| 29 | * |
||
| 30 | * @var TargetField[] |
||
| 31 | */ |
||
| 32 | private $targetFields; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Truncate a table. |
||
| 36 | * |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | private $truncate; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param string $name |
||
| 43 | * @param array $primaryKey |
||
| 44 | * @param TargetField[] $targetFields |
||
| 45 | * @param bool $truncate |
||
| 46 | */ |
||
| 47 | public function __construct(string $name, array $primaryKey, array $targetFields, bool $truncate) |
||
| 48 | { |
||
| 49 | if ($truncate && $targetFields) { |
||
|
0 ignored issues
–
show
|
|||
| 50 | throw new \InvalidArgumentException(sprintf( |
||
| 51 | 'Invalid configuration of %s. Table can be either anonymized or truncated.', TargetTable::class |
||
| 52 | )); |
||
| 53 | } |
||
| 54 | |||
| 55 | $this->name = $name; |
||
| 56 | $this->primaryKey = $primaryKey; |
||
| 57 | $this->targetFields = $targetFields; |
||
| 58 | $this->truncate = $truncate; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get the name of this table. |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function getName(): string |
||
| 67 | { |
||
| 68 | return $this->name; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get the name of the primary key. |
||
| 73 | * |
||
| 74 | * @return array |
||
| 75 | */ |
||
| 76 | public function getPrimaryKey(): array |
||
| 77 | { |
||
| 78 | return $this->primaryKey; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get the list target fields to anonymize. |
||
| 83 | * |
||
| 84 | * @return TargetField[] |
||
| 85 | */ |
||
| 86 | public function getTargetFields(): array |
||
| 87 | { |
||
| 88 | return $this->targetFields; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get the names of all known fields. |
||
| 93 | * |
||
| 94 | * @return string[] |
||
| 95 | */ |
||
| 96 | public function getAllFieldNames(): array |
||
| 97 | { |
||
| 98 | $fields = $this->primaryKey; |
||
| 99 | |||
| 100 | foreach ($this->targetFields as $targetField) { |
||
| 101 | $fields[] = $targetField->getName(); |
||
| 102 | } |
||
| 103 | |||
| 104 | return $fields; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Table must be truncated. |
||
| 109 | * |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function isTruncate(): bool |
||
| 113 | { |
||
| 114 | return $this->truncate; |
||
| 115 | } |
||
| 116 | } |
||
| 117 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.