22
33namespace Swaggest \JsonSchema \Structure ;
44
5- use Swaggest \JsonSchema \Base ;
65use Swaggest \JsonSchema \Constraint \Properties ;
76use Swaggest \JsonSchema \Constraint \Type ;
7+ use Swaggest \JsonSchema \NameMirror ;
88use Swaggest \JsonSchema \Schema ;
9- use Swaggest \JsonSchema \OldSchema ;
109
11- abstract class ClassStructure implements ClassStructureContract
10+ abstract class ClassStructure extends ObjectItem implements ClassStructureContract
1211{
1312 /**
1413 * @return Schema
1514 */
16- public static function makeSchema ()
15+ public static function schema ()
1716 {
1817 $ schema = new Schema ();
1918 $ schema ->type = new Type (Type::OBJECT );
2019 $ properties = new Properties ();
2120 $ schema ->properties = $ properties ;
21+ $ schema ->objectItemClass = get_called_class ();
2222 static ::setUpProperties ($ properties , $ schema );
2323 return $ schema ;
2424 }
@@ -31,7 +31,7 @@ public static function makeSchema()
3131 public static function import ($ data )
3232 {
3333 //static $schemas = array();
34- return static ::makeSchema ()->import ($ data );
34+ return static ::schema ()->import ($ data );
3535 }
3636
3737 /**
@@ -41,7 +41,7 @@ public static function import($data)
4141 */
4242 public static function export ($ data )
4343 {
44- return static ::makeSchema ()->export ($ data );
44+ return static ::schema ()->export ($ data );
4545 }
4646
4747 /**
@@ -51,4 +51,49 @@ static function create()
5151 {
5252 return new static ;
5353 }
54+
55+ protected $ __hasNativeProperties = true ;
56+ protected $ __validateOnSet = true ;
57+
58+ public function jsonSerialize ()
59+ {
60+ if ($ this ->__hasNativeProperties ) {
61+ $ result = new \stdClass ();
62+ foreach (static ::schema ()->properties ->toArray () as $ name => $ schema ) {
63+ $ value = $ this ->$ name ;
64+ if (null !== $ value || array_key_exists ($ name , $ this ->_arrayOfData )) {
65+ $ result ->$ name = $ value ;
66+ }
67+ }
68+ } else {
69+ $ result = parent ::jsonSerialize ();
70+ }
71+
72+ return $ result ;
73+ }
74+
75+ /**
76+ * @return static
77+ */
78+ public static function names ()
79+ {
80+ static $ nameflector = null ;
81+ if (null === $ nameflector ) {
82+ $ nameflector = new NameMirror ();
83+ }
84+ return $ nameflector ;
85+ }
86+
87+ public function __set ($ name , $ column )
88+ {
89+ if ($ this ->__validateOnSet ) {
90+ if ($ property = static ::schema ()->properties [$ name ]) {
91+ $ property ->export ($ column );
92+ }
93+ }
94+ $ this ->_arrayOfData [$ name ] = $ column ;
95+ return $ this ;
96+ }
97+
98+
5499}
0 commit comments