Completed
Push — develop ( e8233d...5c8fb2 )
by Neomerx
16:15 queued 11:26
created

DatabaseRulesTrait::exists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 3
crap 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\Flute\Validation\JsonApi\Rules\ExistInDbTableMultipleWithDoctrineRule;
20
use Limoncello\Flute\Validation\JsonApi\Rules\ExistInDbTableSingleWithDoctrineRule;
21
use Limoncello\Flute\Validation\JsonApi\Rules\UniqueInDbTableSingleWithDoctrineRule;
22
use Limoncello\Validation\Contracts\Rules\RuleInterface;
23
use Limoncello\Validation\Rules\Generic\AndOperator;
24
25
/**
26
 * @package Limoncello\Flute
27
 */
28
trait DatabaseRulesTrait
29
{
30
    /**
31
     * @param string             $tableName
32
     * @param string             $primaryName
33
     * @param RuleInterface|null $next
34
     *
35
     * @return RuleInterface
36
     */
37 28
    public static function exists(string $tableName, string $primaryName, RuleInterface $next = null): RuleInterface
38
    {
39 28
        $primary = new ExistInDbTableSingleWithDoctrineRule($tableName, $primaryName);
40
41 28
        return $next === null ? $primary : new AndOperator($primary, $next);
42
    }
43
44
    /**
45
     * @param string             $tableName
46
     * @param string             $primaryName
47
     * @param RuleInterface|null $next
48
     *
49
     * @return RuleInterface
50
     */
51 28
    public static function existAll(string $tableName, string $primaryName, RuleInterface $next = null): RuleInterface
52
    {
53 28
        $primary = new ExistInDbTableMultipleWithDoctrineRule($tableName, $primaryName);
54
55 28
        return $next === null ? $primary : new AndOperator($primary, $next);
56
    }
57
58
    /**
59
     * @param string             $tableName
60
     * @param string             $primaryName
61
     * @param RuleInterface|null $next
62
     *
63
     * @return RuleInterface
64
     */
65 2
    public static function unique(string $tableName, string $primaryName, RuleInterface $next = null): RuleInterface
66
    {
67 2
        $primary = new UniqueInDbTableSingleWithDoctrineRule($tableName, $primaryName);
68
69 2
        return $next === null ? $primary : new AndOperator($primary, $next);
70
    }
71
}
72