55namespace PackageFactory \NodeTypeObjects \Factory ;
66
77use Neos \Utility \Unicode \Functions as UnicodeFunctions ;
8+ use PackageFactory \NodeTypeObjects \Domain \NodePropertySpecification ;
89use PackageFactory \NodeTypeObjects \Domain \NodeTypeObjectFile ;
910use PackageFactory \NodeTypeObjects \Domain \NodeTypeObjectSpecification ;
1011
@@ -14,10 +15,13 @@ public function createNodeTypeObjectPhpCodeFromNode(
1415 NodeTypeObjectSpecification $ specification ,
1516 ): NodeTypeObjectFile {
1617 $ propertyAccessors = '' ;
18+ $ internalPropertyAccessors = '' ;
1719 foreach ($ specification ->properties as $ property ) {
1820 $ propertyType = $ property ->propertyType ;
21+ $ propertyDefaultValue = $ property ->defaultValue ;
1922 $ propertyName = $ property ->propertyName ;
20- if (str_starts_with ($ property ->propertyName , '_ ' )) {
23+ $ propertyIsInternal = str_starts_with ($ property ->propertyName , '_ ' );
24+ if ($ propertyIsInternal ) {
2125 $ methodName = 'getInternal ' . UnicodeFunctions::ucfirst (substr ($ propertyName , 1 ));
2226 } else {
2327 $ methodName = 'get ' . UnicodeFunctions::ucfirst ($ propertyName );
@@ -47,28 +51,59 @@ public function createNodeTypeObjectPhpCodeFromNode(
4751 $ annotationType = '\\' . $ annotationType ;
4852 }
4953
50- if ($ annotationType ) {
51- $ propertyAccessors .= <<<EOL
54+ $ returnType = ($ propertyDefaultValue === null ) ? '? ' . $ phpType : $ phpType ;
55+
56+ $ defaultReturn = match (true ) {
57+ ($ propertyType === 'DateTime ' && is_string ($ propertyDefaultValue )) => 'new \DateTime( \'' . $ propertyDefaultValue . '\') ' ,
58+ default => var_export ($ propertyDefaultValue , true ),
59+ };
5260
61+ $ typeCheck = match ($ phpType ) {
62+ 'null ' => 'is_null($value) ' ,
63+ 'string ' => 'is_string($value) ' ,
64+ 'int ' => 'is_int($value) ' ,
65+ 'float ' => 'is_float($value) ' ,
66+ 'bool ' => 'is_bool($value) ' ,
67+ 'array ' => 'is_array($value) ' ,
68+ default => '$value instanceof ' . $ phpType ,
69+ };
70+
71+ if ($ annotationType ) {
72+ $ propertyAccessor = <<<EOL
5373
5474 /**
5575 * @return ? $ annotationType;
5676 */
57- public function $ methodName(): ? $ phpType
77+ public function $ methodName(): $ returnType
5878 {
59- return \$this->node->getProperty(' $ propertyName');
79+ \$value = \$this->node->getProperty(' $ propertyName');
80+ if ( $ typeCheck) {
81+ return \$value;
82+ }
83+ return $ defaultReturn;
6084 }
85+
6186 EOL ;
6287 } else {
63- $ propertyAccessors .= <<<EOL
64-
88+ $ propertyAccessor = <<<EOL
6589
66- public function $ methodName(): ? $ phpType
90+ public function $ methodName(): $ returnType
6791 {
68- return \$this->node->getProperty(' $ propertyName');
92+ \$value = \$this->node->getProperty(' $ propertyName');
93+ if ( $ typeCheck) {
94+ return \$value;
95+ }
96+ return $ defaultReturn;
6997 }
98+
7099 EOL ;
71100 }
101+
102+ if ($ propertyIsInternal ) {
103+ $ internalPropertyAccessors .= $ propertyAccessor ;
104+ } else {
105+ $ propertyAccessors .= $ propertyAccessor ;
106+ }
72107 }
73108
74109 $ nodeTypeName = $ specification ->nodeTypeName ;
@@ -106,7 +141,13 @@ public static function fromNode(Node \$node): self
106141 throw new \Exception("unsupported nodetype " . \$node->nodeTypeName->value);
107142 }
108143 return new self( \$node);
109- } $ propertyAccessors
144+ }
145+
146+ // property accessors
147+ $ propertyAccessors
148+
149+ // internal property accessors
150+ $ internalPropertyAccessors
110151 }
111152
112153 EOL ;
0 commit comments