vendredi 22 juin 2018

design pattern for iteration php

What would be the best way to implement a design pattern in OOP where you take an associative array and perform several changes to each iteration.

I was thinking it would be Iterator class and then injecting functionality?

Here is a simplified class showing the problem:

class Contacts {

    private $contacts = [
        [
            "name" => "Peter Parker",
            "email" => "peterparker@mail.com",
        ],
        [
            "name" => "Clark Kent",
            "email" => "clarkkent@mail.com",
        ],
        [
            "name" => "Harry Potter",
            "email" => "harrypotter@mail.com",
        ]
   ];

   function changeEmailDomain( {
        foreach($this->contacts as $key => $contact){
            str replace mail.com etc...
        }
    }

    function removeSurname(){
        foreach($this->contacts as $key => $contact){
            explode name field etc...
        }
    }

   function sortByName() {
        foreach etc...
   }
}

Aucun commentaire:

Enregistrer un commentaire