Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5693 | | Comparar con el anterior | 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 Laminas\Db\Adapter\AdapterInterface;
5671 anderson 8
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
1 www 9
use LeadersLinked\Mapper\Common\MapperCommon;
10
use LeadersLinked\Model\Conversation;
11
 
12
class ConversationMapper extends MapperCommon
13
{
14
    const _TABLE = 'tbl_conversations';
15
 
5671 anderson 16
 
1 www 17
    /**
18
     *
19
     * @var ConversationMapper
20
     */
21
    private static $_instance;
5671 anderson 22
 
1 www 23
    /**
24
     *
25
     * @param AdapterInterface $adapter
26
     */
27
    private function __construct($adapter)
28
    {
29
        parent::__construct($adapter);
30
    }
5671 anderson 31
 
1 www 32
    /**
33
     *
34
     * @param AdapterInterface $adapter
35
     * @return ConversationMapper
36
     */
37
    public static function getInstance($adapter)
38
    {
5671 anderson 39
        if (self::$_instance == null) {
1 www 40
            self::$_instance = new ConversationMapper($adapter);
41
        }
42
        return self::$_instance;
43
    }
44
 
5671 anderson 45
 
1 www 46
    /**
47
     *
48
     * @param int $id
49
     * @return Conversation
50
     */
51
    public function fetchOne($id)
52
    {
53
        $select = $this->sql->select(self::_TABLE);
54
        $select->where->equalTo('id', $id);
5671 anderson 55
 
1 www 56
        $prototype = new Conversation();
57
        return $this->executeFetchOneObject($select, $prototype);
58
    }
5671 anderson 59
 
6749 efrain 60
 
61
 
62
    /**
63
     *
64
     * @param string $uuid
65
     * @return Conversation
66
     */
67
    public function fetchByUuid($uuid)
68
    {
69
        $select = $this->sql->select(self::_TABLE);
70
        $select->where->equalTo('uuid', $uuid);
71
 
72
        $prototype = new Conversation();
73
        return $this->executeFetchOneObject($select, $prototype);
74
    }
75
 
76
 
5671 anderson 77
 
78
 
79
 
1 www 80
    /**
81
     * @param int $user1
82
     * @param int $user2
83
     * @return Conversation
84
     */
85
    public function fetchOneByUserId1AndUserId2($user1, $user2)
86
    {
87
        $prototype = new Conversation();
88
        $select = $this->sql->select(self::_TABLE);
5690 anderson 89
        $select->where->and->nest()
5691 anderson 90
            ->nest()
5690 anderson 91
            ->equalTo('sender_id', $user1)
5689 anderson 92
            ->and->equalTo('receiver_id', $user2)
5692 anderson 93
            ->unnest()
94
            ->or->nest()
95
            ->equalTo('receiver_id', $user1)
96
            ->and->equalTo('sender_id', $user2)
97
            ->unnest()
98
            ->unnest();
5671 anderson 99
 
1 www 100
        return $this->executeFetchOneObject($select, $prototype);
101
    }
2339 nelberth 102
 
103
 
104
 
1 www 105
    /**
106
     *
107
     * @param int $user_id
108
     * @return Conversation[]
109
     */
110
    public function fetchAllByUserId($user_id)
111
    {
112
        $prototype = new Conversation();
5671 anderson 113
 
1 www 114
        $select = $this->sql->select(self::_TABLE);
115
        $select->where->and->nest()
5671 anderson 116
            ->nest()
117
            ->equalTo('sender_id', $user_id)
118
            ->and->equalTo('sender_status', Conversation::STATUS_NORMAL)
119
            ->unnest()
120
            ->or->nest()
121
            ->equalTo('receiver_id', $user_id)
122
            ->and->equalTo('receiver_status', Conversation::STATUS_NORMAL)
123
            ->unnest()
124
            ->unnest();
125
 
1 www 126
        return $this->executeFetchAllObject($select, $prototype);
127
    }
6749 efrain 128
 
2339 nelberth 129
 
2350 nelberth 130
 
5671 anderson 131
 
6749 efrain 132
 
1 www 133
    /**
134
     *
135
     * @param Conversation $conversation
136
     * @return boolean
137
     */
138
    public function insert($conversation)
139
    {
140
        $hydrator = new ObjectPropertyHydrator();
141
        $values = $hydrator->extract($conversation);
142
        $values = $this->removeEmpty($values);
5671 anderson 143
 
1 www 144
        $insert = $this->sql->insert(self::_TABLE);
145
        $insert->values($values);
5671 anderson 146
 
1 www 147
        $response = $this->executeInsert($insert);
5671 anderson 148
        if ($response) {
1 www 149
            $conversation->id = $this->lastInsertId;
150
        }
5671 anderson 151
 
1 www 152
        return $response;
153
    }
5671 anderson 154
 
155
 
1 www 156
    /**
157
     *
158
     * @param Conversation $conversation
159
     * @return boolean
160
     */
5671 anderson 161
    public function update($conversation)
1 www 162
    {
163
        $hydrator = new ObjectPropertyHydrator();
164
        $values = $hydrator->extract($conversation);
165
        $values = $this->removeEmpty($values);
5671 anderson 166
 
1 www 167
        $update = $this->sql->update(self::_TABLE);
168
        $update->set($values);
169
        $update->where->equalTo('id', $conversation->id);
5671 anderson 170
 
1 www 171
        return $this->executeUpdate($update);
172
    }
5671 anderson 173
}