1212namespace Symfony \UX \LiveComponent \Attribute ;
1313
1414/**
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+ *
1519 * @experimental
1620 */
1721#[\Attribute(\Attribute::TARGET_PROPERTY )]
@@ -23,89 +27,83 @@ final class LiveProp
2327 */
2428 public const IDENTITY = '@identity ' ;
2529
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- * @var bool
63- *
64- * Tells if this property should be bound to the URL
65- */
66- private bool $ url ;
67-
68- /**
69- * @param bool|array $writable If true, this property can be changed by the frontend.
70- * Or set to an array of paths within this object/array
71- * that are writable.
72- * @param bool $useSerializerForHydration If true, the serializer will be used to
73- * dehydrate then hydrate this property.
74- * Incompatible with hydrateWith and dehydrateWith.
75- * @param string|null $format The format to be used if the value is a DateTime of some sort.
76- * For example: 'Y-m-d H:i:s'. If this property is writable, set this
77- * to the format that your frontend field will use/set.
78- * @param bool $updateFromParent if true, while a parent component is re-rendering,
79- * if the parent passes in this prop and it changed
80- * from the value used when originally rendering
81- * this child, the value in the child will be updated
82- * to match the new value and the child will be re-rendered
83- * @param bool $url if true, this property will be synchronized with a query parameter
84- * in the URL
85- */
8630 public function __construct (
87- bool |array $ writable = false ,
88- string $ hydrateWith = null ,
89- string $ dehydrateWith = null ,
90- bool $ useSerializerForHydration = false ,
91- array $ serializationContext = [],
92- string $ fieldName = null ,
93- string $ format = null ,
94- bool $ updateFromParent = false ,
95- string |array $ onUpdated = null ,
96- 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 ,
97106 ) {
98- $ this ->writable = $ writable ;
99- $ this ->hydrateWith = $ hydrateWith ;
100- $ this ->dehydrateWith = $ dehydrateWith ;
101- $ this ->useSerializerForHydration = $ useSerializerForHydration ;
102- $ this ->serializationContext = $ serializationContext ;
103- $ this ->fieldName = $ fieldName ;
104- $ this ->format = $ format ;
105- $ this ->acceptUpdatesFromParent = $ updateFromParent ;
106- $ this ->onUpdated = $ onUpdated ;
107- $ this ->url = $ url ;
108-
109107 if ($ this ->useSerializerForHydration && ($ this ->hydrateWith || $ this ->dehydrateWith )) {
110108 throw new \InvalidArgumentException ('Cannot use useSerializerForHydration with hydrateWith or dehydrateWith. ' );
111109 }
@@ -192,7 +190,7 @@ public function format(): ?string
192190
193191 public function acceptUpdatesFromParent (): bool
194192 {
195- return $ this ->acceptUpdatesFromParent ;
193+ return $ this ->updateFromParent ;
196194 }
197195
198196 public function onUpdated (): null |string |array
0 commit comments