Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1682 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
use LeadersLinked\Model\Comment;
8
use LeadersLinked\Mapper\Common\MapperCommon;
9
use Laminas\Db\Adapter\AdapterInterface;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\Db\Sql\Expression;
12
use Laminas\Db\Sql\Insert;
13
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
14
use LeadersLinked\Model\Feed;
15
 
16
 
17
class CommentMapper extends MapperCommon
18
{
19
    const _TABLE = 'tbl_comments';
20
 
21
 
22
    /**
23
     *
24
     * @var CommentMapper
25
     */
26
    private static $_instance;
27
 
28
    /**
29
     *
30
     * @param AdapterInterface $adapter
31
     */
32
    private function __construct($adapter)
33
    {
34
        parent::__construct($adapter);
35
    }
36
 
37
    /**
38
     *
39
     * @param AdapterInterface $adapter
40
     * @param LoggerInterface $logger
41
     * @param int $user_id
42
     * @return \LeadersLinked\Mapper\CommentMapper
43
     */
44
    public static function getInstance($adapter)
45
    {
46
        if(self::$_instance == null) {
47
            self::$_instance = new CommentMapper($adapter);
48
        }
49
        return self::$_instance;
50
    }
51
 
52
    /**
53
     *
54
     * @param int $uuid
55
     * @return Comment
56
     */
57
    public function fetchOneByUuid($uuid)
58
    {
59
        $prototype = new Comment;
60
        $select = $this->sql->select(self::_TABLE);
61
        $select->where->equalTo('uuid', $uuid);
62
 
63
        return $this->executeFetchOneObject($select, $prototype);
64
    }
65
 
66
 
67
    /**
68
     *
69
     * @param int $feed_id
70
     * @return int
71
     */
72
    public function fetchCountCommentByFeedId($feed_id)
73
    {
74
        $select = $this->sql->select(self::_TABLE);
75
        $select->columns(['total' => new Expression('COUNT(*)') ]);
76
        $select->where->equalTo('feed_id', $feed_id);
77
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
78
 
79
        $record = $this->executeFetchOneArray($select);
80
        return $record['total'];
81
    }
82
 
83
 
84
    /**
85
     *
86
     * @param int $feed_id
87
     * @return Comment[]
88
     */
89
    public function fetchAllByFeedId($feed_id)
90
    {
91
        $prototype = new Comment();
92
 
93
        $select = $this->sql->select(self::_TABLE);
94
        $select->where->equalTo('feed_id', $feed_id);
95
 
96
        return $this->executeFetchAllObject($select, $prototype);
97
    }
98
 
99
    /**
100
     *
101
     * @param int $feed_id
102
     * @return Comment[]
103
     */
104
    public function fetchAllPublishedByFeedId($feed_id)
105
    {
106
        $prototype = new Comment();
107
 
108
        $select = $this->sql->select(self::_TABLE);
109
        $select->where->equalTo('feed_id', $feed_id);
110
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
111
 
112
        return $this->executeFetchAllObject($select, $prototype);
113
    }
114
 
115
 
116
    /**
117
     *
118
     * @param int $id
119
     * @return Comment
120
     */
121
    public function fetchOne($id)
122
    {
123
        $prototype = new Comment();
124
 
125
        $select = $this->sql->select(self::_TABLE);
126
        $select->where->equalTo('id', $id);
127
 
128
        return $this->executeFetchOneObject($select, $prototype);
129
    }
130
 
131
    /**
132
     *
133
     * @param Comment $comment
134
     * @return boolean
135
     */
136
    public function insert($comment)
137
    {
138
        $hydrator = new ObjectPropertyHydrator();
139
        $values = $hydrator->extract($comment);
140
        $values = $this->removeEmpty($values);
141
 
142
 
143
        $insert = $this->sql->insert(self::_TABLE);
144
        $insert->values($values);
145
 
146
        $result = $this->executeInsert($insert);
147
        if($result) {
148
           $comment->id = $this->getLastInsertId();
149
        }
150
 
151
        return $result;
152
 
153
    }
154
 
155
 
156
    /**
157
     *
158
     * @param Comment $comment
159
     * @return boolean
160
     */
161
    public function update($comment)
162
    {
163
        $hydrator = new ObjectPropertyHydrator();
164
        $values = $hydrator->extract($comment);
165
        $values = $this->removeEmpty($values);
166
 
167
 
168
        $update = $this->sql->update(self::_TABLE);
169
        $update->set($values);
170
        $update->where->equalTo('id', $comment->id);
171
 
172
 
173
 
174
 
175
        return $this->executeUpdate($update);
176
    }
177
 
178
 
179
    /**
180
     *
181
     * @param int $id
182
     * @return boolean
183
     */
184
    public function delete($id)
185
    {
186
 
187
 
188
        $update = $this->sql->update(self::_TABLE);
189
        $update->set([
190
            'status' => Comment::STATUS_DELETED
191
        ]);
192
        $update->where->equalTo('id', $id);
193
 
194
        return $this->executeUpdate($update);
195
    }
196
 
197
    /**
198
     *
199
     * @param int $feed_id
200
     * @return boolean
201
     */
202
    public function deleteAllByFeedId($feed_id)
203
    {
204
 
205
        $update = $this->sql->update(self::_TABLE);
206
        $update->set([
207
            'status' => Comment::STATUS_DELETED
208
        ]);
209
        $update->where->equalTo('feed_id', $feed_id);
210
 
211
        return $this->executeUpdate($update);
212
    }
213
}