Completed
Push — master ( 0ca994...bcdd07 )
by Neomerx
05:23
created

RelationshipsTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toOneRelationship() 0 6 2
A toManyRelationship() 0 6 2
1
<?php namespace Limoncello\Flute\Validation\Rules;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Validation\Contracts\Rules\RuleInterface;
20
use Limoncello\Validation\Rules\Generic\AndOperator;
21
22
/**
23
 * @package Limoncello\Flute
24
 */
25
trait RelationshipsTrait
26
{
27
    /**
28
     * @param string             $type
29
     * @param RuleInterface|null $rule
30
     *
31
     * @return RuleInterface
32
     */
33
    public static function toOneRelationship(string $type, RuleInterface $rule = null): RuleInterface
34
    {
35
        $primary = new ToOneRelationshipTypeChecker($type);
36
37
        return $rule === null ? $primary : new AndOperator($primary, $rule);
38
    }
39
40
    /**
41
     * @param string             $type
42
     * @param RuleInterface|null $rule
43
     *
44
     * @return RuleInterface
45
     */
46
    public static function toManyRelationship(string $type, RuleInterface $rule = null): RuleInterface
47
    {
48
        $primary = new ToManyRelationshipTypeChecker($type);
49
50
        return $rule === null ? $primary : new AndOperator($primary, $rule);
51
    }
52
}
53