Skip to content

Commit 777a278

Browse files
Jiaming Youmeta-codesync[bot]
authored andcommitted
Added RequestAdaptor
Summary: Added RequestAdaptor to extract Plain Data Object (PDO) for CAPI param builder Reviewed By: hongj-src Differential Revision: D91758326 fbshipit-source-id: e58bf2329025de1b29c8c415f8f21c518d00e2a5
1 parent 4871699 commit 777a278

2 files changed

Lines changed: 1026 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/*
3+
* Copyright (c) Meta Platforms, Inc. and affiliates.
4+
*
5+
* This source code is licensed under the license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
namespace FacebookAds;
10+
require_once __DIR__ . '/../model/PlainDataObject.php';
11+
12+
use Throwable;
13+
use FacebookAds\PlainDataObject;
14+
15+
class RequestAdaptor {
16+
17+
/**
18+
* Extracts request data from global server variables or overrides.
19+
* * @param array|null $server_overrides
20+
* @return PlainDataObject
21+
*/
22+
public static function extract($server_overrides = null): PlainDataObject {
23+
// 1. Initialize Defaults (matching PlainDataObject types)
24+
$host = "";
25+
$query_params = [];
26+
$cookies = [];
27+
$referer = null; // Defaults to null for ?string
28+
$x_forwarded_for = null; // Defaults to null for ?string
29+
$remote_address = null; // Defaults to null for ?string
30+
31+
try {
32+
// Use global server or override
33+
$server = $server_overrides ?? $_SERVER;
34+
35+
if ($server) {
36+
// Extract Headers
37+
$host = $server['HTTP_HOST'] ?? '';
38+
$referer = $server['HTTP_REFERER'] ?? null;
39+
$x_forwarded_for = $server['HTTP_X_FORWARDED_FOR'] ?? null;
40+
$remote_address = $server['REMOTE_ADDR'] ?? null;
41+
42+
// Extract Query Params
43+
// Priority: $_GET -> Parse QUERY_STRING
44+
if (!empty($_GET)) {
45+
$query_params = $_GET;
46+
} elseif (!empty($server['QUERY_STRING'])) {
47+
parse_str($server['QUERY_STRING'], $parsed);
48+
if (is_array($parsed)) {
49+
$query_params = $parsed;
50+
}
51+
}
52+
53+
// Extract Cookies
54+
// Priority: $_COOKIE -> Parse HTTP_COOKIE
55+
if (!empty($_COOKIE)) {
56+
$cookies = $_COOKIE;
57+
} elseif (!empty($server['HTTP_COOKIE'])) {
58+
$pairs = explode(';', $server['HTTP_COOKIE']);
59+
foreach ($pairs as $pair) {
60+
$parts = explode('=', trim($pair), 2);
61+
if (count($parts) === 2) {
62+
$cookies[$parts[0]] = urldecode($parts[1]);
63+
}
64+
}
65+
}
66+
}
67+
} catch (Throwable $t) {
68+
// Silently ignore exceptions and return the object with default values
69+
}
70+
71+
// 2. Return the Data Object
72+
return new PlainDataObject(
73+
$host,
74+
$query_params,
75+
$cookies,
76+
$referer,
77+
$x_forwarded_for,
78+
$remote_address
79+
);
80+
}
81+
}

0 commit comments

Comments
 (0)