12
12
namespace Symfony \UX \LiveComponent \Attribute ;
13
13
14
14
/**
15
+ * An attribute to mark a property as a "LiveProp".
16
+ *
17
+ * @see https://symfony.com/bundles/ux-live-component/current/index.html#liveprops-stateful-component-properties
18
+ *
15
19
* @experimental
16
20
*/
17
21
#[\Attribute(\Attribute::TARGET_PROPERTY )]
@@ -23,87 +27,83 @@ final class LiveProp
23
27
*/
24
28
public const IDENTITY = '@identity ' ;
25
29
26
- /** @var bool|string[] */
27
- private bool |array $ writable ;
28
-
29
- private ?string $ hydrateWith ;
30
-
31
- private ?string $ dehydrateWith ;
32
-
33
- private bool $ useSerializerForHydration ;
34
-
35
- private array $ serializationContext ;
36
-
37
- /**
38
- * The "frontend" field name that should be used for this property.
39
- *
40
- * This can be used, for example, to have a property called "foo", which actually
41
- * maps to a frontend data model called "bar".
42
- *
43
- * If you pass a string that ends in () - like "getFieldName()" - that
44
- * method on the component will be called to determine this.
45
- */
46
- private ?string $ fieldName ;
47
-
48
- private ?string $ format ;
49
-
50
- private bool $ acceptUpdatesFromParent ;
51
-
52
- /**
53
- * @var string|string[]|null
54
- *
55
- * A hook that will be called after the property is updated.
56
- * Set it to a method name on the Live Component that should be called.
57
- * The old value of the property will be passed as an argument to it.
58
- */
59
- private null |string |array $ onUpdated ;
60
-
61
- /**
62
- * Tells if this property should be bound to the URL.
63
- */
64
- private bool $ url ;
65
-
66
- /**
67
- * @param bool|array $writable If true, this property can be changed by the frontend.
68
- * Or set to an array of paths within this object/array
69
- * that are writable.
70
- * @param bool $useSerializerForHydration If true, the serializer will be used to
71
- * dehydrate then hydrate this property.
72
- * Incompatible with hydrateWith and dehydrateWith.
73
- * @param string|null $format The format to be used if the value is a DateTime of some sort.
74
- * For example: 'Y-m-d H:i:s'. If this property is writable, set this
75
- * to the format that your frontend field will use/set.
76
- * @param bool $updateFromParent if true, while a parent component is re-rendering,
77
- * if the parent passes in this prop and it changed
78
- * from the value used when originally rendering
79
- * this child, the value in the child will be updated
80
- * to match the new value and the child will be re-rendered
81
- * @param bool $url if true, this property will be synchronized with a query parameter
82
- * in the URL
83
- */
84
30
public function __construct (
85
- bool |array $ writable = false ,
86
- string $ hydrateWith = null ,
87
- string $ dehydrateWith = null ,
88
- bool $ useSerializerForHydration = false ,
89
- array $ serializationContext = [],
90
- string $ fieldName = null ,
91
- string $ format = null ,
92
- bool $ updateFromParent = false ,
93
- string |array $ onUpdated = null ,
94
- bool $ url = false ,
31
+ /**
32
+ * If true, this property can be changed by the frontend.
33
+ *
34
+ * Or set to an array of paths within this object/array
35
+ * that are writable.
36
+ *
37
+ * @var bool|string[]
38
+ */
39
+ private bool |array $ writable = false ,
40
+
41
+ /**
42
+ * Method to call to hydrate this property.
43
+ */
44
+ private ?string $ hydrateWith = null ,
45
+
46
+ /**
47
+ * Method to call to dehydrate this property.
48
+ */
49
+ private ?string $ dehydrateWith = null ,
50
+
51
+ /**
52
+ * If true, the serializer will be used to dehydrate then hydrate
53
+ * this property.
54
+ *
55
+ * Incompatible with hydrateWith and dehydrateWith.
56
+ */
57
+ private bool $ useSerializerForHydration = false ,
58
+
59
+ /**
60
+ * @var array<string, mixed>
61
+ */
62
+ private array $ serializationContext = [],
63
+
64
+ /**
65
+ * The "frontend" field name that should be used for this property.
66
+ *
67
+ * This can be used, for example, to have a property called "foo", which
68
+ * actually maps to a frontend data model called "bar".
69
+ *
70
+ * If you pass a string that ends in () - like "getFieldName()" - that
71
+ * method on the component will be called to determine this.
72
+ */
73
+ private ?string $ fieldName = null ,
74
+
75
+ /**
76
+ * The format to be used if the value is a DateTime of some sort.
77
+ *
78
+ * For example: 'Y-m-d H:i:s'. If this property is writable, set this
79
+ * to the format that your frontend field will use/set.
80
+ */
81
+ private ?string $ format = null ,
82
+
83
+ /**
84
+ * If true, while a parent component is re-rendering, if the parent
85
+ * passes in this prop and it changed from the value used when
86
+ * originally rendering this child, the value in the child will be
87
+ * updated to match the new value and the child will be re-rendered.
88
+ */
89
+ private bool $ updateFromParent = false ,
90
+
91
+ /**
92
+ * A hook that will be called after the property is updated.
93
+ *
94
+ * Set it to a method name on the Live Component that should be called.
95
+ * The old value of the property will be passed as an argument to it.
96
+ *
97
+ * @var string|string[]|null
98
+ */
99
+ private null |string |array $ onUpdated = null ,
100
+
101
+ /**
102
+ * If true, this property will be synchronized with a query parameter
103
+ * in the URL.
104
+ */
105
+ private bool $ url = false ,
95
106
) {
96
- $ this ->writable = $ writable ;
97
- $ this ->hydrateWith = $ hydrateWith ;
98
- $ this ->dehydrateWith = $ dehydrateWith ;
99
- $ this ->useSerializerForHydration = $ useSerializerForHydration ;
100
- $ this ->serializationContext = $ serializationContext ;
101
- $ this ->fieldName = $ fieldName ;
102
- $ this ->format = $ format ;
103
- $ this ->acceptUpdatesFromParent = $ updateFromParent ;
104
- $ this ->onUpdated = $ onUpdated ;
105
- $ this ->url = $ url ;
106
-
107
107
if ($ this ->useSerializerForHydration && ($ this ->hydrateWith || $ this ->dehydrateWith )) {
108
108
throw new \InvalidArgumentException ('Cannot use useSerializerForHydration with hydrateWith or dehydrateWith. ' );
109
109
}
@@ -190,7 +190,7 @@ public function format(): ?string
190
190
191
191
public function acceptUpdatesFromParent (): bool
192
192
{
193
- return $ this ->acceptUpdatesFromParent ;
193
+ return $ this ->updateFromParent ;
194
194
}
195
195
196
196
public function onUpdated (): null |string |array
0 commit comments