• 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//
6// Use of this source code is governed by a BSD-style
7// license that can be found in the LICENSE file or at
8// https://developers.google.com/open-source/licenses/bsd
9
10namespace Google\Protobuf\Internal;
11
12class OneofDescriptor
13{
14    use HasPublicDescriptorTrait;
15
16    private $name;
17    /** @var \Google\Protobuf\FieldDescriptor[] $fields */
18    private $fields;
19
20    public function __construct()
21    {
22        $this->public_desc = new \Google\Protobuf\OneofDescriptor($this);
23    }
24
25    public function setName($name)
26    {
27        $this->name = $name;
28    }
29
30    public function getName()
31    {
32        return $this->name;
33    }
34
35    public function addField(FieldDescriptor $field)
36    {
37        $this->fields[] = $field;
38    }
39
40    public function getFields()
41    {
42        return $this->fields;
43    }
44
45    public function isSynthetic()
46    {
47        return !is_null($this->fields) && count($this->fields) === 1
48            && $this->fields[0]->getProto3Optional();
49    }
50
51    public static function buildFromProto($oneof_proto, $desc, $index)
52    {
53        $oneof = new OneofDescriptor();
54        $oneof->setName($oneof_proto->getName());
55        foreach ($desc->getField() as $field) {
56            /** @var FieldDescriptor $field */
57            if ($field->getOneofIndex() == $index) {
58                $oneof->addField($field);
59                $field->setContainingOneof($oneof);
60            }
61        }
62        return $oneof;
63    }
64}
65