ferrero-opentext/src/PostmanCollectionParser.php
DJP 716f0be068 Initial commit: Ferrero OpenText Content Scaling Application
Complete PHP-based workflow application for Ferrero DAM system:
- OAuth2 authentication with automatic token management
- Campaign discovery and filtering
- Folder structure navigation
- Asset download (individual and bulk)
- Metadata extraction and display
- Clean step-by-step web interface

Status: Fully functional and production-ready

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-17 14:29:20 -04:00

109 lines
No EOL
2.7 KiB
PHP

<?php
class PostmanCollectionParser
{
private $collection;
private $variables;
public function __construct($collectionPath)
{
$json = file_get_contents($collectionPath);
$this->collection = json_decode($json, true);
$this->variables = $this->extractVariables();
}
public function getCollection()
{
return $this->collection;
}
public function getVariables()
{
return $this->variables;
}
private function extractVariables()
{
$variables = [];
if (isset($this->collection['variable'])) {
foreach ($this->collection['variable'] as $var) {
$variables[$var['key']] = $var['value'] ?? '';
}
}
return $variables;
}
public function getAuthConfig()
{
if (!isset($this->collection['auth'])) {
return null;
}
$auth = $this->collection['auth'];
if ($auth['type'] === 'oauth2' && isset($auth['oauth2'])) {
$oauth2Config = [];
foreach ($auth['oauth2'] as $item) {
$oauth2Config[$item['key']] = $item['value'];
}
return [
'type' => 'oauth2',
'config' => $oauth2Config
];
}
return $auth;
}
public function getAllRequests()
{
$requests = [];
$this->extractRequestsRecursive($this->collection, $requests);
return $requests;
}
private function extractRequestsRecursive($item, &$requests, $path = '')
{
if (isset($item['item'])) {
foreach ($item['item'] as $subItem) {
$currentPath = $path ? $path . ' > ' . $subItem['name'] : $subItem['name'];
$this->extractRequestsRecursive($subItem, $requests, $currentPath);
}
} elseif (isset($item['request'])) {
$requests[] = [
'name' => $item['name'],
'path' => $path,
'request' => $item['request'],
'response' => $item['response'] ?? []
];
}
}
public function resolveVariables($url, $customVariables = [])
{
$allVariables = array_merge($this->variables, $customVariables);
foreach ($allVariables as $key => $value) {
$url = str_replace('{{' . $key . '}}', $value, $url);
}
return $url;
}
public function getRequestById($name)
{
$requests = $this->getAllRequests();
foreach ($requests as $request) {
if ($request['name'] === $name) {
return $request;
}
}
return null;
}
}