Passed
Push — develop ( bca4b6...66c5e1 )
by Schlaefer
03:54
created

SaitoValidationProvider::validateAssoc()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 3
dl 0
loc 13
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Saito\Validation;
14
15
class SaitoValidationProvider
16
{
17
    /**
18
     * validator checking if value is unique case insensitive
19
     *
20
     * simplified version Cake\ORM\Table\validateUnique and ORM\Rule\isUnique
21
     *
22
     * @param string $value value
23
     * @param array $context context
24
     * @return bool
25
     */
26
    public static function validateIsUniqueCiString($value, array $context)
27
    {
28
        $field = $context['field'];
29
        $Table = $context['providers']['table'];
30
        $primaryKey = $Table->getPrimaryKey();
31
32
        $conditions = ["LOWER($field)" => mb_strtolower($value)];
33
        if ($context['newRecord'] === false) {
34
            $conditions['NOT'] = [$primaryKey => $context['data'][$primaryKey]];
35
        }
36
37
        return !$Table->exists($conditions);
38
    }
39
}
40