|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nicklas\Comment; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* A database driven model. |
|
7
|
|
|
*/ |
|
8
|
|
|
class Comment extends ActiveRecordModelExtender |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var string $tableName name of the database table. |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $tableName = "ramverk1_comments"; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Columns in the table. |
|
18
|
|
|
* |
|
19
|
|
|
* @var integer $id primary key auto incremented. |
|
20
|
|
|
*/ |
|
21
|
|
|
public $id; |
|
22
|
|
|
public $di; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
public $text; |
|
26
|
|
|
|
|
27
|
|
|
public $parentId; # All posts have different ids |
|
28
|
|
|
public $type; # question/answer/comment |
|
29
|
|
|
|
|
30
|
|
|
public $created; |
|
31
|
|
|
public $status; # default is active |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* HA EN FOREACH I En FOREACH FÖR ATT HA COMMENTS CONNECTED |
|
38
|
|
|
* |
|
39
|
|
|
|
|
40
|
|
|
*/ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Constructor injects with DI container. |
|
44
|
|
|
* |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct($di = null) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->di = $di; |
|
49
|
|
|
} |
|
50
|
|
|
/** |
|
51
|
|
|
* Returns post with markdown and gravatar |
|
52
|
|
|
* @param string $sql |
|
53
|
|
|
* @param array $param |
|
|
|
|
|
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getPosts($sql, $params) |
|
58
|
|
|
{ |
|
59
|
|
|
$posts = $this->findAllWhere("$sql", $params); |
|
60
|
|
|
|
|
61
|
|
|
return array_map(function ($post) { |
|
62
|
|
|
$user = new User(); |
|
63
|
|
|
$user->setDb($this->di->get("db")); |
|
64
|
|
|
$user->find("name", $post->user); |
|
65
|
|
|
|
|
66
|
|
|
$post->img = $this->gravatar($user->email); |
|
67
|
|
|
$post->markdown = $this->getMD($post->text); |
|
68
|
|
|
|
|
69
|
|
|
return $post; |
|
70
|
|
|
}, $posts); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* return question/answer, three attributes are set, comments connected to them is an array. |
|
75
|
|
|
* |
|
76
|
|
|
* @return object |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getPost($id) |
|
79
|
|
|
{ |
|
80
|
|
|
$post = $this->find("id", $id); |
|
81
|
|
|
|
|
82
|
|
|
// Get user who posted |
|
83
|
|
|
$user = new User(); |
|
84
|
|
|
$user->setDb($this->di->get("db")); |
|
85
|
|
|
$user->find("name", $post->user); |
|
86
|
|
|
|
|
87
|
|
|
// Start setting attributes |
|
88
|
|
|
$post->comments = $this->getPosts("parentId = ? AND type = ?", [$id, "comment"]); |
|
89
|
|
|
$post->img = $this->gravatar($user->email); |
|
90
|
|
|
$post->markdown = $this->getMD($post->text); |
|
91
|
|
|
|
|
92
|
|
|
return $post; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Check if a post belongs to user |
|
99
|
|
|
* |
|
100
|
|
|
* |
|
101
|
|
|
* @return boolean |
|
102
|
|
|
*/ |
|
103
|
|
|
public function controlAuthority($name) |
|
104
|
|
|
{ |
|
105
|
|
|
$user = new User(); |
|
106
|
|
|
$user->setDb($this->di->get("db")); |
|
107
|
|
|
$user->find("name", $name); |
|
108
|
|
|
|
|
109
|
|
|
// IF AUTHORITY == admin, then continue |
|
110
|
|
|
if ($user->authority != "admin") { |
|
111
|
|
|
return ($user->name == $this->user); |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.