GetAssociationTrait::getAssociation()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 9
rs 10
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2022 Atlas Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
16
namespace BEdita\Placeholders\Model\Behavior;
17
18
use Cake\ORM\Association;
19
use Cake\Utility\Inflector;
20
21
/**
22
 * Trait to get association from relation name.
23
 */
24
trait GetAssociationTrait
25
{
26
    /**
27
     * Getter for Table object.
28
     *
29
     * @return \Cake\ORM\Table
30
     */
31
    abstract public function table();
32
33
    /**
34
     * Get association for a relation.
35
     *
36
     * @param string $relation Relation name.
37
     * @return \Cake\ORM\Association|null
38
     */
39
    protected function getAssociation(string $relation): ?Association
40
    {
41
        $name = Inflector::camelize($relation);
42
        $table = $this->table();
43
        if (!$table->hasAssociation($name)) {
44
            return null;
45
        }
46
47
        return $table->getAssociation($name);
48
    }
49
}
50