ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilTermsOfServiceAcceptanceHistoryCriteriaBag.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
9 {
15  public function __construct($data = [])
16  {
17  if (is_array($data)) {
19 
20  parent::__construct(array_values(array_map(function (\ilTermsOfServiceEvaluableCriterion $criterionAssignment) {
21  return [
22  'id' => $criterionAssignment->getCriterionId(),
23  'value' => $criterionAssignment->getCriterionValue()
24  ];
25  }, $data)));
26  } else {
27  parent::__construct([]);
28 
29  if (is_string($data)) {
30  $this->fromJson($data);
31  }
32  }
33  }
34 
39  private function ensureValidArrayTypes(array $data)
40  {
41  array_walk($data, function ($value) {
42  if (!($value instanceof \ilTermsOfServiceEvaluableCriterion)) {
43  throw new \ilTermsOfServiceUnexpectedCriteriaBagContentException(sprintf(
44  "Unexpected element found, given %s, expected instanceof '%s'",
45  var_export($value, 1),
46  \ilTermsOfServiceEvaluableCriterion::class
47  ));
48  }
49  });
50  }
51 
55  private function ensureValidInternalTypes(array $data)
56  {
57  array_walk($data, function ($value) {
58  if (!is_array($value)) {
59  throw new \ilTermsOfServiceUnexpectedCriteriaBagContentException(sprintf(
60  "Unexpected element found, given %s, expected array",
61  var_export($value, 1)
62  ));
63  }
64 
65  if (count($value) !== 2 || !array_key_exists('id', $value) || !array_key_exists('value', $value)) {
66  throw new \ilTermsOfServiceUnexpectedCriteriaBagContentException(sprintf(
67  "Unexpected element found, given %s, expected array with keys 'id' and 'value'",
68  var_export($value, 1)
69  ));
70  }
71  });
72  }
73 
77  public function toJson() : string
78  {
79  $json = json_encode($this);
80 
81  return $json;
82  }
83 
88  public function fromJson(string $json)
89  {
90  $data = json_decode($json, true);
91 
92  if (!is_array($data)) {
93  throw new \ilTermsOfServiceUnexpectedCriteriaBagContentException(sprintf(
94  "Unexpected element found, given %s, expected array",
95  var_export($data, 1)
96  ));
97  }
98 
100 
101  $this->exchangeArray($data);
102  }
103 
107  public function jsonSerialize()
108  {
109  return $this->getArrayCopy();
110  }
111 }
Interface ilTermsOfServiceEvaluableCriterion.
Interface ilTermsOfServiceJsonSerializable.
__construct($data=[])
ilTermsOfServiceAcceptanceHistoryCriteriaBag constructor.
$data
Definition: bench.php:6