Completed
Branch feature-relations (7f62b3)
by Thomas
03:51
created

Relation::addJoin()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
3
namespace ORM;
4
5
use ORM\Exceptions\IncompletePrimaryKey;
6
use ORM\Exceptions\InvalidRelation;
7
8
abstract class Relation
9
{
10
    /** The name of the relation for error messages
11
     * @var string */
12
    protected $name;
13
14
    /** The class that is related
15
     * @var string */
16
    protected $class;
17
18
    /** The name of the relation in the related class
19
     * @var string */
20
    protected $opponent;
21
22
    /** Reference definition as key value pairs
23
     * @var array */
24
    protected $reference;
25
26
    /**
27
     * @return string
28
     */
29 8
    public function getClass()
30
    {
31 8
        return $this->class;
32
    }
33
34
    /**
35
     * @return array
36
     */
37 30
    public function getReference()
38
    {
39 30
        return $this->reference;
40
    }
41
42
    /**
43
     * @return Relation
44
     */
45 32
    public function getOpponent()
46
    {
47
        // @todo seems we need a test for it
48
//        if (!$this->opponent) {
49
//            return null;
50
//        }
51
52 32
        return call_user_func([$this->class, 'getRelation'], $this->opponent);
53
    }
54
55
    /**
56
     * Fetch the relation
57
     *
58
     * Runs fetch on the EntityManager and returns its result.
59
     *
60
     * @param Entity        $me
61
     * @param EntityManager $entityManager
62
     * @return mixed
63
     */
64
    abstract public function fetch(Entity $me, EntityManager $entityManager);
65
66
    /**
67
     * Fetch all from the relation
68
     *
69
     * Runs fetch and returns EntityFetcher::all() if a Fetcher is returned.
70
     *
71
     * @param Entity        $me
72
     * @param EntityManager $entityManager
73
     * @return array
74
     */
75 3
    public function fetchAll(Entity $me, EntityManager $entityManager)
76
    {
77 3
        $fetcher = $this->fetch($me, $entityManager);
78
79 3
        if ($fetcher instanceof EntityFetcher) {
80 1
            return $fetcher->all();
81
        }
82
83 2
        return $fetcher;
84
    }
85
86
    /**
87
     * Get the foreign key for the given reference
88
     *
89
     * @param Entity $me
90
     * @param array  $reference
91
     * @return array
92
     * @throws IncompletePrimaryKey
93
     */
94 10
    protected function getForeignKey(Entity $me, $reference)
95
    {
96 10
        $foreignKey = [];
97
98 10
        foreach ($reference as $var => $fkCol) {
99 10
            $value = $me->__get($var);
100
101 10
            if ($value === null) {
102 2
                throw new IncompletePrimaryKey('Key incomplete for join');
103
            }
104
105 8
            $foreignKey[$fkCol] = $value;
106
        }
107
108 8
        return $foreignKey;
109
    }
110
111
    /**
112
     * Set the relation to $entity
113
     *
114
     * @param Entity      $me
115
     * @param Entity|null $entity
116
     * @throws InvalidRelation
117
     */
118 1
    public function setRelated(Entity $me, Entity $entity = null)
119
    {
120 1
        throw new InvalidRelation('This is not the owner of the relation');
121
    }
122
123
    /**
124
     * Add $entities to association table
125
     *
126
     * @param Entity        $me
127
     * @param Entity[]      $entities
128
     * @param EntityManager $entityManager
129
     * @throws InvalidRelation
130
     */
131 1
    public function addRelated(Entity $me, array $entities, EntityManager $entityManager)
132
    {
133 1
        throw new InvalidRelation('This is not a many-to-many relation');
134
    }
135
136
    /**
137
     * Delete $entities from association table
138
     *
139
     * @param Entity        $me
140
     * @param Entity[]      $entities
141
     * @param EntityManager $entityManager
142
     * @throws InvalidRelation
143
     */
144 1
    public function deleteRelated(Entity $me, array $entities, EntityManager $entityManager)
145
    {
146 1
        throw new InvalidRelation('This is not a many-to-many relation');
147
    }
148
149
    /**
150
     * Join this relation in $fetcher
151
     *
152
     * @param EntityFetcher $fetcher
153
     * @param string        $join
154
     * @return mixed
155
     */
156
    abstract public function addJoin(EntityFetcher $fetcher, $join);
157
}
158