Test Failed
Push — master ( 0bc352...8c447f )
by Todd
03:54
created

IsInstanceOf::isValidClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Logikos\Util\Validation\Validator;
4
5
use Logikos\Util\Validation\Validator;
6
7
class IsInstanceOf implements Validator {
8
  private $fqcn;
9
  private $description;
10
11
  /**
12
   * @param string $fqcn
13
   * @param string $description
14
   * @throws Exception
15
   */
16
  public function __construct($fqcn, $description = null) {
17
    if (!$this->isValidClassName($fqcn))
18
      throw new Exception("Invalid Regex pattern: {$fqcn}");
19
20
    $this->fqcn = $fqcn;
21
    $this->description = $description ?? "Must be an instance of {$fqcn}";
22
  }
23
24
  public function getFqcn() {
25
    return $this->fqcn;
26
  }
27
28
  public function validate($value) : bool {
29
    return is_a($value, $this->fqcn);
30
  }
31
32
  protected function isValidClassName($fqcn) {
33
    return class_exists($fqcn);
34
  }
35
36
  public function getDescription() {
37
    return $this->description;
38
  }
39
}