Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ajaxjson.php
1<?php
2
4
5
9
10class AjaxJson extends Json implements Errorable
11{
12 const STATUS_SUCCESS = 'success';
13 const STATUS_DENIED = 'denied';
14 const STATUS_ERROR = 'error';
18 private $status;
22 private $errorCollection;
23
24 public function __construct($data = null, $status = self::STATUS_SUCCESS, ErrorCollection $errorCollection = null)
25 {
26 $this->status = $status?: self::STATUS_SUCCESS;
27 $this->errorCollection = $errorCollection?: new ErrorCollection;
28
29 parent::__construct($data);
30 }
31
32 final public static function createSuccess($data = null)
33 {
34 return new self($data, self::STATUS_SUCCESS, null);
35 }
36
37 final public static function createError(ErrorCollection $errorCollection = null, $data = null)
38 {
39 return new self($data, self::STATUS_ERROR, $errorCollection);
40 }
41
42 final public static function createDenied(ErrorCollection $errorCollection = null, $data = null)
43 {
44 return new self($data, self::STATUS_DENIED, $errorCollection);
45 }
46
47 final public function setData($data)
48 {
54 return parent::setData(
55 array(
56 'status' => $this->status,
57 'data' => $data,
58 'errors' => $this->getErrorsToResponse(),
59 )
60 );
61 }
62
63 final protected function getErrorsToResponse()
64 {
65 $errors = array();
66 foreach ($this->errorCollection as $error)
67 {
69 $errors[] = $error;
70 }
71
72 return $errors;
73 }
74
79 final public function getErrors()
80 {
81 return $this->errorCollection->toArray();
82 }
83
89 final public function getErrorByCode($code)
90 {
91 return $this->errorCollection->getErrorByCode($code);
92 }
93}
static createDenied(ErrorCollection $errorCollection=null, $data=null)
Definition ajaxjson.php:42
__construct($data=null, $status=self::STATUS_SUCCESS, ErrorCollection $errorCollection=null)
Definition ajaxjson.php:24
static createError(ErrorCollection $errorCollection=null, $data=null)
Definition ajaxjson.php:37