|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Symfony package. |
| 7 | + * |
| 8 | + * (c) Fabien Potencier <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Symfony\UX\Threejs\Live; |
| 15 | + |
| 16 | +use Symfony\UX\Threejs\Three; |
| 17 | +use Symfony\UX\LiveComponent\Attribute\LiveProp; |
| 18 | +use Symfony\UX\TwigComponent\Attribute\PostMount; |
| 19 | +use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Hugo Alliaume <[email protected]> |
| 23 | + * |
| 24 | + * @experimental |
| 25 | + */ |
| 26 | +trait ComponentWithThreeTrait |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @internal |
| 30 | + */ |
| 31 | + #[LiveProp(hydrateWith: 'hydrateThree', dehydrateWith: 'dehydrateThree')] |
| 32 | + #[ExposeInTemplate(getter: 'getThree')] |
| 33 | + public ?Three $three = null; |
| 34 | + |
| 35 | + abstract protected function instantiateThree(): Three; |
| 36 | + |
| 37 | + public function getThree(): Three |
| 38 | + { |
| 39 | + return $this->three ??= $this->instantiateThree(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @internal |
| 44 | + */ |
| 45 | + #[PostMount] |
| 46 | + public function initializeThree(array $data): array |
| 47 | + { |
| 48 | + // allow the Three object to be passed into the component() as "three" |
| 49 | + if (\array_key_exists('three', $data)) { |
| 50 | + $this->three = $data['three']; |
| 51 | + unset($data['three']); |
| 52 | + } |
| 53 | + |
| 54 | + return $data; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @internal |
| 59 | + */ |
| 60 | + public function hydrateThree(array $data): Three |
| 61 | + { |
| 62 | + return Three::fromArray($data); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @internal |
| 67 | + */ |
| 68 | + public function dehydrateThree(Three $three): array |
| 69 | + { |
| 70 | + return $three->toArray(); |
| 71 | + } |
| 72 | +} |
0 commit comments