Use ensureIsArray() helper to make code more readable

This commit is contained in:
Benjamin Renard 2020-11-30 19:43:52 +01:00
parent 770533b7ec
commit 106a2d9f04
19 changed files with 110 additions and 199 deletions

View file

@ -47,14 +47,8 @@ class LSattr_html_image extends LSattr_html {
LSerror :: addErrorCode('LSattr_html_03','password');
}
if ($data) {
if(is_array($data)) {
$element -> setValue($data[0]);
}
else {
$element -> setValue($data);
}
}
if ($data)
$element -> setValue(ensureIsArray($data)[0]);
return $element;
}

View file

@ -44,15 +44,11 @@ class LSattr_html_password extends LSattr_html {
}
if ($data) {
if(is_array($data)) {
$data = ensureIsArray($data);
if (count($data) > 1)
LSerror :: addErrorCode('LSattr_html_03', 'password');
$element -> setValue($data[0]);
}
else {
$element -> setValue($data);
}
}
return $element;
}

View file

@ -283,8 +283,7 @@ class LSattr_html_select_list extends LSattr_html{
$list = $LSsearch -> getSearchEntries();
foreach($list as $entry) {
$keys = $entry -> get($conf['values_attribute']);
if (!is_array($keys)) $keys=array($keys);
foreach ($keys as $key) {
foreach (ensureIsArray($keys) as $key) {
$retInfos[$key]=$key;
}
}
@ -312,11 +311,7 @@ class LSattr_html_select_list extends LSattr_html{
$retInfos=array();
if (is_string($attr)) {
if (isset($ldapObject->attrs[$attr]) && $ldapObject->attrs[$attr] instanceof LSattribute) {
$attr_values = $ldapObject->attrs[$attr]->getValue();
if (!$attr_values)
$attr_values = array();
elseif (!is_array($attr_values))
$attr_values = array($attr_values);
$attr_values = ensureIsArray($ldapObject->attrs[$attr]->getValue());
if (isset($options['translate_labels']) && !$options['translate_labels']) {
foreach($attr_values as $attr_value)
$retInfos[$attr_value] = $attr_value;
@ -334,11 +329,7 @@ class LSattr_html_select_list extends LSattr_html{
if (isset($ldapObject->attrs[$attr['attr']]) && $ldapObject->attrs[$attr['attr']] instanceof LSattribute) {
if (isset($attr['json_component_key'])) {
if (get_class($ldapObject->attrs[$attr['attr']]->html) == 'LSattr_html_jsonCompositeAttribute') {
$attr_values = $ldapObject->attrs[$attr['attr']]->getValue();
if (!$attr_values)
$attr_values = array();
elseif (!is_array($attr_values))
$attr_values = array($attr_values);
$attr_values = ensureIsArray($ldapObject->attrs[$attr['attr']]->getValue());
foreach($attr_values as $attr_value) {
$value_data = @json_decode($attr_value, true);
if (!isset($value_data[$attr['json_component_key']])) {

View file

@ -46,10 +46,7 @@ class LSattr_html_select_object extends LSattr_html{
return;
}
if ($data) {
if (!is_array($data)) {
$data=array($data);
}
$values=$this -> getFormValues($data);
$values = $this -> getFormValues(ensureIsArray($data));
if ($values) {
$element -> setValue($values);
}

View file

@ -184,10 +184,8 @@ class LSattribute extends LSlog_staticLoggerClass {
$onDisplay = $this -> getConfig('onDisplay');
if ($onDisplay) {
if (!is_array($onDisplay))
$onDisplay = array($onDisplay);
$result = $data;
foreach($onDisplay as $func) {
foreach(ensureIsArray($onDisplay) as $func) {
if (function_exists($func)) {
$result = call_user_func($func, $result);
}
@ -615,8 +613,7 @@ class LSattribute extends LSlog_staticLoggerClass {
self :: log_debug(strval($this)." -> fireEvent($event)");
$return = true;
if(isset($this -> config[$event])) {
$funcs = (!is_array($this -> config[$event])?array($this -> config[$event]):$this -> config[$event]);
foreach($funcs as $func) {
foreach(ensureIsArray($this -> config[$event]) as $func) {
if(function_exists($func)) {
self :: log_debug(strval($this)." -> fireEvent($event): run ".format_callable($func));
if(!call_user_func_array($func, array(&$this -> ldapObject))) {

View file

@ -87,11 +87,7 @@ class LSformElement extends LSlog_staticLoggerClass {
* @retval boolean Retourne True
*/
public function setValue($data) {
if (!is_array($data)) {
$data=array($data);
}
$this -> values = $data;
$this -> values = ensureIsArray($data);
return true;
}
@ -109,10 +105,7 @@ class LSformElement extends LSlog_staticLoggerClass {
* @retval boolean Retourne True
*/
public function setValueFromPostData($data) {
if (!is_array($data)) {
$data=array($data);
}
$this -> values = $data;
$this -> values = ensureIsArray($data);
return true;
}
@ -137,12 +130,7 @@ class LSformElement extends LSlog_staticLoggerClass {
* @retval void
*/
public function addValue($data) {
if (is_array($data)) {
$this -> values = array_merge($this -> values, $data);
}
else {
$this -> values[] = $data;
}
$this -> values = array_merge($this -> values, ensureIsArray($data));
}
/**
@ -254,9 +242,7 @@ class LSformElement extends LSlog_staticLoggerClass {
protected static function getData(&$post, $name) {
if (isset($post[$name])) {
$return = array();
if(!is_array($post[$name])) {
$post[$name] = array($post[$name]);
}
$post[$name] = ensureIsArray($post[$name]);
foreach($post[$name] as $key => $val) {
if (!is_empty($val)) {
$return[$key] = $val;

View file

@ -74,12 +74,9 @@ class LSformElement_date extends LSformElement {
* @retval boolean Retourne True
*/
public function setValue($data) {
if (!is_array($data)) {
$data=array($data);
}
$special_values = $this -> getSpecialValues();
$values = array();
foreach ($data as $value) {
foreach (ensureIsArray($data) as $value) {
if(is_numeric($value)) {
if (array_key_exists($value, $special_values)) {
$values[] = $special_values[$value];
@ -239,11 +236,7 @@ class LSformElement_date extends LSformElement {
}
}
else {
if(!is_array($values))
$values = array();
if(!is_array($special_values))
$special_values = array();
$return[$this -> name] = $special_values + $values;
$return[$this -> name] = ensureIsArray($special_values) + ensureIsArray($values);
self :: log_trace($this." -> merged values=".varDump($return[$this -> name]));
}
return true;

View file

@ -151,9 +151,7 @@ class LSformElement_jsonCompositeAttribute extends LSformElement {
protected function translateComponentValue($c,$value,$inLoop=false) {
if (!$inLoop && isset($this -> components[$c]['multiple']) && $this -> components[$c]['multiple']) {
$retval = array();
if (!is_array($value))
$value = array($value);
foreach($value as $val)
foreach(ensureIsArray($value) as $val)
$retval[] = $this -> translateComponentValue($c, $val, true);
}
else {

View file

@ -122,12 +122,8 @@ class LSformElement_labeledValue extends LSformElement {
}
if (isset($_POST[$this -> name."_labels"]) && isset($_POST[$this -> name."_values"])) {
$return[$this -> name] = array();
if(!is_array($_POST[$this -> name."_labels"])) {
$_POST[$this -> name."_labels"] = array($_POST[$this -> name."_labels"]);
}
if(!is_array($_POST[$this -> name."_values"])) {
$_POST[$this -> name."_values"] = array($_POST[$this -> name."_values"]);
}
$_POST[$this -> name."_labels"] = ensureIsArray($_POST[$this -> name."_labels"]);
$_POST[$this -> name."_values"] = ensureIsArray($_POST[$this -> name."_values"]);
foreach($_POST[$this -> name."_labels"] as $key => $label) {
$val = $_POST[$this -> name."_values"][$key];
if (!empty($label) && !is_empty($val)) {

View file

@ -85,8 +85,7 @@ class LSformElement_mail extends LSformElement_text {
public function autocomplete($pattern) {
$ret = array();
if ($this -> getParam('html_options.autocomplete')) {
$mail_attributes = $this -> getParam('html_options.autocomplete.mail_attributes', array('mail'));
if (!is_array($mail_attributes)) $mail_attributes = array($mail_attributes);
$mail_attributes = ensureIsArray($this -> getParam('html_options.autocomplete.mail_attributes', array('mail')));
$obj_type = $this -> getParam('html_options.autocomplete.object_type');
if ($obj_type) {
@ -124,9 +123,8 @@ class LSformElement_mail extends LSformElement_text {
$search -> run();
foreach($search -> getSearchEntries() as $e) {
foreach($mail_attributes as $attr) {
$mails = $e->get($attr);
$mails = ensureIsArray($e->get($attr));
if (!$mails) continue;
if (!is_array($mails)) $mails = array($mails);
foreach($mails as $mail)
$ret[$mail] = $e->displayName;
}
@ -170,8 +168,7 @@ class LSformElement_mail extends LSformElement_text {
$displayName = ($displayNameFormat?getFData($displayNameFormat, $object['attrs']):null);
foreach($mail_attributes as $attr) {
if (!isset($object['attrs'][$attr])) continue;
$mails = $object['attrs'][$attr];
if (!is_array($mails)) $mails = array($mails);
$mails = ensureIsArray($object['attrs'][$attr]);
foreach($mails as $mail)
$ret[$mail] = ($displayName?$displayName:$mail);
}

View file

@ -139,9 +139,7 @@ class LSformElement_mailQuota extends LSformElement {
}
if (isset($_POST[$this -> name.'_size'])) {
$return[$this -> name]=array();
if(!is_array($_POST[$this -> name.'_size'])) {
$_POST[$this -> name.'_size'] = array($_POST[$this -> name.'_size']);
}
$_POST[$this -> name.'_size'] = ensureIsArray($_POST[$this -> name.'_size']);
if(isset($_POST[$this -> name.'_sizeFact']) && !is_array($_POST[$this -> name.'_sizeFact'])) {
$_POST[$this -> name.'_sizeFact'] = array($_POST[$this -> name.'_sizeFact']);
}

View file

@ -278,19 +278,14 @@ class LSformElement_password extends LSformElement {
$mail = (String)$this -> sendMail['mail'];
self :: log_debug("send(): mail from params: '$mail'");
if (!$mail) {
$mail_attrs = $this -> getMailAttrs();
if (!is_array($mail_attrs)) {
$mail_attrs=array($mail_attrs);
}
$mail_attrs = ensureIsArray($this -> getMailAttrs());
self :: log_debug('send(): mail attrs: '.varDump($mail_attrs));
$checkDomainsList = $this -> getParam('html_options.mail.domain');
$checkDomain = $this -> getParam('html_options.mail.checkDomain', true, 'bool');
foreach($mail_attrs as $attr) {
$mail_attr = $this -> attr_html -> attribute -> ldapObject -> attrs[$attr];
if ($mail_attr instanceOf LSattribute) {
$mail_values = $mail_attr -> getValue();
if (!is_array($mail_values))
$mail_values = array($mail_values);
$mail_values = ensureIsArray($mail_attr -> getValue());
foreach($mail_values as $mail_value) {
if ($mail_value && checkEmail($mail_value, $checkDomainsList, $checkDomain)) {
$mail = $mail_value;

View file

@ -130,9 +130,7 @@ class LSformElement_quota extends LSformElement {
}
if (isset($_POST[$this -> name.'_size'])) {
$return[$this -> name]=array();
if(!is_array($_POST[$this -> name.'_size'])) {
$_POST[$this -> name.'_size'] = array($_POST[$this -> name.'_size']);
}
$_POST[$this -> name.'_size'] = ensureIsArray($_POST[$this -> name.'_size']);
if(isset($_POST[$this -> name.'_sizeFact']) && !is_array($_POST[$this -> name.'_sizeFact'])) {
$_POST[$this -> name.'_sizeFact'] = array($_POST[$this -> name.'_sizeFact']);
}

View file

@ -159,9 +159,7 @@ class LSformElement_valueWithUnit extends LSformElement {
}
$return[$this -> name]=array();
if (isset($_POST[$this -> name.'_valueWithUnit'])) {
if(!is_array($_POST[$this -> name.'_valueWithUnit'])) {
$_POST[$this -> name.'_valueWithUnit'] = array($_POST[$this -> name.'_valueWithUnit']);
}
$_POST[$this -> name.'_valueWithUnit'] = ensureIsArray($_POST[$this -> name.'_valueWithUnit']);
if(isset($_POST[$this -> name.'_unitFact']) && !is_array($_POST[$this -> name.'_unitFact'])) {
$_POST[$this -> name.'_unitFact'] = array($_POST[$this -> name.'_unitFact']);
}
@ -186,15 +184,11 @@ class LSformElement_valueWithUnit extends LSformElement {
}
}
if (isset($_POST[$this -> name])) {
if(!is_array($_POST[$this -> name])) {
$_POST[$this -> name] = array($_POST[$this -> name]);
}
$_POST[$this -> name] = ensureIsArray($_POST[$this -> name]);
$return[$this -> name] = array_merge($return[$this -> name], $_POST[$this -> name]);
}
if (isset($_POST[$this -> name.'_value'])) {
if (!is_array($_POST[$this -> name.'_value'])) {
$_POST[$this -> name.'_value']=array($_POST[$this -> name.'_value']);
}
$_POST[$this -> name.'_value'] = ensureIsArray($_POST[$this -> name.'_value']);
$return[$this -> name] = array_merge($return[$this -> name], $_POST[$this -> name.'_value']);
}
return true;

View file

@ -40,10 +40,6 @@ class LSformRule_differentPassword extends LSformRule {
public static function validate($value, $options=array(), &$formElement) {
$otherPasswordAttributes = LSconfig :: get('params.otherPasswordAttributes', null, null, $options);
if (!is_null($otherPasswordAttributes)) {
// Make sure otherPasswordAttributes is an array
if (!is_array($otherPasswordAttributes))
$otherPasswordAttributes = array($otherPasswordAttributes);
// Load LSattr_ldap_password
if (!LSsession :: loadLSclass("LSattr_ldap_password")) {
LSerror :: addErrorCode('LSformRule_differentPassword_02');
@ -51,7 +47,7 @@ class LSformRule_differentPassword extends LSformRule {
}
// Iter on otherPasswordAttributes to check password does not match
foreach($otherPasswordAttributes as $attr) {
foreach(ensureIsArray($otherPasswordAttributes) as $attr) {
// Check attribute exist
if (!isset($formElement -> attr_html -> attribute -> ldapObject -> attrs[$attr])) {
LSerror :: addErrorCode('LSformRule_differentPassword_03', $attr);

View file

@ -42,17 +42,9 @@ class LSformRule_mimetype extends LSformRule {
$file = LSsession :: getTmpFile($value);
$real_mimetype = mime_content_type($file);
$mimetype = LSconfig :: get('params.mimeType', null, null, $options);
if (!is_null($mimetype)) {
if (is_array($mimetype)) {
if (!in_array($real_mimetype, $mimetype))
return;
}
else {
if ($real_mimetype != $mimetype)
return;
}
}
$mimetypes = ensureIsArray(LSconfig :: get('params.mimeType', null, null, $options));
if ($mimetypes && !in_array($real_mimetype, $mimetypes))
return false;
$mimeTypeRegEx = LSconfig :: get('params.mimeTypeRegEx', null, 'string', $options);
if (is_string($mimeTypeRegEx) && !preg_match($mimeTypeRegEx, $real_mimetype))

View file

@ -56,11 +56,8 @@ class LSformRule_password extends LSformRule {
return;
}
$regex = LSconfig :: get('params.regex', null, null, $options);
if(!is_null($regex)) {
if (!is_array($regex))
$regex = array($regex);
$regex = ensureIsArray(LSconfig :: get('params.regex', null, null, $options));
if($regex) {
$minValidRegex = LSconfig :: get('params.minValidRegex', count($regex), 'int', $options);
if ($minValidRegex == 0 || $minValidRegex > count($regex))
$minValidRegex = count($regex);
@ -85,8 +82,8 @@ class LSformRule_password extends LSformRule {
}
}
$prohibitedValues = LSconfig :: get('params.prohibitedValues', null, null, $options);
if(is_array($prohibitedValues) && in_array($value, $prohibitedValues)) {
$prohibitedValues = ensureIsArray(LSconfig :: get('params.prohibitedValues', null, null, $options));
if(in_array($value, $prohibitedValues)) {
self :: log_debug("this password is prohibited");
return;
}

View file

@ -1359,9 +1359,8 @@ class LSldapObject extends LSlog_staticLoggerClass {
LSerror :: addErrorCode('LSrelation_05','getObjectKeyValueInRelation');
return;
}
if (!is_array($attrValues)) $attrValues=array($attrValues);
$keyValues = array();
foreach ($attrValues as $attrValue) {
foreach (ensureIsArray($attrValues) as $attrValue) {
if ($attrValue == 'dn') {
$dn=$object -> getDn();
if (!in_array($dn, $keyValues))
@ -1401,7 +1400,6 @@ class LSldapObject extends LSlog_staticLoggerClass {
LSerror :: addErrorCode('LSrelation_05','listObjectsInRelation');
return;
}
if (!is_array($attrValues)) $attrValues=array($attrValues);
$keyValues = self :: getObjectKeyValueInRelation($object, $objectType, $attrValues);
if (!empty($keyValues)) {
$keyValuesFilters = array();
@ -1409,7 +1407,16 @@ class LSldapObject extends LSlog_staticLoggerClass {
$keyValuesFilters[] = Net_LDAP2_Filter::create($attr,'equals',$keyValue);
}
$filter = LSldap::combineFilters('or', $keyValuesFilters);
return $this -> listObjects($filter,LSsession :: getRootDn(),array('scope' => 'sub','recursive' => true,'withoutCache'=>true, 'onlyAccessible' => false));
return $this -> listObjects(
$filter,
LSsession :: getRootDn(),
array(
'scope' => 'sub',
'recursive' => true,
'withoutCache'=>true,
'onlyAccessible' => false
)
);
}
return array();
@ -1511,13 +1518,9 @@ class LSldapObject extends LSlog_staticLoggerClass {
return;
}
if ($this -> attrs[$attr] instanceof LSattribute) {
if (!is_array($attrValues)) $attrValues=array($attrValue);
$keyValues = self :: getObjectKeyValueInRelation($object, $objectType, $attrValues);
$values = $this -> attrs[$attr] -> getValue();
if ((!is_array($values)) && (!empty($values))) {
$values = array($values);
}
if (is_array($values)) {
$values = ensureIsArray($this -> attrs[$attr] -> getValue());
if ($values) {
$updateData = array();
foreach($values as $value) {
if (!in_array($value, $keyValues)) {
@ -1550,15 +1553,15 @@ class LSldapObject extends LSlog_staticLoggerClass {
LSerror :: addErrorCode('LSrelation_05','renameOneObjectInRelation');
return;
}
if (!is_array($oldValues)) $oldValues=array($oldValues);
if ($object instanceof $objectType) {
if ($this -> attrs[$attr] instanceof LSattribute) {
$values = $this -> attrs[$attr] -> getValue();
if ((!is_array($values)) && (!empty($values))) {
$values = array($values);
}
if (is_array($values)) {
if (!($object instanceof $objectType))
return;
if (!($this -> attrs[$attr] instanceof LSattribute))
return;
$values = ensureIsArray($this -> attrs[$attr] -> getValue());
if (!$values)
return;
$updateData = array();
$oldValues = ensureIsArray($oldValues);
foreach($values as $value) {
if (!in_array($value, $oldValues)) {
$updateData[] = $value;
@ -1576,10 +1579,6 @@ class LSldapObject extends LSlog_staticLoggerClass {
}
return $this -> _updateData(array($attr => $updateData));
}
}
}
return;
}
/**
* Met à jour les objets du meme type que $this en relation avec l'objet $object
@ -1606,14 +1605,13 @@ class LSldapObject extends LSlog_staticLoggerClass {
LSerror :: addErrorCode('LSrelation_05','updateObjectsInRelation');
return;
}
if (!is_array($attrValues)) $attrValues=array($attrValue);
$currentDns = array();
$currentObjects = $this -> listObjectsInRelation($object, $attr, $objectType, $attrValues);
if(is_array($currentObjects)) {
if(!is_array($currentObjects))
return;
for ($i=0; $i<count($currentObjects); $i++) {
$currentDns[] = $currentObjects[$i] -> getDn();
}
}
$dontTouch = array_intersect($listDns, $currentDns);
for($i=0; $i<count($currentObjects); $i++) {
@ -2109,7 +2107,7 @@ class LSldapObject extends LSlog_staticLoggerClass {
if (empty($values)) {
return $return." empty\n";
}
if (!is_array($values)) $values = array($values);
$values = ensureIsArray($values);
// Truncate values if too long
for ($i=0; $i < count($values); $i++)

View file

@ -112,7 +112,7 @@ class LSrelation extends LSlog_staticLoggerClass {
case 'canEdit_attribute':
return $this -> getConfig($key);
case 'linkAttributeValues':
$linkAttributeValues = (is_array($this -> linkAttributeOtherValues)?$this -> linkAttributeOtherValues:array());
$linkAttributeValues = ensureIsArray($this -> linkAttributeOtherValues);
if ($this -> linkAttributeValue)
$linkAttributeValues[] = $this -> linkAttributeValue;
return $linkAttributeValues;
@ -761,9 +761,7 @@ class LSrelation extends LSlog_staticLoggerClass {
// Check additional required parameters
if ($additional_required_parameters) {
if (!is_array($additional_required_parameters))
$additional_required_parameters = array($additional_required_parameters);
foreach($additional_required_parameters as $p) {
foreach(ensureIsArray($additional_required_parameters) as $p) {
if (!isset($_REQUEST[$p])) {
self :: log_warning("Parameter '$p' is missing.");
LSerror :: addErrorCode('LSsession_12');