Commit 95ec8fc6 authored by Côme Chilliet's avatar Côme Chilliet
Browse files

🚜 feat(Link) Move ObjectClass parsing to a public function

In case external code needs to call it.
Note: it could also work for attributetype parsing if we add attribute
 keywords to the switch.

issue #1
parent 0ece5b21
......@@ -158,46 +158,11 @@ class Link
$res2 = $this->search($nb, 'objectClass=*', ['objectClasses'], 'base');
foreach ($res2 as $attrs2) {
foreach ($attrs2['objectClasses'] as $val) {
$name = 'OID';
$value = '';
$pattern = explode(' ', $val);
$ocname = preg_replace("/^.* NAME\s+\(*\s*'([^']+)'\s*\)*.*$/", '\\1', $val);
$objectclasses[$ocname] = [];
foreach ($pattern as $chunk) {
switch ($chunk) {
case '(':
$value = '';
break;
case ')':
$chunk = '';
case 'NAME':
case 'DESC':
case 'SUP':
case 'STRUCTURAL':
case 'ABSTRACT':
case 'AUXILIARY':
case 'MUST':
case 'MAY':
if ($name != '') {
$v = $this->value2container($value);
if (in_array($name, ['MUST','MAY'])) {
if ($v === TRUE) {
$v = [];
} else if (!is_array($v)) {
$v = [$v];
}
}
$objectclasses[$ocname][$name] = $v;
}
$name = $chunk;
$value = '';
break;
default: $value .= $chunk.' ';
}
$infos = static::parseObjectClassDefinition($val);
if (isset($infos['NAME']) && is_string($infos['NAME'])) {
$objectclasses[$infos['NAME']] = $infos;
} else {
throw new Exception('Invalid NAME in class definition: '.print_r($infos['NAME'] ?? NULL, TRUE));
}
}
}
......@@ -206,10 +171,59 @@ class Link
return $objectclasses;
}
/**
* @return array<string,string|true|array<string>>
*/
public static function parseObjectClassDefinition (string $definition): array
{
$name = 'OID';
$value = '';
$pattern = explode(' ', $definition);
$infos = [];
foreach ($pattern as $chunk) {
switch ($chunk) {
case '(':
$value = '';
break;
case ')':
$chunk = '';
case 'NAME':
case 'DESC':
case 'SUP':
case 'STRUCTURAL':
case 'ABSTRACT':
case 'AUXILIARY':
case 'MUST':
case 'MAY':
if ($name != '') {
$v = static::value2container($value);
if (in_array($name, ['MUST','MAY'])) {
if ($v === TRUE) {
$v = [];
} else if (!is_array($v)) {
$v = [$v];
}
}
$infos[$name] = $v;
}
$name = $chunk;
$value = '';
break;
default: $value .= $chunk.' ';
}
}
return $infos;
}
/**
* @return string|true|array<string>
*/
protected function value2container (string $value)
protected static function value2container (string $value)
{
/* Set empty values to "TRUE" only */
if (preg_match('/^\s*$/', $value)) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment