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

ICanBoogie / ActiveRecord / 4437457574

pending completion
4437457574

push

github

Olivier Laviale
Case of a nullable belong_to

7 of 7 new or added lines in 1 file covered. (100.0%)

1356 of 1686 relevant lines covered (80.43%)

34.68 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 ALIAS = 'unique';
26
    public const DEFAULT_MESSAGE = '`{value}` is already used';
27

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

33
    /**
34
     * @inheritdoc
35
     */
36
    public function validate($value, Context $context)
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
        if (!empty($record->$primary)) {
1✔
46
            $where['!' . $primary] = $record->$primary;
1✔
47
        }
48

49
        $a = $model->where($where)->all;
1✔
50

51
        return !$model->where($where)->exists;
1✔
52
    }
53

54
    /**
55
     * @inheritdoc
56
     */
57
    protected function get_params_mapping()
58
    {
59
        return [ self::OPTION_COLUMN ];
1✔
60
    }
61

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

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

73
        return $reader->record;
1✔
74
    }
75
}
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