ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
CrawlerAssertion.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30{
31 protected ?Factory $f = null;
32
33 public function __construct()
34 {
35 $this->f = new Factory();
36 }
37
42 public function isArray($array): void
43 {
44 if (!is_array($array)) {
45 throw $this->f->exception(CrawlerException::ARRAY_EXPECTED, $array);
46 }
47 }
48
53 public function isString($string, bool $allow_empty = true): void
54 {
55 if (!is_string($string)) {
56 if (is_array($string)) {
57 $string = json_encode($string);
58 }
59 throw $this->f->exception(CrawlerException::STRING_EXPECTED, (string) $string);
60 }
61 if (!$allow_empty && $string == "") {
62 throw $this->f->exception(CrawlerException::EMPTY_STRING, (string) $string);
63 }
64 }
65
70 public function isIndex($index, array $array): void
71 {
72 if (!array_key_exists($index, $array)) {
73 throw $this->f->exception(CrawlerException::INVALID_INDEX, strval($index));
74 }
75 }
76
81 public function isNotIndex($index, array $array): void
82 {
83 if (array_key_exists($index, $array)) {
84 throw $this->f->exception(CrawlerException::DUPLICATE_ENTRY, strval($index));
85 }
86 }
87
92 public function hasIndex(array $array, $index): void
93 {
94 if (!array_key_exists($index, $array)) {
95 throw $this->f->exception(CrawlerException::MISSING_INDEX, strval($index));
96 }
97 }
98
103 public function isTypeOf($element, string $class_name): void
104 {
105 if (!get_class($element) == $class_name) {
106 throw $this->f->exception(
108 "Expected: " . $class_name . " got " . get_class($element)
109 );
110 }
111 }
112}
Tests properties and throws exceptions if not met.