Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 2340 | Rev 5671 | 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
    }
2339 nelberth 85
 
86
 
87
 
1 www 88
    /**
89
     *
90
     * @param int $user_id
91
     * @return Conversation[]
92
     */
93
    public function fetchAllByUserId($user_id)
94
    {
95
        $prototype = new Conversation();
96
 
97
        $select = $this->sql->select(self::_TABLE);
98
        $select->where->and->nest()
99
        ->nest()
100
        ->equalTo('sender_id', $user_id)
101
        ->and->equalTo('sender_status', Conversation::STATUS_NORMAL)
102
        ->unnest()
103
        ->or->nest()
104
        ->equalTo('receiver_id', $user_id)
105
        ->and->equalTo('receiver_status', Conversation::STATUS_NORMAL)
106
        ->unnest()
107
        ->unnest();
108
 
109
        return $this->executeFetchAllObject($select, $prototype);
110
    }
2339 nelberth 111
 
2350 nelberth 112
 
1 www 113
 
114
    /**
115
     *
116
     * @param Conversation $conversation
117
     * @return boolean
118
     */
119
    public function insert($conversation)
120
    {
121
        $hydrator = new ObjectPropertyHydrator();
122
        $values = $hydrator->extract($conversation);
123
        $values = $this->removeEmpty($values);
124
 
125
        $insert = $this->sql->insert(self::_TABLE);
126
        $insert->values($values);
127
 
128
        $response = $this->executeInsert($insert);
129
        if($response) {
130
            $conversation->id = $this->lastInsertId;
131
        }
132
 
133
        return $response;
134
    }
135
 
136
 
137
    /**
138
     *
139
     * @param Conversation $conversation
140
     * @return boolean
141
     */
142
    public function update($conversation)
143
    {
144
        $hydrator = new ObjectPropertyHydrator();
145
        $values = $hydrator->extract($conversation);
146
        $values = $this->removeEmpty($values);
147
 
148
        $update = $this->sql->update(self::_TABLE);
149
        $update->set($values);
150
        $update->where->equalTo('id', $conversation->id);
151
 
152
        return $this->executeUpdate($update);
153
    }
154
 
155
 
156
 
157
}