Skip to content

Commit 11775eb

Browse files
committed
Required field support
1 parent fe5c7c8 commit 11775eb

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

Tool/Generator.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ public function generateDefinition(string $namespacedClass, array $properties) :
8787

8888
if (! isset($properties['type']))
8989
{
90-
return;
90+
echo "{$namespacedClass} has no type";
91+
if (str_contains($namespacedClass, 'ResendToNonOpenersInput'))
92+
{
93+
echo 'but assuming object';
94+
$properties['type'] = 'object';
95+
}
96+
echo "\n";
9197
}
9298

9399
$class = \str_replace('_', '', $class);
@@ -99,6 +105,7 @@ public function generateDefinition(string $namespacedClass, array $properties) :
99105
$minLength = [];
100106
$maxLength = [];
101107
$docBlock = [];
108+
$required = [];
102109
$dollar = '$';
103110

104111
foreach ($properties['properties'] ?? [] as $name => $details)
@@ -179,6 +186,11 @@ public function generateDefinition(string $namespacedClass, array $properties) :
179186
$maxLength[$name] = (int)$details['maxLength'];
180187
}
181188

189+
if (isset($details['required']))
190+
{
191+
$required = $details['required'];
192+
}
193+
182194
$description = '';
183195

184196
if (isset($details['description']))
@@ -193,7 +205,7 @@ public function generateDefinition(string $namespacedClass, array $properties) :
193205
$type = \str_replace('\\\\', '\\', $type);
194206
$docBlock[] = \trim("{$type} {$dollar}{$name} {$description}");
195207
}
196-
$this->generateFromTemplate($class, ['fields' => $fields, 'minLength' => $minLength, 'maxLength' => $maxLength, ], $docBlock);
208+
$this->generateFromTemplate($class, ['fields' => $fields, 'minLength' => $minLength, 'maxLength' => $maxLength, 'requiredFields' => $required], $docBlock);
197209
}
198210
}
199211

@@ -564,8 +576,14 @@ class ~class~ extends {$this->definitionNamespace}\Base
564576

565577
break;
566578
}
567-
568-
$output .= "\t\t'{$field}' => {$value},\n";
579+
if ('string' == \gettype($field))
580+
{
581+
$output .= "\t\t'{$field}' => {$value},\n";
582+
}
583+
else
584+
{
585+
$output .= "\t\t{$value},\n";
586+
}
569587
}
570588

571589
$output .= "\n\t];\n";

src/ConstantContact/Definition/Base.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,32 @@ abstract class Base
1919
protected static array $fields = [];
2020

2121
/**
22-
* @var array of minimum allowed values. Arrays are size, int and float are values, strings are length.
22+
* @var array<string, int> minimum allowed values. Arrays are size, int and float are values, strings are length.
2323
*/
2424
protected static array $minLength = [];
2525

2626
/**
27-
* @var array of maximum allowed values. Arrays are size, int and float are values, strings are length.
27+
* @var array<string, int> maximum allowed values. Arrays are size, int and float are values, strings are length.
2828
*/
2929
protected static array $maxLength = [];
3030

31+
/**
32+
* @var array<string> required fields.
33+
*/
34+
protected static array $requiredFields = [];
35+
3136
/**
32-
* $var array of the actual object data
37+
* $var array<string, mixed> the actual object data by field name.
3338
*/
3439
private array $data = [];
3540

3641
/**
37-
* @var array of bool indicating which values are set to reduce data output.
42+
* @var array<string, bool> indicates which values are set to reduce data output.
3843
*/
3944
private array $setFields = [];
4045

4146
/**
42-
* @var array of supported scalars
47+
* @var array<string, bool> supported scalars
4348
*/
4449
private static array $scalars = [
4550
'bool' => true,
@@ -241,6 +246,14 @@ public function getData() : array
241246
{
242247
$result = [];
243248

249+
foreach (static::$requiredFields as $field)
250+
{
251+
if (! empty($this->setFields[$field]))
252+
{
253+
throw new \PHPFUI\ConstantContact\Exception\RequiredField(static::class . "::{$field} is required but not set.");
254+
}
255+
}
256+
244257
foreach ($this->data as $field => $value)
245258
{
246259
if (! empty($this->setFields[$field]))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace PHPFUI\ConstantContact\Exception;
4+
5+
class RequiredField extends \PHPFUI\ConstantContact\Exception
6+
{
7+
}

0 commit comments

Comments
 (0)