From 4ff0def140f9be10cd7ac6fad26da093370f65fe Mon Sep 17 00:00:00 2001 From: kbariotis Date: Sat, 11 May 2013 20:07:59 +0300 Subject: [PATCH] Added DataMapper files --- DataMapper/Test.php | 0 DataMapper/User.php | 73 +++++++++++++++++++++++++++++++++++++ DataMapper/UserMapper.php | 77 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 DataMapper/Test.php create mode 100644 DataMapper/User.php create mode 100644 DataMapper/UserMapper.php 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 @@ +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 @@ + $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