Commit 82796fdf authored by David Surville's avatar David Surville
Browse files

Meilleure gestion du setter de l'attribut "unicaenServiceEtat"

parent a421d20a
Loading
Loading
Loading
Loading
+33 −4
Original line number Diff line number Diff line
@@ -1572,11 +1572,40 @@ class People extends Entity
    public function setUnicaenServiceEtat($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
            return preg_match(self::$service_etat_pattern, $v);
        });

        $this->appendOrNot('unicaenServiceEtat', $value, $append);
        if(empty($value)) {
            $this->appendOrNot('unicaenServiceEtat', $value, false);
        }

        if ($append) {
            $this->appendOrNot('unicaenServiceEtat', $value, true);
        }

        $newValues = [];
        foreach ($value as $v) {
            if (preg_match(self::$service_etat_pattern, $v, $matches)) {
                $newValues[$matches['etiquette']][] = $matches['value'];
            }
        };

        foreach ($newValues as $label => $values) {
            $currentValues = $this->preFormat($this->unicaenServiceEtat);
            array_walk($currentValues, function (&$cv) use ($label, $values) {
                if (preg_match("/^".$label."(?<value>.+)$/", $cv, $matches)) {
                    if(!in_array($matches['value'], $values)) {
                        $this->remove('unicaenServiceEtat', $cv);
                        $cv = null;
                    }
                }
            });
            foreach($values as $v) {
                $v = $label.$v;
                if (!in_array($v, $currentValues)) {
                    $currentValues[] = $v;
                }
            }
            $this->appendOrNot('unicaenServiceEtat', array_filter($currentValues), false);
        }

        return $this;
    }