ILIAS  trunk Revision v12.0_alpha-399-g579a087ced2
NewsContext.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\News\Data;
22
28final class NewsContext
29{
30 public function __construct(
31 private readonly int $ref_id,
32 private ?int $obj_id = null,
33 private ?string $obj_type = null,
34 private ?int $parent_ref_id = null,
35 private int $level = 0,
36 ) {
37 }
38
39 /*
40 Getters & Setters
41 */
42
43 public function getRefId(): int
44 {
45 return $this->ref_id;
46 }
47
48 public function getObjId(): ?int
49 {
50 return $this->obj_id;
51 }
52
53 public function getObjType(): ?string
54 {
55 return $this->obj_type;
56 }
57
58 public function getParentRefId(): ?int
59 {
60 return $this->parent_ref_id;
61 }
62
63 public function setObjId(int $obj_id): self
64 {
65 $this->obj_id = $obj_id;
66 return $this;
67 }
68
69 public function setObjType(string $obj_type): self
70 {
71 $this->obj_type = $obj_type;
72 return $this;
73 }
74
75 public function setParentRefId(?int $parent_ref_id): self
76 {
77 $this->parent_ref_id = $parent_ref_id;
78 return $this;
79 }
80
81 public function setLevel(int $level): self
82 {
83 $this->level = $level;
84 return $this;
85 }
86
91 public function getLevel(): int
92 {
93 return $this->level;
94 }
95
96 /*
97 Accessor Methods
98 */
99
103 public function isChildOf(NewsContext $parent_context): bool
104 {
105 return $this->parent_ref_id === $parent_context->getRefId();
106 }
107
111 public function isParentOf(NewsContext $child_context): bool
112 {
113 return $child_context->getParentRefId() === $this->ref_id;
114 }
115
119 public function isRoot(): bool
120 {
121 return $this->parent_ref_id === null;
122 }
123
124 /*
125 Optimized Serializing
126 */
127
133 public function normalize(): array
134 {
135 $vars = get_object_vars($this);
136 return array_filter($vars);
137 }
138
145 public static function denormalize(array $raw): self
146 {
147 return new self(
148 $raw['ref_id'],
149 $raw['obj_id'] ?? null,
150 $raw['obj_type'] ?? null,
151 $raw['parent_ref_id'] ?? null,
152 $raw['level'] ?? 0,
153 );
154 }
155}
News Context DTO represents a context where news items can be associated with.
Definition: NewsContext.php:29
isChildOf(NewsContext $parent_context)
Check if this context is a child of another context.
setObjType(string $obj_type)
Definition: NewsContext.php:69
normalize()
Transform this object into array representation and keep only properties which are not default values...
setParentRefId(?int $parent_ref_id)
Definition: NewsContext.php:75
isRoot()
Check if this context is at the root level.
__construct(private readonly int $ref_id, private ?int $obj_id=null, private ?string $obj_type=null, private ?int $parent_ref_id=null, private int $level=0,)
Definition: NewsContext.php:30
isParentOf(NewsContext $child_context)
Check if this context is a parent of another context.
static denormalize(array $raw)
Create new object from reduced array representation.
$ref_id
Definition: ltiauth.php:66