FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
DesignPatternsPHP/Structural/DataMapper/UserMapper.php at master · codulator/DesignPatternsPHP · GitHub
codulator
/
DesignPatternsPHP
Public
forked from
DesignPatternsPHP/DesignPatternsPHP
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
DesignPatternsPHP
/
Structural
/
DataMapper
/
UserMapper.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
107 lines (92 loc) · 2.29 KB
Breadcrumbs
DesignPatternsPHP
/
Structural
/
DataMapper
/
UserMapper.php
Copy path
File metadata and controls
107 lines (92 loc) · 2.29 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace
DesignPatterns
\
Structural
\
DataMapper
;
/**
* class UserMapper
*/
class
UserMapper
{
/**
* @var DBAL
*/
protected
$
adapter
;
/**
* @param DBAL $dbLayer
*/
public
function
__construct
(
DBAL
$
dbLayer
)
{
$
this
->
adapter
=
$
dbLayer
;
}
/**
* saves a user object from memory to Database
*
* @param User $user
*
* @return boolean
*/
public
function
save
(
User
$
user
)
{
/* $data keys should correspond to valid Table columns on the Database */
$
data
=
array
(
'
userid
'
=>
$
user
->
getUserId
(),
'
username
'
=>
$
user
->
getUsername
(),
'
email
'
=>
$
user
->
getEmail
(),
);
/* if no ID specified create new user else update the one in the Database */
if
(
null
=== (
$
id
=
$
user
->
getUserId
())) {
unset(
$
data
[
'
userid
'
]);
$
this
->
adapter
->
insert
(
$
data
);
return
true
;
}
else
{
$
this
->
adapter
->
update
(
$
data
,
array
(
'
userid = ?
'
=>
$
id
));
return
true
;
}
}
/**
* finds a user from Database based on ID and returns a User object located
* in memory
*
* @param int $id
*
* @throws \InvalidArgumentException
* @return User
*/
public
function
findById
(
$
id
)
{
$
result
=
$
this
->
adapter
->
find
(
$
id
);
if
(
0
==
count
(
$
result
)) {
throw
new
\
InvalidArgumentException
(
"
User #
$
id
not found
"
);
}
$
row
=
$
result
->
current
();
return
$
this
->
mapObject
(
$
row
);
}
/**
* fetches an array from Database and returns an array of User objects
* located in memory
*
* @return array
*/
public
function
findAll
()
{
$
resultSet
=
$
this
->
adapter
->
findAll
();
$
entries
=
array
();
foreach
(
$
resultSet
as
$
row
) {
$
entries
[] =
$
this
->
mapObject
(
$
row
);
}
return
$
entries
;
}
/**
* Maps a table row to an object
*
* @param array $row
*
* @return User
*/
protected
function
mapObject
(
array
$
row
)
{
$
entry
=
new
User
();
$
entry
->
setUserID
(
$
row
[
'
userid
'
]);
$
entry
->
setUsername
(
$
row
[
'
username
'
]);
$
entry
->
setEmail
(
$
row
[
'
email
'
]);
return
$
entry
;
}
}
Back
|
FazBrowse Home
|
New Git URL