Skip to content

Commit 3049c24

Browse files
committed
Better custom field support
1 parent 7095ddf commit 3049c24

File tree

3 files changed

+100
-16
lines changed

3 files changed

+100
-16
lines changed

Tests/CustomFieldTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHPFUI\ConstantContact package
5+
*
6+
* (c) Bruce Wells
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source
10+
* code
11+
*/
12+
class CustomFieldTest extends \PHPUnit\Framework\TestCase
13+
{
14+
public function testGetSet() : void
15+
{
16+
$string = 'A long string';
17+
$fixture = new \Tests\Fixtures\CustomFieldTest();
18+
$fixture->cf_string = $string;
19+
$this->assertEquals($string, $fixture->cf_string);
20+
$this->assertIsString($fixture->cf_string);
21+
}
22+
23+
public function testBadIntType() : void
24+
{
25+
$fixture = new \Tests\Fixtures\CustomFieldTest();
26+
$this->expectException(\PHPFUI\ConstantContact\Exception\InvalidType::class);
27+
$fixture->cf_integer = 0;
28+
}
29+
30+
public function testBadFloatType() : void
31+
{
32+
$fixture = new \Tests\Fixtures\CustomFieldTest();
33+
$this->expectException(\PHPFUI\ConstantContact\Exception\InvalidType::class);
34+
$fixture->cf_float = 1.234;
35+
}
36+
37+
public function testBadBooleanType() : void
38+
{
39+
$fixture = new \Tests\Fixtures\CustomFieldTest();
40+
$this->expectException(\PHPFUI\ConstantContact\Exception\InvalidType::class);
41+
$fixture->cf_boolean = true;
42+
}
43+
44+
public function testBadArrayType() : void
45+
{
46+
$fixture = new \Tests\Fixtures\CustomFieldTest();
47+
$this->expectException(\PHPFUI\ConstantContact\Exception\InvalidType::class);
48+
$fixture->cf_array = [];
49+
}
50+
51+
public function testMaxLength() : void
52+
{
53+
$fixture = new \Tests\Fixtures\CustomFieldTest();
54+
$this->expectException(\PHPFUI\ConstantContact\Exception\InvalidLength::class);
55+
$fixture->cf_string = \str_pad('', 300);
56+
}
57+
58+
}

Tests/Fixtures/CustomFieldTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Tests\Fixtures;
4+
5+
class CustomFieldTest extends \PHPFUI\ConstantContact\Definition\Base
6+
{
7+
protected static array $fields = [
8+
'cf:custom_field_name' => 'string',
9+
10+
];
11+
12+
protected static array $maxLength = [
13+
'cf:custom_field_name' => 255,
14+
15+
];
16+
}

src/ConstantContact/Definition/Base.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,36 @@ public function __construct(array $initialValues = [])
7676
*/
7777
public function __get(string $field) : mixed
7878
{
79+
$actualField = $field;
80+
if (str_starts_with($field, 'cf_'))
81+
{
82+
$field = 'cf:custom_field_name';
83+
}
7984
if (! isset(static::$fields[$field]))
8085
{
81-
throw new \PHPFUI\ConstantContact\Exception\InvalidField(static::class . "::{$field} is not a valid field");
86+
throw new \PHPFUI\ConstantContact\Exception\InvalidField(static::class . "::{$actualField} is not a valid field");
8287
}
8388

84-
$this->setFields[$field] = true;
89+
$this->setFields[$actualField] = true;
8590

86-
return $this->data[$field] ?? null;
91+
return $this->data[$actualField] ?? null;
8792
}
8893

8994
/**
9095
* @return mixed value being set to allow for assignment chaining
9196
*/
9297
public function __set(string $field, $value)
9398
{
99+
$actualField = $field;
100+
if (str_starts_with($field, 'cf_'))
101+
{
102+
$field = 'cf:custom_field_name';
103+
}
94104
$expectedType = static::$fields[$field] ?? null;
95105

96106
if (null === $expectedType)
97107
{
98-
throw new \PHPFUI\ConstantContact\Exception\InvalidField(static::class . "::{$field} is not a valid field");
108+
throw new \PHPFUI\ConstantContact\Exception\InvalidField(static::class . "::{$actualField} is not a valid field");
99109
}
100110

101111
$type = \get_debug_type($value);
@@ -104,7 +114,7 @@ public function __set(string $field, $value)
104114
{
105115
if (! \in_array($value, $expectedType))
106116
{
107-
throw new \PHPFUI\ConstantContact\Exception\InvalidValue(static::class . "::{$field} is {$value} but must be one of " . \implode(', ', $expectedType));
117+
throw new \PHPFUI\ConstantContact\Exception\InvalidValue(static::class . "::{$actualField} is {$value} but must be one of " . \implode(', ', $expectedType));
108118
}
109119
}
110120
else
@@ -136,7 +146,7 @@ public function __set(string $field, $value)
136146

137147
if ($arrayType != $elementType)
138148
{
139-
throw new \PHPFUI\ConstantContact\Exception\InvalidType(static::class . "::{$field} should be an array[{$arrayType}] but index {$index} is of type {$elementType}");
149+
throw new \PHPFUI\ConstantContact\Exception\InvalidType(static::class . "::{$actualField} should be an array[{$arrayType}] but index {$index} is of type {$elementType}");
140150
}
141151
}
142152
}
@@ -147,7 +157,7 @@ public function __set(string $field, $value)
147157
}
148158
elseif ($expectedType != $type)
149159
{
150-
throw new \PHPFUI\ConstantContact\Exception\InvalidType(static::class . "::{$field} is of type {$type} but should be type {$expectedType}");
160+
throw new \PHPFUI\ConstantContact\Exception\InvalidType(static::class . "::{$actualField} is of type {$type} but should be type {$expectedType}");
151161
}
152162
}
153163

@@ -159,16 +169,16 @@ public function __set(string $field, $value)
159169
{
160170
if (\count($value) < $minLength)
161171
{
162-
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$field} array must have at least {$minLength} values");
172+
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$actualField} array must have at least {$minLength} values");
163173
}
164174
}
165175
elseif ((\is_int($value) || \is_float($value)) && $value < $minLength)
166176
{
167-
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$field} must be at least {$minLength}");
177+
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$actualField} must be at least {$minLength}");
168178
}
169179
elseif (\strlen($value) < $minLength)
170180
{
171-
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$field} must be at least {$minLength} characters long");
181+
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$actualField} must be at least {$minLength} characters long");
172182
}
173183
}
174184

@@ -180,16 +190,16 @@ public function __set(string $field, $value)
180190
{
181191
if (\count($value) > $maxLength)
182192
{
183-
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$field} array has a limit of {$maxLength} values");
193+
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$actualField} array has a limit of {$maxLength} values");
184194
}
185195
}
186196
elseif ((\is_int($value) || \is_float($value)) && $value > $maxLength)
187197
{
188-
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$field} must be equal or less than {$maxLength}");
198+
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$actualField} must be equal or less than {$maxLength}");
189199
}
190200
elseif (\strlen($value) > $maxLength)
191201
{
192-
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$field} must be at less than {$maxLength} characters long");
202+
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$actualField} must be at less than {$maxLength} characters long");
193203
}
194204
}
195205

@@ -203,9 +213,9 @@ public function __set(string $field, $value)
203213
break;
204214
}
205215

206-
$this->setFields[$field] = true;
216+
$this->setFields[$actualField] = true;
207217

208-
return $this->data[$field] = $value;
218+
return $this->data[$actualField] = $value;
209219
}
210220

211221
/**
@@ -246,7 +256,7 @@ public function getData() : array
246256
}
247257
else
248258
{
249-
$result[$field] = \is_object($value) ? (string)$value : $value;
259+
$result[str_replace('cf_', 'cf:', $field)] = \is_object($value) ? (string)$value : $value;
250260
}
251261
}
252262
}

0 commit comments

Comments
 (0)