Précédent   Forum des professionnels en informatique > PHP > Langage > Syntaxe
Syntaxe Forum d'entraide sur la syntaxe de PHP et la POO. Avant de poster -> FAQ syntaxe, Cours d'initiation et cours de POO
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 19/08/2011, 13h19   #1
Membre habitué
 
Inscription : décembre 2007
Messages : 579
Détails du profil
Informations personnelles :
Localisation : France, Seine Maritime (Haute Normandie)

Informations forums :
Inscription : décembre 2007
Messages : 579
Points : 114
Points : 114
Par défaut Fonction récursive avec un paramètre en objet

Bonjour à tous !

Alors voilà, pour l'entreprise pour laquelle je travaille, j'ai créé un WebService.
J'ai des objets avec des propriétés en string qui passent dans le WSDL. Mais il y a une curieuse chose ! certains textes passent mal, ceux disposant de \r\n, seul le \n arrive à destination, le \r est complètement oublié O_o.

J'ai donc créé une fonction récursive permettant de nettoyer un objet.

voici mon exemple :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
 
class myCLass
{
    public $boolean = false;
    public $integer = 0;
    public $double = 0.0;
    public $string = null;
 
    function myCLass($boolean, $integer, $double, $string)
    {
        $this->boolean = $boolean;
        $this->integer = $integer;
        $this->double = $double;
        $this->string = $string;
    }
}
 
function FixObjectPDA($myParam)
{
    switch(gettype($myParam))
    {
        case 'array' :
            foreach($myParam AS $oneObject)
            {
                $myParam[array_search($oneObject, $myParam)] = FixObjectPDA($oneObject);
            }
        break;
 
        case 'object' :
            foreach($myParam AS $myField)
            {
                $myParam[array_search($myField, $myParam)] = FixObjectPDA($myField);
            }
        break;
 
        case 'string' :
            return FixStringPDA($myParam);
        break;
 
        default :
            return  $myParam;
        break;
    }
}
 
function FixStringPDA($str)
{
    $FindLineFeed = preg_match("#\n#", $str);
    $FindCarriage = preg_match("#\r#", $str);
    if($FindLineFeed && !$FindCarriage)
    {
        return preg_replace("/(\n)/", "\r\n", $str);
    }
    return $str;
}
 
$myObject[] = new myCLass(true, 3, 1.8, "essai\r\n avec des retour chariot d'après ce qu'il semble !");
$myObject = FixObjectPDA($myObject);
var_dump($myObject);
 
?>
Seulement voilà, je ne suis pas un grand expert en fonction récursive.

Le fonctionnement :
Je peux passer en paramètre de la fonction un Objet ou une collection d'Objets.
Pour chaque éléments de la collection, on relance la fonction.
Dans le cas d'un champ string, on répare les données.
Dans le cas ou il s'agit d'un autre type de données, on ne fait aucun traitement.

ATTENTION ! Un objet peut posséder des sous-objets !

Le problème est que je n'arrive pas à obtenir de retour :/

Pourriez-vous m'aider svp ? Merci.
thor76160 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/08/2011, 13h32   #2
Membre habitué
 
Inscription : décembre 2007
Messages : 579
Détails du profil
Informations personnelles :
Localisation : France, Seine Maritime (Haute Normandie)

Informations forums :
Inscription : décembre 2007
Messages : 579
Points : 114
Points : 114
je viens de modifier un peu mon code. Je pense pouvoir m'en sortir, mais j'ai un problème à ma ligne n°35 :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
 
class myCLass
{
    public $boolean = false;
    public $integer = 0;
    public $double = 0.0;
    public $string = null;
 
    function myCLass($boolean, $integer, $double, $string)
    {
        $this->boolean = $boolean;
        $this->integer = $integer;
        $this->double = $double;
        $this->string = $string;
    }
}
 
function FixObjectPDA($myParam)
{
    switch(gettype($myParam))
    {
        case 'array' :
            $myTab = array();
            foreach($myParam AS $oneObject)
            {
                $myTab[] = FixObjectPDA($oneObject);
            }
            return $myTab;
        break;
 
        case 'object' :
            foreach($myParam AS $myField)
            {
                $myParam[array_search($myField, $myParam)] = FixObjectPDA($myField);
            }
            return $myParam;
        break;
 
        case 'string' :
            return FixStringPDA($myParam);
        break;
 
        default :
            return  $myParam;
        break;
    }
}
 
function FixStringPDA($str)
{
    $FindLineFeed = preg_match("#\n#", $str);
    $FindCarriage = preg_match("#\r#", $str);
    if($FindLineFeed && !$FindCarriage)
    {
        return preg_replace("/(\n)/", "\r\n", $str);
    }
    return $str;
}
 
$myObject[] = new myCLass(true, 3, 1.8, "essai\r\n avec des retour chariot d'après ce qu'il semble !");
$myObject = FixObjectPDA($myObject);
var_dump($myObject);
 
?>
auriez-vous une idée svp ?
thor76160 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/08/2011, 14h07   #3
Membre habitué
 
Inscription : décembre 2007
Messages : 579
Détails du profil
Informations personnelles :
Localisation : France, Seine Maritime (Haute Normandie)

Informations forums :
Inscription : décembre 2007
Messages : 579
Points : 114
Points : 114
j'ai réussi, et j'ai lutté !! -_-

Voici donc tout mon travail :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
 
class myCLass
{
    public $boolean = false;
    public $integer = 0;
    public $double = 0.0;
    public $string = null;
 
    function myCLass($boolean, $integer, $double, $string)
    {
        $this->boolean = $boolean;
        $this->integer = $integer;
        $this->double = $double;
        $this->string = $string;
    }
}
 
function FixObject($myParam)
{
    switch(gettype($myParam))
    {
        case 'array' :
            $myTab = array();
            foreach($myParam AS $oneObject)
            {
                $myTab[] = FixObject($oneObject);
            }
            return $myTab;
        break;
 
        case 'object' :
            $myParam = ((array) ($myParam));
            foreach($myParam AS $myField)
            {
                $myParam[array_search($myField, $myParam)] = FixObject($myField);
            }
            $myParam = ((object) ($myParam));
            return $myParam;
        break;
 
        case 'string' :
            return FixString($myParam);
        break;
 
        default :
            return $myParam;
        break;
    }
}
 
function FixString($str)
{
    $FindLineFeed = preg_match("#\n#", $str);
    $FindCarriage = preg_match("#\r#", $str);
    if($FindLineFeed && !$FindCarriage)
    {
        $str = preg_replace("/(\n)/", "\r\n", $str);
    }
 
    $FindLineFeed = preg_match("#\n#", $str);
    $FindDoubleCarriage = preg_match("#\r#", $str);
    if($FindLineFeed && $FindDoubleCarriage)
    {
        $str = preg_replace("/(\r\r\n)/", "\r\n", $str);
    }
 
    return $str;
}
 
$myObject = new myCLass(true, 3, 1.8, "essai\r\n avec des retours chariot d'après ce qu'il semble !");
$myObject = FixObject($myObject);
 
echo $myObject->string;
 
?>
thor76160 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/08/2011, 14h48   #4
Membre Expert
 
Avatar de kdmbella
 
Homme Demazy Mbella
Développeur Web
Inscription : août 2010
Messages : 620
Détails du profil
Informations personnelles :
Nom : Homme Demazy Mbella
Localisation : Cameroun

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : août 2010
Messages : 620
Points : 1 470
Points : 1 470
je pense que tu utilise mal ton foreach peut tu nous poster le message d'erreur ? retourner a cette ligne ?
__________________
Trois personnes peuvent garder un secret si deux d'entre elles sont mortes. :Benjamin Franklin
L'humanité se divise en trois catégories : ceux qui ne peuvent pas bouger, ceux qui peuvent bouger, et ceux qui bougent : Benjamin Franklin
Le hasard, c'est le déguisement que prend Dieu pour voyager incognito: Albert Einstein
bon je m'arrête là au risque de me faire buter
kdmbella est actuellement connecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/08/2011, 08h46   #5
Membre habitué
 
Inscription : décembre 2007
Messages : 579
Détails du profil
Informations personnelles :
Localisation : France, Seine Maritime (Haute Normandie)

Informations forums :
Inscription : décembre 2007
Messages : 579
Points : 114
Points : 114
j'ai réussi mon opération. voilà mon code final :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
 
class myCLass
{
    public $boolean = false;
    public $integer = 0;
    public $double = 0.0;
    public $string = null;
 
    function myCLass($boolean, $integer, $double, $string)
    {
        $this->boolean = $boolean;
        $this->integer = $integer;
        $this->double = $double;
        $this->string = $string;
    }
}
 
function FixObject($myParam)
{
    switch(gettype($myParam))
    {
        case 'array' :
            $myTab = array();
            foreach($myParam AS $oneObject)
            {
                $myTab[] = FixObject($oneObject);
            }
            return $myTab;
        break;
 
        case 'object' :
            $myParam = ((array) ($myParam));
            foreach($myParam AS $myField)
            {
                $myParam[array_search($myField, $myParam)] = FixObject($myField);
            }
            $myParam = ((object) ($myParam));
            return $myParam;
        break;
 
        case 'string' :
            return FixString($myParam);
        break;
 
        default :
            return $myParam;
        break;
    }
}
 
function FixString($str)
{
    $FindLineFeed = preg_match("#\n#", $str);
    $FindCarriage = preg_match("#\r#", $str);
    if($FindLineFeed && !$FindCarriage)
    {
        $str = preg_replace("/(\n)/", "\r\n", $str);
    }
 
    $FindLineFeed = preg_match("#\n#", $str);
    $FindDoubleCarriage = preg_match("#\r#", $str);
    if($FindLineFeed && $FindDoubleCarriage)
    {
        $str = preg_replace("/(\r\r\n)/", "\r\n", $str);
    }
 
    return $str;
}
 
$myObject = new myCLass(true, 3, 1.8, "essai\r\n avec des retours chariot d'après ce qu'il semble !");
$myObject = FixObject($myObject);
 
echo $myObject->string;
 
?>
merci quand même
thor76160 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 11h23.


 
 
 
 
Partenaires

Hébergement Web