|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2023 Google LLC. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * For instructions on how to run the full sample: |
| 21 | + * |
| 22 | + * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/bigquery/api/README.md |
| 23 | + */ |
| 24 | + |
| 25 | +namespace Google\Cloud\Samples\Dlp; |
| 26 | + |
| 27 | +// [START dlp_inspect_string_omit_overlap] |
| 28 | +use Google\Cloud\Dlp\V2\DlpServiceClient; |
| 29 | +use Google\Cloud\Dlp\V2\ContentItem; |
| 30 | +use Google\Cloud\Dlp\V2\ExcludeInfoTypes; |
| 31 | +use Google\Cloud\Dlp\V2\ExclusionRule; |
| 32 | +use Google\Cloud\Dlp\V2\InfoType; |
| 33 | +use Google\Cloud\Dlp\V2\InspectConfig; |
| 34 | +use Google\Cloud\Dlp\V2\InspectionRule; |
| 35 | +use Google\Cloud\Dlp\V2\InspectionRuleSet; |
| 36 | +use Google\Cloud\Dlp\V2\Likelihood; |
| 37 | +use Google\Cloud\Dlp\V2\MatchingType; |
| 38 | + |
| 39 | +/** |
| 40 | + * Inspect a string for sensitive data, omitting overlapping matches on person and email |
| 41 | + * Omit matches on a PERSON_NAME detector if also matched by an EMAIL_ADDRESS detector. |
| 42 | + * |
| 43 | + * @param string $projectId The Google Cloud project id to use as a parent resource. |
| 44 | + * @param string $textToInspect The string to inspect. |
| 45 | + */ |
| 46 | +function inspect_string_omit_overlap( |
| 47 | + // TODO(developer): Replace sample parameters before running the code. |
| 48 | + string $projectId, |
| 49 | + string $textToInspect = '[email protected] is an email.' |
| 50 | +): void { |
| 51 | + // Instantiate a client. |
| 52 | + $dlp = new DlpServiceClient(); |
| 53 | + |
| 54 | + $parent = "projects/$projectId/locations/global"; |
| 55 | + |
| 56 | + // Specify what content you want the service to Inspect. |
| 57 | + $item = (new ContentItem()) |
| 58 | + ->setValue($textToInspect); |
| 59 | + |
| 60 | + // Specify the type of info the inspection will look for. |
| 61 | + $personName = (new InfoType()) |
| 62 | + ->setName('PERSON_NAME'); |
| 63 | + $emailAddress = (new InfoType()) |
| 64 | + ->setName('EMAIL_ADDRESS'); |
| 65 | + $infoTypes = [$personName, $emailAddress]; |
| 66 | + |
| 67 | + // Exclude EMAIL_ADDRESS matches |
| 68 | + $exclusionRule = (new ExclusionRule()) |
| 69 | + ->setMatchingType(MatchingType::MATCHING_TYPE_PARTIAL_MATCH) |
| 70 | + ->setExcludeInfoTypes((new ExcludeInfoTypes()) |
| 71 | + ->setInfoTypes([$emailAddress]) |
| 72 | + ); |
| 73 | + |
| 74 | + // Construct a ruleset that applies the exclusion rule to the PERSON_NAME infotype. |
| 75 | + // If a PERSON_NAME match overlaps with an EMAIL_ADDRESS match, the PERSON_NAME match will |
| 76 | + // be excluded. |
| 77 | + $inspectionRuleSet = (new InspectionRuleSet()) |
| 78 | + ->setInfoTypes([$personName]) |
| 79 | + ->setRules([ |
| 80 | + (new InspectionRule()) |
| 81 | + ->setExclusionRule($exclusionRule), |
| 82 | + ]); |
| 83 | + |
| 84 | + // Construct the configuration for the Inspect request, including the ruleset. |
| 85 | + $inspectConfig = (new InspectConfig()) |
| 86 | + ->setInfoTypes($infoTypes) |
| 87 | + ->setIncludeQuote(true) |
| 88 | + ->setRuleSet([$inspectionRuleSet]); |
| 89 | + |
| 90 | + // Run request |
| 91 | + $response = $dlp->inspectContent([ |
| 92 | + 'parent' => $parent, |
| 93 | + 'inspectConfig' => $inspectConfig, |
| 94 | + 'item' => $item |
| 95 | + ]); |
| 96 | + |
| 97 | + // Print the results |
| 98 | + $findings = $response->getResult()->getFindings(); |
| 99 | + if (count($findings) == 0) { |
| 100 | + printf('No findings.' . PHP_EOL); |
| 101 | + } else { |
| 102 | + printf('Findings:' . PHP_EOL); |
| 103 | + foreach ($findings as $finding) { |
| 104 | + printf(' Quote: %s' . PHP_EOL, $finding->getQuote()); |
| 105 | + printf(' Info type: %s' . PHP_EOL, $finding->getInfoType()->getName()); |
| 106 | + printf(' Likelihood: %s' . PHP_EOL, Likelihood::name($finding->getLikelihood())); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | +// [END dlp_inspect_string_omit_overlap] |
| 111 | + |
| 112 | +// The following 2 lines are only needed to run the samples |
| 113 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 114 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments