Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5687 | Rev 5689 | 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;
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
 
60
 
61
 
62
 
1 www 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);
5687 anderson 72
        $select->where->equalTo('sender_id', $user1)
5688 anderson 73
            ->and->equalTo('receiver_id', $user2);
5671 anderson 74
 
1 www 75
        return $this->executeFetchOneObject($select, $prototype);
76
    }
2339 nelberth 77
 
78
 
79
 
1 www 80
    /**
81
     *
82
     * @param int $user_id
83
     * @return Conversation[]
84
     */
85
    public function fetchAllByUserId($user_id)
86
    {
87
        $prototype = new Conversation();
5671 anderson 88
 
1 www 89
        $select = $this->sql->select(self::_TABLE);
90
        $select->where->and->nest()
5671 anderson 91
            ->nest()
92
            ->equalTo('sender_id', $user_id)
93
            ->and->equalTo('sender_status', Conversation::STATUS_NORMAL)
94
            ->unnest()
95
            ->or->nest()
96
            ->equalTo('receiver_id', $user_id)
97
            ->and->equalTo('receiver_status', Conversation::STATUS_NORMAL)
98
            ->unnest()
99
            ->unnest();
100
 
1 www 101
        return $this->executeFetchAllObject($select, $prototype);
102
    }
2339 nelberth 103
 
2350 nelberth 104
 
5671 anderson 105
 
1 www 106
    /**
107
     *
108
     * @param Conversation $conversation
109
     * @return boolean
110
     */
111
    public function insert($conversation)
112
    {
113
        $hydrator = new ObjectPropertyHydrator();
114
        $values = $hydrator->extract($conversation);
115
        $values = $this->removeEmpty($values);
5671 anderson 116
 
1 www 117
        $insert = $this->sql->insert(self::_TABLE);
118
        $insert->values($values);
5671 anderson 119
 
1 www 120
        $response = $this->executeInsert($insert);
5671 anderson 121
        if ($response) {
1 www 122
            $conversation->id = $this->lastInsertId;
123
        }
5671 anderson 124
 
1 www 125
        return $response;
126
    }
5671 anderson 127
 
128
 
1 www 129
    /**
130
     *
131
     * @param Conversation $conversation
132
     * @return boolean
133
     */
5671 anderson 134
    public function update($conversation)
1 www 135
    {
136
        $hydrator = new ObjectPropertyHydrator();
137
        $values = $hydrator->extract($conversation);
138
        $values = $this->removeEmpty($values);
5671 anderson 139
 
1 www 140
        $update = $this->sql->update(self::_TABLE);
141
        $update->set($values);
142
        $update->where->equalTo('id', $conversation->id);
5671 anderson 143
 
1 www 144
        return $this->executeUpdate($update);
145
    }
5671 anderson 146
}