Passed
Pull Request — master (#19)
by Jitendra
02:10
created

Existence::failed()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 1
nop 2
1
<?php
2
3
namespace PhalconExt\Validation;
4
5
use Phalcon\Validation;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, PhalconExt\Validation\Validation. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
Bug introduced by
The type Phalcon\Validation was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Phalcon\Validation\Message;
0 ignored issues
show
Bug introduced by
The type Phalcon\Validation\Message was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Phalcon\Validation\Validator;
0 ignored issues
show
Bug introduced by
The type Phalcon\Validation\Validator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use PhalconExt\Di\ProvidesDi;
9
10
/**
11
 * Check that a field exists in the related table.
12
 *
13
 * @author  Jitendra Adhikari <[email protected]>
14
 * @license MIT
15
 *
16
 * @link    https://github.com/adhocore/phalcon-ext
17
 */
18
class Existence extends Validator
19
{
20
    use ProvidesDi;
21
22
    /**
23
     * Executes the validation.
24
     *
25
     * @param validation $validation
26
     * @param string     $field
27
     */
28
    public function validate(Validation $validation, $field) : bool
29
    {
30
        $options = $this->_options + [
31
            'table'  => $field,
32
            'column' => isset($this->_options['table']) ? $field : 'id',
33
        ];
34
35
        $count = $this->di('db')->countBy($options['table'], [
36
            $options['column'] => $validation->getValue($field),
37
        ]);
38
39
        if ($count > 0) {
40
            return true;
41
        }
42
43
        return $this->failed($validation, $field);
44
    }
45
46
    public function failed(Validation $validation, $field): bool
47
    {
48
        $label = $this->getOption('label') ?: $validation->getLabel($field);
49
        $error = $this->getOption('message') ?: $validation->getDefaultMessage('Existence');
50
        $error = \strtr($error, [':field' => $label]);
51
52
        $validation->appendMessage(
53
            new Message($error, $field, 'Existence', $this->getOption('code'))
54
        );
55
56
        return false;
57
    }
58
}
59