Debian Codegraph @

lib/3rdParty/composer/illuminate/contracts/Database/ModelIdentifier.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Contracts\Database;
4
5 use Illuminate\Database\Eloquent\Relations\Relation;
6
7 class ModelIdentifier
8 {
9 /**
10 * Use the Relation morphMap for a Model's name when serializing.
11 */
12 protected static bool $useMorphMap = false;
13
14 /**
15 * The class name of the model.
16 *
17 * @var class-string<\Illuminate\Database\Eloquent\Model>|string|null
18 */
19 public $class;
20
21 /**
22 * The unique identifier of the model.
23 *
24 * This may be either a single ID or an array of IDs.
25 *
26 * @var mixed
27 */
28 public $id;
29
30 /**
31 * The relationships loaded on the model.
32 *
33 * @var array
34 */
35 public $relations;
36
37 /**
38 * The connection name of the model.
39 *
40 * @var string|null
41 */
42 public $connection;
43
44 /**
45 * The class name of the model collection.
46 *
47 * @var class-string<\Illuminate\Database\Eloquent\Collection>|null
48 */
49 public $collectionClass;
50
51 /**
52 * Create a new model identifier.
53 *
54 * @param class-string<\Illuminate\Database\Eloquent\Model>|null $class
55 * @param mixed $id
56 * @param array $relations
57 * @param mixed $connection
58 */
59 public function __construct($class, $id, array $relations, $connection)
60 {
61 if ($class !== null && self::$useMorphMap) {
62 $class = Relation::getMorphAlias($class);
63 }
64
65 $this->class = $class;
66 $this->id = $id;
67 $this->relations = $relations;
68 $this->connection = $connection;
69 }
70
71 /**
72 * Specify the collection class that should be used when serializing / restoring collections.
73 *
74 * @param class-string<\Illuminate\Database\Eloquent\Collection> $collectionClass
75 * @return $this
76 */
77 public function useCollectionClass(?string $collectionClass)
78 {
79 $this->collectionClass = $collectionClass;
80
81 return $this;
82 }
83
84 /**
85 * Get the fully-qualified class name of the Model.
86 *
87 * @return class-string<\Illuminate\Database\Eloquent\Model>|null
88 */
89 public function getClass(): ?string
90 {
91 if (self::$useMorphMap && $this->class !== null) {
92 return Relation::getMorphedModel($this->class) ?? $this->class;
93 }
94
95 return $this->class;
96 }
97
98 /**
99 * Indicate whether to use the relational morph-map when serializing Models.
100 */
101 public static function useMorphMap(bool $useMorphMap = true): void
102 {
103 static::$useMorphMap = $useMorphMap;
104 }
105 }