• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?php
2
3// Protocol Buffers - Google's data interchange format
4// Copyright 2008 Google Inc.  All rights reserved.
5// https://developers.google.com/protocol-buffers/
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions are
9// met:
10//
11//     * Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//     * Redistributions in binary form must reproduce the above
14// copyright notice, this list of conditions and the following disclaimer
15// in the documentation and/or other materials provided with the
16// distribution.
17//     * Neither the name of Google Inc. nor the names of its
18// contributors may be used to endorse or promote products derived from
19// this software without specific prior written permission.
20//
21// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33namespace Google\Protobuf\Internal;
34
35use Google\Protobuf\Internal\GPBLabel;
36use Google\Protobuf\Internal\GPBType;
37use Google\Protobuf\Internal\Descriptor;
38use Google\Protobuf\Internal\FieldDescriptor;
39
40class MessageBuilderContext
41{
42
43    private $descriptor;
44    private $pool;
45
46    public function __construct($full_name, $klass, $pool)
47    {
48        $this->descriptor = new Descriptor();
49        $this->descriptor->setFullName($full_name);
50        $this->descriptor->setClass($klass);
51        $this->pool = $pool;
52    }
53
54    private function getFieldDescriptor($name, $label, $type,
55                                      $number, $type_name = null)
56    {
57        $field = new FieldDescriptor();
58        $field->setName($name);
59        $camel_name = implode('', array_map('ucwords', explode('_', $name)));
60        $field->setGetter('get' . $camel_name);
61        $field->setSetter('set' . $camel_name);
62        $field->setType($type);
63        $field->setNumber($number);
64        $field->setLabel($label);
65
66        // At this time, the message/enum type may have not been added to pool.
67        // So we use the type name as place holder and will replace it with the
68        // actual descriptor in cross building.
69        switch ($type) {
70        case GPBType::MESSAGE:
71          $field->setMessageType($type_name);
72          break;
73        case GPBType::ENUM:
74          $field->setEnumType($type_name);
75          break;
76        default:
77          break;
78        }
79
80        return $field;
81    }
82
83    public function optional($name, $type, $number, $type_name = null)
84    {
85        $this->descriptor->addField($this->getFieldDescriptor(
86            $name,
87            GPBLabel::OPTIONAL,
88            $type,
89            $number,
90            $type_name));
91        return $this;
92    }
93
94    public function repeated($name, $type, $number, $type_name = null)
95    {
96        $this->descriptor->addField($this->getFieldDescriptor(
97            $name,
98            GPBLabel::REPEATED,
99            $type,
100            $number,
101            $type_name));
102        return $this;
103    }
104
105    public function required($name, $type, $number, $type_name = null)
106    {
107        $this->descriptor->addField($this->getFieldDescriptor(
108            $name,
109            GPBLabel::REQUIRED,
110            $type,
111            $number,
112            $type_name));
113        return $this;
114    }
115
116    public function finalizeToPool()
117    {
118        $this->pool->addDescriptor($this->descriptor);
119    }
120}
121