Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Context/ContextBuilderTrait.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Serializer\Context;
13
14 /**
15 * @author Mathias Arlaud <[email protected]>
16 */
17 trait ContextBuilderTrait
18 {
19 /**
20 * @var array<string, mixed>
21 */
22 private array $context = [];
23
24 protected function with(string $key, mixed $value): static
25 {
26 $instance = new static();
27 $instance->context = array_merge($this->context, [$key => $value]);
28
29 return $instance;
30 }
31
32 /**
33 * @param ContextBuilderInterface|array<string, mixed> $context
34 */
35 public function withContext(ContextBuilderInterface|array $context): static
36 {
37 if ($context instanceof ContextBuilderInterface) {
38 $context = $context->toArray();
39 }
40
41 $instance = new static();
42 $instance->context = array_merge($this->context, $context);
43
44 return $instance;
45 }
46
47 /**
48 * @return array<string, mixed>
49 */
50 public function toArray(): array
51 {
52 return $this->context;
53 }
54 }