44
55use Swaggest \JsonSchema \Exception ;
66use Swaggest \JsonSchema \Schema ;
7+ use Swaggest \JsonSchema \SchemaContract ;
78use Swaggest \JsonSchema \Structure \Egg ;
89use Swaggest \JsonSchema \Structure \Nested ;
910use Swaggest \JsonSchema \Structure \ObjectItem ;
1011
1112/**
12- * @method Schema __get($key)
13+ * @method SchemaContract __get($key)
1314 * @method Schema[] toArray()
1415 */
1516class Properties extends ObjectItem implements Constraint
1617{
18+ private $ __isReadOnly = false ;
19+
1720 /** @var Schema[] */
1821 protected $ __arrayOfData = array ();
1922
2023 /** @var Schema */
2124 protected $ __schema ;
2225
23- public function setSchema ( Schema $ schema )
26+ public function lock ( )
2427 {
25- $ this ->__schema = $ schema ;
28+ $ this ->__isReadOnly = true ;
2629 return $ this ;
2730 }
2831
29- public function getSchema ()
30- {
31- return $ this ->__schema ;
32- }
33-
3432 /**
3533 * @param string $name
3634 * @param mixed $column
@@ -39,6 +37,9 @@ public function getSchema()
3937 */
4038 public function __set ($ name , $ column )
4139 {
40+ if ($ this ->__isReadOnly ) {
41+ throw new Exception ('Trying to modify read-only Properties ' );
42+ }
4243 if ($ column instanceof Nested) {
4344 $ this ->addNested ($ column ->schema , $ name );
4445 return $ this ;
@@ -55,30 +56,28 @@ public static function create()
5556 /** @var Schema|null */
5657 private $ additionalProperties ;
5758
58- /**
59- * @param Schema $additionalProperties
60- * @return Properties
61- */
62- public function setAdditionalProperties (Schema $ additionalProperties = null )
63- {
64- $ this ->additionalProperties = $ additionalProperties ;
65- return $ this ;
66- }
67-
68-
6959 /** @var Egg[][] */
7060 public $ nestedProperties = array ();
7161
72- /** @var Schema [] */
62+ /** @var string [] */
7363 public $ nestedPropertyNames = array ();
7464
75- protected function addNested (Schema $ nested , $ name )
65+ /**
66+ * @param SchemaContract $nested
67+ * @param string $name
68+ * @return $this
69+ * @throws Exception
70+ */
71+ protected function addNested (SchemaContract $ nested , $ name )
7672 {
77- if (null === $ nested ->properties ) {
73+ if ($ this ->__isReadOnly ) {
74+ throw new Exception ('Trying to modify read-only Properties ' );
75+ }
76+ if (null === $ nested ->getProperties ()) {
7877 throw new Exception ('Schema with properties required ' , Exception::PROPERTIES_REQUIRED );
7978 }
8079 $ this ->nestedPropertyNames [$ name ] = $ name ;
81- foreach ($ nested ->properties ->toArray () as $ propertyName => $ property ) {
80+ foreach ($ nested ->getProperties () ->toArray () as $ propertyName => $ property ) {
8281 $ this ->nestedProperties [$ propertyName ][] = new Egg ($ nested , $ name , $ property );
8382 }
8483 return $ this ;
0 commit comments