Completed
Pull Request — 5.1 (#2266)
by
unknown
12:07
created

EntityValidator::validate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace Kunstmaan\GeneratorBundle\Helper;
4
5
use Symfony\Component\HttpKernel\Kernel;
6
7
/**
8
 * @internal
9
 */
10
final class EntityValidator
11
{
12
    /**
13
     * Performs basic checks in entity name.
14
     *
15
     * @param string $entity
16
     *
17
     * @return string
18
     *
19
     * @throws \InvalidArgumentException
20
     */
21
    public static function validate($entity)
22
    {
23
        if (Kernel::VERSION_ID >= 40000) {
24
            $classFound = class_exists($entity, true);
25
26
            if (!$classFound) {
27
                throw new \InvalidArgumentException(sprintf('Entity "%s" was not found', $entity));
28
            }
29
30
            return $entity;
31
        }
32
33
        if (!preg_match('{^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*:[a-zA-Z0-9_\x7f-\xff\\\/]+$}', $entity)) {
34
            throw new \InvalidArgumentException(sprintf('The entity name isn\'t valid ("%s" given, expecting something like AcmeBlogBundle:Blog/Post)', $entity));
35
        }
36
37
        return $entity;
38
    }
39
}
40