|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of Hyperf. |
| 6 | + * |
| 7 | + * @link https://www.hyperf.io |
| 8 | + * @document https://hyperf.wiki |
| 9 | + * @contact group@hyperf.io |
| 10 | + * @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Hyperf\Database\Model\Casts; |
| 14 | + |
| 15 | +use Hyperf\Collection\Collection; |
| 16 | +use Hyperf\Contract\CastsAttributes; |
| 17 | +use Hyperf\Stringable\Str; |
| 18 | +use InvalidArgumentException; |
| 19 | + |
| 20 | +class AsCollection implements CastsAttributes |
| 21 | +{ |
| 22 | + public function __construct(protected ?string $collectionClass = null, protected ?string $parseCallback = null) |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + public function get($model, string $key, $value, array $attributes) |
| 27 | + { |
| 28 | + if (! isset($attributes[$key])) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + |
| 32 | + $data = Json::decode($attributes[$key]); |
| 33 | + |
| 34 | + $collectionClass = empty($this->collectionClass) ? Collection::class : $this->collectionClass; |
| 35 | + |
| 36 | + if (! is_a($collectionClass, Collection::class, true)) { |
| 37 | + throw new InvalidArgumentException('The provided class must extend [' . Collection::class . '].'); |
| 38 | + } |
| 39 | + |
| 40 | + if (! is_array($data)) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + $instance = new $collectionClass($data); |
| 45 | + |
| 46 | + if (empty($this->parseCallback)) { |
| 47 | + return $instance; |
| 48 | + } |
| 49 | + |
| 50 | + $parseCallback = Str::parseCallback($this->parseCallback); |
| 51 | + if (is_callable($parseCallback)) { |
| 52 | + return $instance->map($parseCallback); |
| 53 | + } |
| 54 | + |
| 55 | + return $instance->mapInto($parseCallback[0]); |
| 56 | + } |
| 57 | + |
| 58 | + public function set($model, $key, $value, $attributes) |
| 59 | + { |
| 60 | + return [$key => Json::encode($value)]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Specify the type of object each item in the collection should be mapped to. |
| 65 | + * |
| 66 | + * @param array{class-string, string}|class-string $map |
| 67 | + * @return string |
| 68 | + */ |
| 69 | + public static function of($map) |
| 70 | + { |
| 71 | + return static::using('', $map); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Specify the collection type for the cast. |
| 76 | + * |
| 77 | + * @param class-string $class |
| 78 | + * @param array{class-string, string}|class-string $map |
| 79 | + * @return string |
| 80 | + */ |
| 81 | + public static function using($class, $map = null) |
| 82 | + { |
| 83 | + if ( |
| 84 | + is_array($map) |
| 85 | + && count($map) === 2 |
| 86 | + && is_string($map[0]) |
| 87 | + && is_string($map[1]) |
| 88 | + && is_callable($map) |
| 89 | + ) { |
| 90 | + $map = $map[0] . '@' . $map[1]; |
| 91 | + } |
| 92 | + |
| 93 | + return static::class . ':' . implode(',', [$class, $map]); |
| 94 | + } |
| 95 | +} |
0 commit comments