|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Config; |
| 15 | + |
| 16 | +use CodeIgniter\Exceptions\InvalidArgumentException; |
| 17 | + |
| 18 | +/** |
| 19 | + * Describes how a Registrar value should be merged into an existing |
| 20 | + * Config property. Interpreted when returned as the value of a config |
| 21 | + * property; nested directives are honored inside Merge::byKey(). |
| 22 | + * |
| 23 | + * @see \CodeIgniter\Config\BaseConfig |
| 24 | + */ |
| 25 | +final readonly class Merge |
| 26 | +{ |
| 27 | + /** |
| 28 | + * Discard the existing value and use the new one. |
| 29 | + */ |
| 30 | + public const REPLACE = 'replace'; |
| 31 | + |
| 32 | + /** |
| 33 | + * Add absent list items to the end of the existing value. |
| 34 | + */ |
| 35 | + public const APPEND = 'append'; |
| 36 | + |
| 37 | + /** |
| 38 | + * Add absent list items to the front of the existing value. |
| 39 | + */ |
| 40 | + public const PREPEND = 'prepend'; |
| 41 | + |
| 42 | + /** |
| 43 | + * Insert list items immediately before the anchor element. |
| 44 | + */ |
| 45 | + public const BEFORE = 'before'; |
| 46 | + |
| 47 | + /** |
| 48 | + * Insert list items immediately after the anchor element. |
| 49 | + */ |
| 50 | + public const AFTER = 'after'; |
| 51 | + |
| 52 | + /** |
| 53 | + * Deep-merge by key: string keys recurse, integer keys append, scalars replace. |
| 54 | + */ |
| 55 | + public const BY_KEY = 'byKey'; |
| 56 | + |
| 57 | + /** |
| 58 | + * @param self::AFTER|self::APPEND|self::BEFORE|self::BY_KEY|self::PREPEND|self::REPLACE $strategy |
| 59 | + * @param mixed $value Any value for REPLACE; array for the list strategies and BY_KEY. |
| 60 | + * @param mixed $anchor The element BEFORE/AFTER position against (matched strictly). |
| 61 | + */ |
| 62 | + private function __construct( |
| 63 | + public string $strategy, |
| 64 | + public mixed $value, |
| 65 | + public mixed $anchor = null, |
| 66 | + ) { |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Replace the existing value entirely (terminal: the payload is used |
| 71 | + * verbatim). Accepts any type, so it works for scalars too: |
| 72 | + * Merge::replace(false), Merge::replace('driver'), Merge::replace(null), |
| 73 | + * or arrays (e.g. ['a','b'] + ['c'] => ['c']). |
| 74 | + */ |
| 75 | + public static function replace(mixed $value): self |
| 76 | + { |
| 77 | + return new self(self::REPLACE, $value); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Append absent list items to the end of the existing value |
| 82 | + * (e.g. ['a','b'] + ['b','c'] => ['a','b','c']). Values already present are |
| 83 | + * left where they are. The payload is literal - for nested control, use |
| 84 | + * byKey() rather than nesting directives in an append() payload. List keys |
| 85 | + * are not preserved: the value is treated as a list. |
| 86 | + * |
| 87 | + * @param list<mixed> $value |
| 88 | + */ |
| 89 | + public static function append(array $value): self |
| 90 | + { |
| 91 | + return new self(self::APPEND, $value); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Prepend absent list items to the front of the existing value |
| 96 | + * (e.g. ['a','b'] + ['c'] => ['c','a','b']). Values already present are left |
| 97 | + * where they are. List keys are not preserved: the value is treated as a list. |
| 98 | + * |
| 99 | + * @param list<mixed> $value |
| 100 | + */ |
| 101 | + public static function prepend(array $value): self |
| 102 | + { |
| 103 | + return new self(self::PREPEND, $value); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Insert list items immediately before the first element equal (===) to |
| 108 | + * $anchor. An already-present value is moved to this position. If the anchor |
| 109 | + * is not in the list this falls back to prepend() and does not relocate |
| 110 | + * already-present values. List keys are not preserved. |
| 111 | + * |
| 112 | + * @param list<mixed> $value |
| 113 | + * |
| 114 | + * @throws InvalidArgumentException if $anchor is also one of the inserted values. |
| 115 | + */ |
| 116 | + public static function before(mixed $anchor, array $value): self |
| 117 | + { |
| 118 | + self::assertAnchorNotInPayload($anchor, $value, self::BEFORE); |
| 119 | + |
| 120 | + return new self(self::BEFORE, $value, $anchor); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Insert list items immediately after the first element equal (===) to |
| 125 | + * $anchor. An already-present value is moved to this position. If the anchor |
| 126 | + * is not in the list this falls back to append() and does not relocate |
| 127 | + * already-present values. List keys are not preserved. |
| 128 | + * |
| 129 | + * @param list<mixed> $value |
| 130 | + * |
| 131 | + * @throws InvalidArgumentException if $anchor is also one of the inserted values. |
| 132 | + */ |
| 133 | + public static function after(mixed $anchor, array $value): self |
| 134 | + { |
| 135 | + self::assertAnchorNotInPayload($anchor, $value, self::AFTER); |
| 136 | + |
| 137 | + return new self(self::AFTER, $value, $anchor); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Guards against anchoring a before()/after() insert on a value that is also |
| 142 | + * being inserted. That request is contradictory - the anchor would be removed |
| 143 | + * by de-duplication before it could be located - so it is rejected outright. |
| 144 | + * |
| 145 | + * @param list<mixed> $value |
| 146 | + */ |
| 147 | + private static function assertAnchorNotInPayload(mixed $anchor, array $value, string $strategy): void |
| 148 | + { |
| 149 | + if (in_array($anchor, $value, true)) { |
| 150 | + throw new InvalidArgumentException( |
| 151 | + 'Merge::' . $strategy . '() cannot use a value that is also being inserted as its anchor.', |
| 152 | + ); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Deep-merge into the existing value by key: associative (string) keys are |
| 158 | + * merged/recursed, list (integer) keys append, scalar leaves are replaced. |
| 159 | + * Nested Merge directives ARE honored within the payload. Named byKey() to |
| 160 | + * distance it from PHP's array_merge_recursive(), which collects scalars |
| 161 | + * into arrays. |
| 162 | + * |
| 163 | + * @param array<array-key, mixed> $value |
| 164 | + */ |
| 165 | + public static function byKey(array $value): self |
| 166 | + { |
| 167 | + return new self(self::BY_KEY, $value); |
| 168 | + } |
| 169 | +} |
0 commit comments