|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\ActiveRecord; |
|
4
|
|
|
|
|
5
|
|
|
use Ffcms\Core\Arch\ActiveModel; |
|
6
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
|
7
|
|
|
use Ffcms\Core\Helper\FileSystem\File; |
|
8
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
|
9
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
10
|
|
|
use Ffcms\Core\Interfaces\iProfile; |
|
11
|
|
|
use Ffcms\Core\App as MainApp; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Profile. Active record model for user profile data store |
|
15
|
|
|
* @package Apps\ActiveRecord |
|
16
|
|
|
* @property int $id |
|
17
|
|
|
* @property int $user_id |
|
18
|
|
|
* @property string $nick |
|
19
|
|
|
* @property int $sex |
|
20
|
|
|
* @property string $birthday |
|
21
|
|
|
* @property string $city |
|
22
|
|
|
* @property string $hobby |
|
23
|
|
|
* @property int $rating |
|
24
|
|
|
* @property string $phone |
|
25
|
|
|
* @property string $url |
|
26
|
|
|
* @property string $custom_data |
|
27
|
|
|
* @property string $created_at |
|
28
|
|
|
* @property string $updated_at |
|
29
|
|
|
*/ |
|
30
|
|
|
class Profile extends ActiveModel implements iProfile |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get user profile via user_id like object (!!! profile.id !== user.id !!!) |
|
35
|
|
|
* @param int|null $user_id |
|
36
|
|
|
* @return self|null |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function identity($user_id = null) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($user_id === null) { |
|
41
|
|
|
$user_id = MainApp::$Session->get('ff_user_id'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($user_id === null || !Obj::isLikeInt($user_id) || $user_id < 1) { |
|
45
|
|
|
return null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// check in cache |
|
49
|
|
|
if (MainApp::$Memory->get('profile.object.cache.' . $user_id) !== null) { |
|
50
|
|
|
return MainApp::$Memory->get('profile.object.cache.' . $user_id); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// find row |
|
54
|
|
|
$profile = self::where('user_id', '=', $user_id); |
|
55
|
|
|
|
|
56
|
|
|
// empty? lets return null |
|
|
|
|
|
|
57
|
|
|
if (false === $profile || null === $profile || $profile->count() !== 1) { |
|
58
|
|
|
return null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$object = $profile->first(); |
|
62
|
|
|
|
|
63
|
|
|
MainApp::$Memory->set('profile.object.cache.' . $user_id, $object); |
|
64
|
|
|
return $object; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get user avatar full url for current object |
|
69
|
|
|
* @param string $type |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getAvatarUrl($type = 'small') |
|
73
|
|
|
{ |
|
74
|
|
|
$default = '/upload/user/avatar/' . $type . '/default.jpg'; |
|
75
|
|
|
if (!Arr::in($type, ['small', 'big', 'medium'])) { |
|
76
|
|
|
return MainApp::$Alias->scriptUrl . $default; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$route = '/upload/user/avatar/' . $type . '/' . $this->user_id . '.jpg'; |
|
80
|
|
|
if (File::exist($route)) { |
|
81
|
|
|
return MainApp::$Alias->scriptUrl . $route . '?mtime=' . File::mTime($route); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return MainApp::$Alias->scriptUrl . $default; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get user nickname. If is empty - return 'id+userId' |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getNickname() |
|
92
|
|
|
{ |
|
93
|
|
|
$userNick = $this->nick; |
|
94
|
|
|
if ($userNick === null || Str::likeEmpty($userNick)) { |
|
95
|
|
|
$userNick = 'id' . $this->id; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $userNick; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get user identity for current object |
|
103
|
|
|
* @return User|null |
|
104
|
|
|
*/ |
|
105
|
|
|
public function User() |
|
106
|
|
|
{ |
|
107
|
|
|
return User::identity($this->user_id); |
|
108
|
|
|
} |
|
109
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.