ILIAS  release_8 Revision v8.24
class.ilTermsOfServiceAcceptanceHistoryCriteriaBag.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
32 public function __construct($data = [])
33 {
34 if (is_array($data)) {
36
37 parent::__construct(array_values(array_map(static function (
38 ilTermsOfServiceEvaluableCriterion $criterionAssignment
39 ): array {
40 return [
41 'id' => $criterionAssignment->getCriterionId(),
42 'value' => $criterionAssignment->getCriterionValue()
43 ];
44 }, $data)));
45 } else {
47
48 if (is_string($data)) {
49 $this->fromJson($data);
50 }
51 }
52 }
53
54 private function ensureValidArrayTypes(array $data): void
55 {
56 array_walk($data, static function ($value): void {
57 if (!($value instanceof ilTermsOfServiceEvaluableCriterion)) {
59 "Unexpected element found, given %s, expected instanceof '%s'",
60 var_export($value, true),
61 ilTermsOfServiceEvaluableCriterion::class
62 ));
63 }
64 });
65 }
66
67 private function ensureValidInternalTypes(array $data): void
68 {
69 array_walk($data, static function ($value): void {
70 if (!is_array($value)) {
72 'Unexpected element found, given %s, expected array',
73 var_export($value, true)
74 ));
75 }
76
77 if (!array_key_exists('id', $value) || !array_key_exists('value', $value) || count($value) !== 2) {
79 "Unexpected element found, given %s, expected array with keys 'id' and 'value'",
80 var_export($value, true)
81 ));
82 }
83 });
84 }
85
86 public function toJson(): string
87 {
88 return json_encode($this, JSON_THROW_ON_ERROR);
89 }
90
95 public function fromJson(string $json): void
96 {
97 $data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
98
99 if (!is_array($data)) {
101 'Unexpected element found, given %s, expected array',
102 var_export($data, true)
103 ));
104 }
105
107
108 $this->exchangeArray($data);
109 }
110
111 public function jsonSerialize(): array
112 {
113 return $this->getArrayCopy();
114 }
115}
__construct($data=[])
ilTermsOfServiceAcceptanceHistoryCriteriaBag constructor.
Interface ilTermsOfServiceEvaluableCriterion.
Interface ilTermsOfServiceJsonSerializable.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc