Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
errorcollection.php
1<?php
2
4
6use Bitrix\Main\Entity\Result;
8
9final class ErrorCollection extends Dictionary
10{
16 public function __construct(array $values = null)
17 {
18 if($values)
19 {
20 foreach($values as $value)
21 {
22 $this->checkType($value);
23 }
24 }
25 unset($value);
26
27 parent::__construct($values);
28 }
29
35 public function add(array $errors)
36 {
37 foreach ($errors as $error)
38 {
39 $this[] = $error;
40 }
41 unset($error);
42 }
43
49 public function addOne(Error $error)
50 {
51 $this[] = $error;
52 }
53
59 public function addFromResult(Result $result)
60 {
61 $errors = array();
62 foreach ($result->getErrorMessages() as $message)
63 {
64 $errors[] = new Error($message);
65 }
66 unset($message);
67
68 $this->add($errors);
69 }
70
75 public function hasErrors()
76 {
77 return (bool)count($this);
78 }
79
85 public function getErrorsByCode($code)
86 {
87 $needle = array();
88 foreach($this->values as $error)
89 {
91 if($error->getCode() == $code)
92 {
93 $needle[] = $error;
94 }
95 }
96 unset($error);
97
98 return $needle;
99 }
100
106 public function getErrorByCode($code)
107 {
108 foreach($this->values as $error)
109 {
111 if($error->getCode() == $code)
112 {
113 return $error;
114 }
115 }
116 unset($error);
117
118 return null;
119 }
120
128 public function offsetSet($offset, $value)
129 {
130 $this->checkType($value);
131 parent::offsetSet($offset, $value);
132 }
133
138 private function checkType($value)
139 {
140 if(!$value instanceof Error)
141 {
142 throw new ArgumentTypeException('Could not push in ErrorCollection non Error.');
143 }
144 }
145}