Completed
Push — master ( d3e92b...b05d3c )
by Filipe
02:47
created

HasAndBelongsToMany::getRelationTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 17
cts 17
cp 1
rs 9.4285
cc 2
eloc 16
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of slick/orm package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Orm\Mapper\Relation;
11
use Slick\Common\Utils\Text;
12
13
/**
14
 * HasAndBelongsToMany relation
15
 * 
16
 * @package Slick\Orm\Mapper\Relation
17
 * @author  Filipe Silva <[email protected]>
18
 */
19
class HasAndBelongsToMany extends HasMany
20
{
21
22
    /**
23
     * @readwrite
24
     * @var string
25
     */
26
    protected $relatedForeignKey;
27
28
    /**
29
     * @readwrite
30
     * @var string
31
     */
32
    protected $relationTable;
33
34
    /**
35
     * Gets the related foreign key
36
     * 
37
     * @return string
38
     */
39
    public function getRelatedForeignKey()
40
    {
41
        return $this->relatedForeignKey;
42
    }
43
44
    /**
45
     * Gets the related table
46
     * 
47
     * @return string
48
     */
49 2
    public function getRelationTable()
50
    {
51 2
        if (is_null($this->relationTable)) {
52 2
            $parentTable = $this->getParentTableName();
53 2
            $table = $this->getEntityDescriptor()->getTableName();
54 2
            $names = [$parentTable, $table];
55 2
            asort($names);
56 2
            $first = array_shift($names);
57 2
            $tableName = Text::camelCaseToSeparator($first, '#');
58 2
            $parts = explode('#', $tableName);
59 2
            $lastName = array_pop($parts);
60 2
            $lastName = Text::singular(strtolower($lastName));
61 2
            array_push($parts, ucfirst($lastName));
62 2
            $tableName = lcfirst(implode('', $parts));
63 2
            array_unshift($names, $tableName);
64 2
            $this->relationTable = implode('_', $names);
65
66 2
        }
67 2
        return $this->relationTable;
68
    }
69
        
70
}