Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
expenses.php
1<?php
2
4
6{
7 protected $data;
8
13 public function __construct(array $data = [])
14 {
15 $this->prepareData($data);
16 }
17
22 public function add(array $data)
23 {
24 foreach($this->getNumericFieldNames() as $name)
25 {
26 if(isset($data[$name]))
27 {
28 $value = $data[$name];
29 if (is_array($value))
30 {
31 $value = array_sum(array_map(
32 function ($value)
33 {
34 return is_numeric($value) ? $value : 0;
35 },
36 array_column($value, 'value')
37 ));
38 }
39 if (is_numeric($value))
40 {
41 $this->data[$name] += $value;
42 }
43 }
44 }
45 if(isset($data['currency']) && empty($this->data['currency']))
46 {
47 $this->data['currency'] = $data['currency'];
48 }
49
50 return $this;
51 }
52
56 public function toArray()
57 {
58 return $this->data;
59 }
60
64 public function getImpressions()
65 {
66 return $this->data['impressions'];
67 }
68
72 public function getClicks()
73 {
74 return $this->data['clicks'];
75 }
76
80 public function getActions()
81 {
82 return $this->data['actions'];
83 }
84
88 public function getCpc()
89 {
90 return $this->data['cpc'];
91 }
92
98 public function getCpm()
99 {
100 return $this->data['cpm'];
101 }
102
106 public function getSpend()
107 {
108 return $this->data['spend'];
109 }
110
114 public function getCurrency()
115 {
116 return $this->data['currency'];
117 }
118
122 protected function prepareData(array $data)
123 {
124 $this->data = [
125 'impressions' => 0,
126 'clicks' => 0,
127 'actions' => 0,
128 'cpc' => 0,
129 'cpm' => 0,
130 'spend' => 0,
131 'currency' => '',
132 ];
133
134 $this->data = array_merge($this->data, $data);
135 }
136
140 protected function getNumericFieldNames()
141 {
142 return ['impressions', 'clicks', 'actions', 'cpc', 'cpm', 'spend'];
143 }
144}