• 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
35class OneofDescriptor
36{
37    use HasPublicDescriptorTrait;
38
39    private $name;
40    /** @var \Google\Protobuf\FieldDescriptor[] $fields */
41    private $fields;
42
43    public function __construct()
44    {
45        $this->public_desc = new \Google\Protobuf\OneofDescriptor($this);
46    }
47
48    public function setName($name)
49    {
50        $this->name = $name;
51    }
52
53    public function getName()
54    {
55        return $this->name;
56    }
57
58    public function addField(FieldDescriptor $field)
59    {
60        $this->fields[] = $field;
61    }
62
63    public function getFields()
64    {
65        return $this->fields;
66    }
67
68    public function isSynthetic()
69    {
70        return !is_null($this->fields) && count($this->fields) === 1
71            && $this->fields[0]->getProto3Optional();
72    }
73
74    public static function buildFromProto($oneof_proto, $desc, $index)
75    {
76        $oneof = new OneofDescriptor();
77        $oneof->setName($oneof_proto->getName());
78        foreach ($desc->getField() as $field) {
79            /** @var FieldDescriptor $field */
80            if ($field->getOneofIndex() == $index) {
81                $oneof->addField($field);
82                $field->setContainingOneof($oneof);
83            }
84        }
85        return $oneof;
86    }
87}
88