Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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