diff --git a/DataMapper/Test.php b/DataMapper/Test.php new file mode 100644 index 0000000..e69de29 diff --git a/DataMapper/User.php b/DataMapper/User.php new file mode 100644 index 0000000..a61b243 --- /dev/null +++ b/DataMapper/User.php @@ -0,0 +1,73 @@ +<?php + +class User +{ + protected $userId; + protected $username; + protected $email; + + public function __construct(array $options = null) + { + if (is_array($options)) { + $this->setOptions($options); + } + } + + public function __set($name, $value) + { + $method = 'set' . $name; + if (('mapper' == $name) || !method_exists($this, $method)) { + throw new Exception('Invalid Offer property'); + } + $this->$method($value); + } + + public function __get($name) + { + $method = 'get' . $name; + if (('mapper' == $name) || !method_exists($this, $method)) { + throw new Exception('Invalid Offer property'); + } + return $this->$method(); + } + + public function setOptions(array $options) + { + $methods = get_class_methods($this); + + foreach ($options as $key => $value) { + $method = 'set' . ucfirst($key); + + if (in_array($method, $methods)) { + $this->$method($value); + + } + } + return $this; + } + + public function getUserId() { + return $this->userId; + } + + public function setUserID($userId) { + $this->userId = $userId; + } + + public function getUsername() { + return $this->username; + } + + public function setUsername($username) { + $this->username = $username; + } + + public function getEmail() { + return $this->email; + } + + public function setEmail($email) { + $this->email = $email; + } + +} \ No newline at end of file diff --git a/DataMapper/UserMapper.php b/DataMapper/UserMapper.php new file mode 100644 index 0000000..69201fa --- /dev/null +++ b/DataMapper/UserMapper.php @@ -0,0 +1,77 @@ +<?php + +namespace DesignPatterns; + +/** + * DataMapper pattern + * + * Purpose: + * A Data Mapper, is a Data Access Layer that performs bidirectional transfer of + * data between a persistent data store (often a relational database) and an in + * memory data representation (the domain layer). The goal of the pattern is to + * keep the in memory representation and the persistent data store independent of + * each other and the data mapper itself. The layer is composed of one or more + * mappers (or Data Access Objects), performing the data transfer. Mapper + * implementations vary in scope. Generic mappers will handle many different domain + * entity types, dedicated mappers will handle one or a few. + * (FROM http://en.wikipedia.org/wiki/Data_mapper_pattern) + * + * Examples: + * - DB Object Relational Mapper (ORM) + * + */ +class UserMapper +{ + + // the table where the mapper is mapping + protected $_dbTable; + + public function __construct(array $options = null) + { + // create new db connector on dbTable using specific table + } + + // will save a certain user from memory to db + public function save(User $user) + { + $data = array( + 'userid' => $user->getUserId(), + 'username' => $user->getUsername(), + 'email' => $user->getEmail(), + ); + + if (null === ($id = $user->getUserId())) { + unset($data['userid']); + $this->_dbTable->insert($data); + } else { + $this->_dbTable->update($data, array('userid = ?' => $id)); + } + } + + // will find a user from db based on the id and return it to a User object in memory + public function find($id, User $user) + { + $result = $this->_dbTable()->find($id); + if (0 == count($result)) { + return; + } + $row = $result->current(); + + $user->setOptions($row); + } + + // will fetch all entries from a table to memory + public function fetchAll() + { + $resultSet = $this->_dbTable()->fetchAll(); + $entries = array(); + + foreach ($resultSet as $row) { + $entry = new User(); + $entry->setOptions($row); + $entries[] = $entry; + } + + return $entries; + } +} \ No newline at end of file