Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
ArrayPath
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
7 / 7
11
100.00% covered (success)
100.00%
1 / 1
 up
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 down
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 setCurrentPath
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getPaths
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCurrentIndex
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * @license MIT
5 * @author hazuki3417<hazuki3417@gmail.com>
6 * @copyright 2022 hazuki3417 all rights reserved.
7 */
8
9namespace Selen\Data;
10
11class ArrayPath
12{
13    private static string $separator = '.';
14
15    /** @var string[] */
16    private array $paths = [];
17
18    /** @var int 階層の深さ */
19    private int $currentIndex = 0;
20
21    private int $minCurrentIndex = 0;
22
23    /**
24     * 現在位置から1つ上の階層に移動します
25     *
26     * @return bool 成功した場合はtrueを、それ以外の場合はfalseを返します
27     */
28    public function up(): bool
29    {
30        if ($this->currentIndex <= $this->minCurrentIndex) {
31            return false;
32        }
33        unset($this->paths[$this->currentIndex]);
34        --$this->currentIndex;
35        return true;
36    }
37
38    /**
39     * 現在位置から1つ下の階層に移動します
40     *
41     * @return bool 常にtrueを返します
42     */
43    public function down(): bool
44    {
45        ++$this->currentIndex;
46
47        if (!isset($this->paths[$this->currentIndex])) {
48            $this->paths[$this->currentIndex] = '';
49        }
50
51        return true;
52    }
53
54    /**
55     * key名を設定します
56     *
57     * @param string $name key名を渡します
58     *
59     * @return bool 成功した場合はtrueを、それ以外の場合はfalseを返します
60     */
61    public function setCurrentPath(string $name)
62    {
63        if ($this->currentIndex <= $this->minCurrentIndex) {
64            return false;
65        }
66        $this->paths[$this->currentIndex] = $name;
67        return true;
68    }
69
70    /**
71     * 配列の階層情報を取得します。
72     *
73     * @return string[] 配列の階層情報を返します
74     */
75    public function getPaths(): array
76    {
77        return $this->paths;
78    }
79
80    /**
81     * 現在位置の階層を取得します
82     *
83     * @return int 現在位置の階層を返します
84     */
85    public function getCurrentIndex(): int
86    {
87        return $this->currentIndex;
88    }
89
90    /**
91     * 配列形式の階層表現を文字列形式に変換します
92     *
93     * @param string[] $paths 配列形式の階層表現配列を渡します
94     *
95     * @return string 文字列形式の階層表現文字列を返します
96     */
97    public static function toString(array $paths): string
98    {
99        return \implode(self::$separator, $paths);
100    }
101
102    /**
103     * 文字列形式の階層表現を配列形式に変換します
104     *
105     * @param string $path 文字列形式の階層表現文字列を渡します
106     *
107     * @return string[] 配列形式の階層表現配列を返します
108     */
109    public static function toArray(string $path): array
110    {
111        if ($path === '') {
112            return [];
113        }
114        return \explode(self::$separator, $path);
115    }
116}