I have UserMapper and UserAdapter classes. Also a UserInterface and User class.

I get some data from a 3rd party service. UserAdapter class implements the UserInterface to match the 3rd party data to my own User class.

In UserMapper class, I have getAll, getById, delete, update methods.

In getAll method I want to return a collection of users. I also have an Users array iterator class. getAll methods gets a raw data from 3rd party. With a foreach loop I create User objects and append them to the collection. Finally I return a Users collection.

The problem is creating the User objects is not that elegant. I am looking for a better way to do the same thing. I might be mixed up design patterns, so please fix me if the idea is completely wrong.

Note: The same thing can be easily done with a class and a few methods maybe but the main idea was decoupling 3rd party data and creating a pure domain layer. Also returning proper objects/data types for easier debugging and easy-to-understand code. There are simplified examples. The real business logic is more complex than this.

User implements UserInterface {
  protected $id;
  protected $name;
  protected $age;
  protected $sex;

  public function __construct($id) {
     $this->id = $id;
  }

  public function getId() {
     return $this->id;
  }

  public function setName($name) {
     $this->name = $name;
  }

  public function getName() {
     return $this->name;
  }

  public function setAge($age) {
     $this->age = $age;
  }

  public function getAge() {
     return $this->age;
  }

  public function setSex($sex) {
     $this->sex = $sex;
  }

  public function getSex() {
     return $this->sex;
  }
}

UserInterface {
  public function getId();
  public function getName();
  public function getAge();
  public function getSex();
}


UserAdapter implements UserInterface {
  protected $rawData;

  public function __construct($rawData) {
     $this->rawData = $rawData;
  }

  public function getId() {
     return $this->rawData['id'];
  }

  public function getName() {
     return $this->rawData['name']
  }

  public function getAge() {
     return $this->rawData['age']
  }

  public function getSex() {
     return $this->rawData['sex']
  }

Users extends IteratorIterator {
   private $iterator;

    public function __construct(ArrayIterator $iterator)
    {
        $this->iterator = $iterator;
        parent::__construct($iterator);
    }

    public function current(): User
    {
        parent::current();
    }

    public function toArray()
    {
        return iterator_to_array($this->iterator, true);
    }
}


UserMapper {
   public function getAll($rawUsers): Users {
      $users = new ArrayIterator();
            foreach ($$rawUsers as $rawUser) {
           $adapter = new UserAdapter($rawUser);
           $user = new User($adapter->getId());
           $user->setName($adapter->getName());
           $user->setAge($adapter->getAge());
           $user->setSex($adapter->getSex());
           $users->append($user);
            }
      $return new Users($users);
   }
}


}

0 Answers