Schema Attribute: add $one_value parameter to php2ldap() method

This commit is contained in:
Benjamin Renard 2023-03-21 16:53:46 +01:00
parent bb68b10ec4
commit 39dd48e0df
2 changed files with 33 additions and 27 deletions

View file

@ -111,29 +111,32 @@ class Attribute extends SchemaEntry {
} }
/** /**
* Convert LDAP value to PHP value * Convert LDAP values to PHP value
* @param array<int,string> $value * @param array<int,string> $values
* @return string|array<string>|null * @return string|array<string>|null
*/ */
public function ldap2php($value) { public function ldap2php($values) {
if ($value) if ($values)
return $this->single?$value[0]:$value; return $this->single?$values[0]:$values;
return $this->single?null:array(); return $this->single?null:array();
} }
/** /**
* Convert LDAP value to PHP value * Convert PHP value to LDAP values
* @param mixed $value * @param mixed $value
* @return array<int,string> * @param bool $one_value Convert one value of the attribute (optional, default: false)
* @return ( $one_value is True ? string|null : array<int,string> )
*/ */
public function php2ldap($value) { public function php2ldap($value, $one_value=false) {
if (is_null($value)) if (is_null($value))
return array(); return $one_value?null:array();
if ($one_value)
return strval($value);
if (!is_array($value)) if (!is_array($value))
$value = array($value); $value = array($value);
$ldap_value = array(); $ldap_values = array();
foreach($value as $v) foreach($value as $v)
$ldap_value[] = strval($v); $ldap_values[] = strval($v);
return $ldap_value; return $ldap_values;
} }
} }

View file

@ -34,35 +34,38 @@ class BooleanAttribute extends \EesyLDAP\Schema\Attribute {
return null; return null;
} }
/** /**
* Convert LDAP value to PHP value * Convert LDAP values to PHP value
* @param array<int,string> $value * @param array<int,string> $values
* @return bool|null|array<bool|null> * @return bool|null|array<bool|null>
*/ */
public function ldap2php($value) { public function ldap2php($values) {
$value = parent::ldap2php($value); $values = parent::ldap2php($values);
if (is_string($value)) if (is_string($values))
return self :: _ldap2php($value); return self :: _ldap2php($values);
if (is_array($value)) { if (is_array($values)) {
$php_value = array(); $php_value = array();
foreach($value as $v) foreach($values as $value)
$php_value[] = self :: _ldap2php($v); $php_value[] = self :: _ldap2php($value);
return $php_value; return $php_value;
} }
return $this->single?null:array(); return $this->single?null:array();
} }
/** /**
* Convert LDAP value to PHP value * Convert PHP value to LDAP values
* @param mixed $value * @param mixed $value
* @return array<int,string> * @param bool $one_value Convert one value value of the attribute (optional, default: false)
* @return ( $one_value is True ? string : array<int,string> )
*/ */
public function php2ldap($value) { public function php2ldap($value, $one_value=false) {
if (is_null($value)) if (is_null($value))
return array(); return array();
if ($one_value)
return $value?self :: TRUE_VALUE:self :: FALSE_VALUE;
$value = is_array($value)?$value:array($value); $value = is_array($value)?$value:array($value);
$ldap_value = array(); $ldap_values = array();
foreach(parent::php2ldap($value) as $value) foreach(parent::php2ldap($value) as $value)
$ldap_value[] = $value?self :: TRUE_VALUE:self :: FALSE_VALUE; $ldap_values[] = $value?self :: TRUE_VALUE:self :: FALSE_VALUE;
return $ldap_value; return $ldap_values;
} }
} }