3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【PMMP】ConfigのgetNestedってナンダ

Last updated at Posted at 2022-12-18

初心者の頃からずっと気になってて夜しか眠れかった。
ConfigクラスのsetNested getNested removeNested

ソースコードを見てなんとなく動作は察しが付いてたけど、今更になって動作確認をした。
まあネストですよね。それ以上でもそれ以下でもない。

前提のconfig

config.yml
foo:
  bar: "deceitya"

getNested

キーを.で繋ぐだけです。setNestedもremoveNestedも同様。

Main.php
$this->getConfig()->getNested("foo.bar");  // deceitya

setNested

Main.php
$this->getConfig()->setNested("foo.bar", "dededeitya");
$this->getConfig()->setNested("hoge.huga", "tsukimiya");
$this->getConfig()->setNested("hoge.fuga", "tsukimiya2nd");
$this->getConfig()->setNested("server.mcbe.jp", ["deceitya", "ツキミヤ", "MCハッピー月宮"]);
$this->getConfig()->save();
実行後のconfig.yml
foo:
  bar: dededeitya
hoge:
  huga: tsukimiya
  fuga: tsukimiya2nd
server:
  mcbe:
    jp:
    - deceitya
    - ツキミヤ
    - MCハッピー月宮

removeNested

Main.php
$this->getConfig()->removeNested("hoge.huga");
$this->getConfig()->removeNested("server.mcbe.jp.1");
$this->getConfig()->save();
実行後のconfig.yml
foo:
  bar: dededeitya
hoge:
  fuga: tsukimiya2nd
server:
  mcbe:
    jp:
      0: deceitya
      2: MCハッピー月宮

hoge.fugaのtsukimiyaとserver.mcbe.jp[1]のツキミヤが消えましたね。
配列の要素を削除すると-で表現されてた配列がキーの番号に変わるようです。

令和コソコソ噂話

12/15は @ribnil さんの誕生日だそうで。
去年のアドベントカレンダーは誕生日をご自身で担当されてたから、今年もてっきり担当するのかと(笑)(笑)

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?