• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ICanBoogie / ActiveRecord / 8587980914

07 Apr 2024 10:12AM UTC coverage: 85.614% (+1.7%) from 83.927%
8587980914

push

github

olvlvl
Tidy Query

13 of 22 new or added lines in 3 files covered. (59.09%)

116 existing lines in 8 files now uncovered.

1345 of 1571 relevant lines covered (85.61%)

24.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.22
/lib/ActiveRecord/Validate/Validator/Unique.php
1
<?php
2

3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace ICanBoogie\ActiveRecord\Validate\Validator;
13

14
use ICanBoogie\ActiveRecord;
15
use ICanBoogie\ActiveRecord\Validate\Reader\RecordAdapter;
16
use ICanBoogie\Validate\Context;
17
use ICanBoogie\Validate\Validator\ValidatorAbstract;
18
use RuntimeException;
19

20
/**
21
 * Validates that a value is unique in a table's column.
22
 */
23
class Unique extends ValidatorAbstract
24
{
25
    public const string ALIAS = 'unique';
26
    public const string DEFAULT_MESSAGE = '`{value}` is already used';
27

28
    /**
29
     * Specify the column to check, otherwise `attribute` is used.
30
     */
31
    public const string OPTION_COLUMN = 'column';
32

33
    /**
34
     * @inheritdoc
35
     */
36
    public function validate($value, Context $context): bool
37
    {
38
        $column = $context->option(self::OPTION_COLUMN, $context->attribute)
1✔
39
            ?? throw new RuntimeException("Unable to resolve column from context option OPTION_COLUMN");
×
40
        $record = $this->resolve_record($context);
1✔
41
        $model = $record->model;
1✔
42
        $where = [ $column => $value ];
1✔
43
        $primary = $model->primary;
1✔
44

45
        assert(is_string($primary));
46

47
        if (!empty($record->$primary)) {
1✔
48
            $where['!' . $primary] = $record->$primary;
1✔
49
        }
50

51
        $a = $model->where($where)->all;
1✔
52

53
        return !$model->where($where)->exists;
1✔
54
    }
55

56
    /**
57
     * @inheritdoc
58
     */
59
    protected function get_params_mapping() // @phpstan-ignore-line
60
    {
61
        return [ self::OPTION_COLUMN ];
1✔
62
    }
63

64
    private function resolve_record(Context $context): ActiveRecord
65
    {
66
        $reader = $context->reader;
1✔
67

68
        if (!$reader instanceof RecordAdapter) {
1✔
69
            throw new \InvalidArgumentException(sprintf(
×
70
                "Expected `context.reader` to be an instance of `%s`.",
×
UNCOV
71
                RecordAdapter::class
×
UNCOV
72
            ));
×
73
        }
74

75
        return $reader->record;
1✔
76
    }
77
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc