Skip to content
This repository was archived by the owner on Jan 12, 2021. It is now read-only.

Commit 38ab1dc

Browse files
committed
Generating has methods
1 parent 3063b9d commit 38ab1dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2904
-130
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,19 @@ For each field a set of accessors is generated. The set of methods is different
135135
136136
* `required` / `optional`
137137
138-
get{FIELD}() // return field value
139-
set{FIELD}($value) // set field value to $value
138+
get{FIELD}() // return a field value
139+
has{FIELD}() // check whether a field is set
140+
set{FIELD}($value) // set a field value to $value
140141
141142
* `repeated`
142143
143-
append{FIELD}($value) // append $value value to field
144+
append{FIELD}($value) // append $value to a field
144145
clear{FIELD}() // empty field
145-
get{FIELD}() // return array of field values
146-
getAt{FIELD}($index) // return field value at $index index
147-
getCount{FIELD}() // return number of field values
148-
getIterator{FIELD}($index) // return ArrayIterator for field values
146+
get{FIELD}() // return an array of field values
147+
getAt{FIELD}($index) // return a field value at $index index
148+
getCount{FIELD}() // return a number of field values
149+
has{FIELD}() // check whether a field is set
150+
getIterator{FIELD}() // return an ArrayIterator
149151
150152
{FIELD} is a camel cased field name.
151153

src/Allegro/Protobuf/Compiler/PhpGenerator.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,19 +226,21 @@ private function _addMessageDescriptor(
226226
$ignoredOneofs = array();
227227

228228
foreach ($descriptorProto->getField() as $fieldDescriptorProto) {
229-
$oneofIndex = $fieldDescriptorProto->getOneofIndex();
230-
if (!is_null($oneofIndex)) {
229+
if ($fieldDescriptorProto->hasOneofIndex()) {
230+
$oneofIndex = $fieldDescriptorProto->getOneofIndex();
231231
if (!in_array($oneofIndex, $ignoredOneofs)) {
232232
$oneofDescriptorProto = $descriptorProto->getOneofDeclAt($oneofIndex);
233233
$name = $oneofDescriptorProto->getName();
234-
Logger::warn("Ignoring '{$name}' field, "
234+
Logger::warn("Ignoring '{$name}', "
235235
. 'oneof is not supported (https://github.com/allegro/php-protobuf/issues/72).');
236236
$ignoredOneofs[] = $oneofIndex;
237237
}
238238
} else {
239239
$fieldDescriptor = new FieldDescriptor();
240240
$fieldDescriptor->setName($fieldDescriptorProto->getName());
241-
$fieldDescriptor->setDefault($fieldDescriptorProto->getDefaultValue());
241+
if ($fieldDescriptorProto->hasDefaultValue()) {
242+
$fieldDescriptor->setDefault($fieldDescriptorProto->getDefaultValue());
243+
}
242244
$fieldDescriptor->setLabel($fieldDescriptorProto->getLabel());
243245
$fieldDescriptor->setNumber($fieldDescriptorProto->getNumber());
244246
$fieldDescriptor->setType($fieldDescriptorProto->getType());
@@ -589,6 +591,22 @@ private function _describeRepeatedField(FieldDescriptor $field, CodeStringBuffer
589591
)
590592
->append('}');
591593

594+
$comment = new CommentStringBuffer(self::TAB, self::EOL);
595+
$comment->append('Returns true if \'' . $field->getName() . '\' property is set, false otherwise')
596+
->newline()
597+
->appendParam('return', 'boolean');
598+
599+
$buffer->newline()
600+
->append($comment)
601+
->append('public function has' . $field->getCamelCaseName() . '()')
602+
->append('{')
603+
->append(
604+
'return count($this->get(self::' . $field->getConstName() . ')) !== 0;',
605+
false,
606+
1
607+
)
608+
->append('}');
609+
592610
$comment = new CommentStringBuffer(self::TAB, self::EOL);
593611
$comment->append('Returns \'' . $field->getName() . '\' iterator')
594612
->newline()
@@ -714,6 +732,22 @@ private function _describeSingleField(FieldDescriptor $field, CodeStringBuffer $
714732
}
715733
$buffer->decreaseIdentation()
716734
->append('}');
735+
736+
$comment = new CommentStringBuffer(self::TAB, self::EOL);
737+
$comment->append('Returns true if \'' . $field->getName() . '\' property is set, false otherwise')
738+
->newline()
739+
->appendParam('return', 'boolean');
740+
741+
$buffer->newline()
742+
->append($comment)
743+
->append('public function has' . $field->getCamelCaseName() . '()')
744+
->append('{')
745+
->append(
746+
'return $this->get(self::' . $field->getConstName() . ') !== null;',
747+
false,
748+
1
749+
)
750+
->append('}');
717751
}
718752

719753
/**

src/Google/Protobuf/Compiler/CodeGeneratorRequest.php

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Auto generated from plugin.proto at 2016-07-08 14:22:37
3+
* Auto generated from plugin.proto at 2019-01-07 11:34:00
44
*
55
* google.protobuf.compiler package
66
*/
@@ -15,6 +15,7 @@ class CodeGeneratorRequest extends \ProtobufMessage
1515
const FILE_TO_GENERATE = 1;
1616
const PARAMETER = 2;
1717
const PROTO_FILE = 15;
18+
const COMPILER_VERSION = 3;
1819

1920
/* @var array Field descriptors */
2021
protected static $fields = array(
@@ -33,6 +34,11 @@ class CodeGeneratorRequest extends \ProtobufMessage
3334
'repeated' => true,
3435
'type' => '\Google\Protobuf\FileDescriptorProto'
3536
),
37+
self::COMPILER_VERSION => array(
38+
'name' => 'compiler_version',
39+
'required' => false,
40+
'type' => '\Google\Protobuf\Compiler\Version'
41+
),
3642
);
3743

3844
/**
@@ -53,6 +59,7 @@ public function reset()
5359
$this->values[self::FILE_TO_GENERATE] = array();
5460
$this->values[self::PARAMETER] = null;
5561
$this->values[self::PROTO_FILE] = array();
62+
$this->values[self::COMPILER_VERSION] = null;
5663
}
5764

5865
/**
@@ -97,6 +104,16 @@ public function getFileToGenerate()
97104
return $this->get(self::FILE_TO_GENERATE);
98105
}
99106

107+
/**
108+
* Returns true if 'file_to_generate' property is set, false otherwise
109+
*
110+
* @return boolean
111+
*/
112+
public function hasFileToGenerate()
113+
{
114+
return count($this->get(self::FILE_TO_GENERATE)) !== 0;
115+
}
116+
100117
/**
101118
* Returns 'file_to_generate' iterator
102119
*
@@ -148,7 +165,18 @@ public function setParameter($value)
148165
*/
149166
public function getParameter()
150167
{
151-
return $this->get(self::PARAMETER);
168+
$value = $this->get(self::PARAMETER);
169+
return $value === null ? (string)$value : $value;
170+
}
171+
172+
/**
173+
* Returns true if 'parameter' property is set, false otherwise
174+
*
175+
* @return boolean
176+
*/
177+
public function hasParameter()
178+
{
179+
return $this->get(self::PARAMETER) !== null;
152180
}
153181

154182
/**
@@ -183,6 +211,16 @@ public function getProtoFile()
183211
return $this->get(self::PROTO_FILE);
184212
}
185213

214+
/**
215+
* Returns true if 'proto_file' property is set, false otherwise
216+
*
217+
* @return boolean
218+
*/
219+
public function hasProtoFile()
220+
{
221+
return count($this->get(self::PROTO_FILE)) !== 0;
222+
}
223+
186224
/**
187225
* Returns 'proto_file' iterator
188226
*
@@ -214,5 +252,37 @@ public function getProtoFileCount()
214252
{
215253
return $this->count(self::PROTO_FILE);
216254
}
255+
256+
/**
257+
* Sets value of 'compiler_version' property
258+
*
259+
* @param \Google\Protobuf\Compiler\Version $value Property value
260+
*
261+
* @return null
262+
*/
263+
public function setCompilerVersion(\Google\Protobuf\Compiler\Version $value=null)
264+
{
265+
return $this->set(self::COMPILER_VERSION, $value);
266+
}
267+
268+
/**
269+
* Returns value of 'compiler_version' property
270+
*
271+
* @return \Google\Protobuf\Compiler\Version
272+
*/
273+
public function getCompilerVersion()
274+
{
275+
return $this->get(self::COMPILER_VERSION);
276+
}
277+
278+
/**
279+
* Returns true if 'compiler_version' property is set, false otherwise
280+
*
281+
* @return boolean
282+
*/
283+
public function hasCompilerVersion()
284+
{
285+
return $this->get(self::COMPILER_VERSION) !== null;
286+
}
217287
}
218288
}

src/Google/Protobuf/Compiler/CodeGeneratorResponse.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Auto generated from plugin.proto at 2016-07-08 14:22:37
3+
* Auto generated from plugin.proto at 2019-01-07 11:34:00
44
*
55
* google.protobuf.compiler package
66
*/
@@ -77,7 +77,18 @@ public function setError($value)
7777
*/
7878
public function getError()
7979
{
80-
return $this->get(self::ERROR);
80+
$value = $this->get(self::ERROR);
81+
return $value === null ? (string)$value : $value;
82+
}
83+
84+
/**
85+
* Returns true if 'error' property is set, false otherwise
86+
*
87+
* @return boolean
88+
*/
89+
public function hasError()
90+
{
91+
return $this->get(self::ERROR) !== null;
8192
}
8293

8394
/**
@@ -112,6 +123,16 @@ public function getFile()
112123
return $this->get(self::FILE);
113124
}
114125

126+
/**
127+
* Returns true if 'file' property is set, false otherwise
128+
*
129+
* @return boolean
130+
*/
131+
public function hasFile()
132+
{
133+
return count($this->get(self::FILE)) !== 0;
134+
}
135+
115136
/**
116137
* Returns 'file' iterator
117138
*

src/Google/Protobuf/Compiler/CodeGeneratorResponse_File.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Auto generated from plugin.proto at 2016-07-08 14:22:37
3+
* Auto generated from plugin.proto at 2019-01-07 11:34:00
44
*
55
* google.protobuf.compiler package
66
*/
@@ -84,7 +84,18 @@ public function setName($value)
8484
*/
8585
public function getName()
8686
{
87-
return $this->get(self::NAME);
87+
$value = $this->get(self::NAME);
88+
return $value === null ? (string)$value : $value;
89+
}
90+
91+
/**
92+
* Returns true if 'name' property is set, false otherwise
93+
*
94+
* @return boolean
95+
*/
96+
public function hasName()
97+
{
98+
return $this->get(self::NAME) !== null;
8899
}
89100

90101
/**
@@ -106,7 +117,18 @@ public function setInsertionPoint($value)
106117
*/
107118
public function getInsertionPoint()
108119
{
109-
return $this->get(self::INSERTION_POINT);
120+
$value = $this->get(self::INSERTION_POINT);
121+
return $value === null ? (string)$value : $value;
122+
}
123+
124+
/**
125+
* Returns true if 'insertion_point' property is set, false otherwise
126+
*
127+
* @return boolean
128+
*/
129+
public function hasInsertionPoint()
130+
{
131+
return $this->get(self::INSERTION_POINT) !== null;
110132
}
111133

112134
/**
@@ -128,7 +150,18 @@ public function setContent($value)
128150
*/
129151
public function getContent()
130152
{
131-
return $this->get(self::CONTENT);
153+
$value = $this->get(self::CONTENT);
154+
return $value === null ? (string)$value : $value;
155+
}
156+
157+
/**
158+
* Returns true if 'content' property is set, false otherwise
159+
*
160+
* @return boolean
161+
*/
162+
public function hasContent()
163+
{
164+
return $this->get(self::CONTENT) !== null;
132165
}
133166
}
134167
}

0 commit comments

Comments
 (0)