|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Testing\Fluent; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Illuminate\Contracts\Support\Arrayable; |
| 7 | +use Illuminate\Support\Arr; |
| 8 | +use Illuminate\Support\Traits\Macroable; |
| 9 | +use Illuminate\Support\Traits\Tappable; |
| 10 | +use Illuminate\Testing\AssertableJsonString; |
| 11 | +use PHPUnit\Framework\Assert as PHPUnit; |
| 12 | + |
| 13 | +class AssertableJson implements Arrayable |
| 14 | +{ |
| 15 | + use Concerns\Has, |
| 16 | + Concerns\Matching, |
| 17 | + Concerns\Debugging, |
| 18 | + Concerns\Interaction, |
| 19 | + Macroable, |
| 20 | + Tappable; |
| 21 | + |
| 22 | + /** |
| 23 | + * The properties in the current scope. |
| 24 | + * |
| 25 | + * @var array |
| 26 | + */ |
| 27 | + private $props; |
| 28 | + |
| 29 | + /** |
| 30 | + * The "dot" path to the current scope. |
| 31 | + * |
| 32 | + * @var string|null |
| 33 | + */ |
| 34 | + private $path; |
| 35 | + |
| 36 | + /** |
| 37 | + * Create a new fluent, assertable JSON data instance. |
| 38 | + * |
| 39 | + * @param array $props |
| 40 | + * @param string|null $path |
| 41 | + * @return void |
| 42 | + */ |
| 43 | + protected function __construct(array $props, string $path = null) |
| 44 | + { |
| 45 | + $this->path = $path; |
| 46 | + $this->props = $props; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Compose the absolute "dot" path to the given key. |
| 51 | + * |
| 52 | + * @param string $key |
| 53 | + * @return string |
| 54 | + */ |
| 55 | + protected function dotPath(string $key): string |
| 56 | + { |
| 57 | + if (is_null($this->path)) { |
| 58 | + return $key; |
| 59 | + } |
| 60 | + |
| 61 | + return implode('.', [$this->path, $key]); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Retrieve a prop within the current scope using "dot" notation. |
| 66 | + * |
| 67 | + * @param string|null $key |
| 68 | + * @return mixed |
| 69 | + */ |
| 70 | + protected function prop(string $key = null) |
| 71 | + { |
| 72 | + return Arr::get($this->props, $key); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Instantiate a new "scope" at the path of the given key. |
| 77 | + * |
| 78 | + * @param string $key |
| 79 | + * @param \Closure $callback |
| 80 | + * @return $this |
| 81 | + */ |
| 82 | + protected function scope(string $key, Closure $callback): self |
| 83 | + { |
| 84 | + $props = $this->prop($key); |
| 85 | + $path = $this->dotPath($key); |
| 86 | + |
| 87 | + PHPUnit::assertIsArray($props, sprintf('Property [%s] is not scopeable.', $path)); |
| 88 | + |
| 89 | + $scope = new self($props, $path); |
| 90 | + $callback($scope); |
| 91 | + $scope->interacted(); |
| 92 | + |
| 93 | + return $this; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Create a new instance from an array. |
| 98 | + * |
| 99 | + * @param array $data |
| 100 | + * @return static |
| 101 | + */ |
| 102 | + public static function fromArray(array $data): self |
| 103 | + { |
| 104 | + return new self($data); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Create a new instance from a AssertableJsonString. |
| 109 | + * |
| 110 | + * @param \Illuminate\Testing\AssertableJsonString $json |
| 111 | + * @return static |
| 112 | + */ |
| 113 | + public static function fromAssertableJsonString(AssertableJsonString $json): self |
| 114 | + { |
| 115 | + return self::fromArray($json->json()); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Get the instance as an array. |
| 120 | + * |
| 121 | + * @return array |
| 122 | + */ |
| 123 | + public function toArray() |
| 124 | + { |
| 125 | + return $this->props; |
| 126 | + } |
| 127 | +} |
0 commit comments