Skip to content

Commit

Permalink
Add TypeInterface::serialize() functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ilijastuden committed Nov 16, 2015
1 parent f2b8d8d commit 77242cc
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 5 deletions.
73 changes: 69 additions & 4 deletions src/Builder/BaseTypeClassBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,13 @@ public function buildType(TypeInterface $type)

foreach ($fields as $field) {
if ($field instanceof ScalarField && $field->getShouldBeAddedToModel() && $field->getName() != 'id') {
$camelized_field_name = Inflector::classify($field->getName());

$result[] = '';
$result[] = ' /**';
$result[] = ' * Return value of ' . $field->getName() . ' field';
$result[] = ' *';
$result[] = ' * @return ' . $field->getNativeType();
$result[] = ' */';
$result[] = ' public function get' . $camelized_field_name . '()';
$result[] = ' public function ' . $this->getGetterName($field->getName()) . '()';
$result[] = ' {';
$result[] = ' return $this->getFieldValue(' . var_export($field->getName(), true) . ');';
$result[] = ' }';
Expand All @@ -152,7 +150,7 @@ public function buildType(TypeInterface $type)
$result[] = ' * @param ' . $field->getNativeType() . ' $value';
$result[] = ' * @return $this';
$result[] = ' */';
$result[] = ' public function &set' . $camelized_field_name . '($value)';
$result[] = ' public function &' . $this->getSetterName($field->getName()) . '($value)';
$result[] = ' {';
$result[] = ' $this->setFieldValue(' . var_export($field->getName(), true) . ', $value);';
$result[] = '';
Expand Down Expand Up @@ -207,6 +205,7 @@ public function buildType(TypeInterface $type)
$result[] = ' return $this;';
$result[] = ' }';

$this->buildJsonSerialize($type->getSerialize(), ' ', $result);
$this->buildCompositeFieldMethods($type->getFields(), ' ', $result);
$this->buildValidate($type->getFields(), ' ', $result);

Expand Down Expand Up @@ -238,6 +237,35 @@ public function buildCompositeFieldMethods($fields, $indent, array &$result)
}
}

/**
* Build JSON serialize method, if we need to serialize extra fields
*
* @param array $serialize
* @param string $indent
* @param array $result
*/
public function buildJsonSerialize(array $serialize, $indent, array &$result)
{
if (count($serialize)) {
$result[] = '';
$result[] = $indent . '/**';
$result[] = $indent . ' * Prepare object properties so they can be serialized to JSON';
$result[] = $indent . ' *';
$result[] = $indent . ' * @return array';
$result[] = $indent . ' */';
$result[] = $indent . 'public function jsonSerialize()';
$result[] = $indent . '{';
$result[] = $indent . ' return array_merge(parent::jsonSerialize(), [';

foreach ($serialize as $field) {
$result[] = $indent . ' ' . var_export($field, true) . ' => $this->' . $this->getGetterName($field) . '(), ';
}

$result[] = $indent . ' ]);';
$result[] = $indent . '}';
}
}

/**
* @param FieldInterface[] $fields
* @param string $indent
Expand Down Expand Up @@ -351,4 +379,41 @@ private function buildValidatePresenceAndUniquenessLine($field_name, array $cont

return '$validator->presentAndUnique(' . implode(', ', $field_names) . ');';
}

/**
* @var array
*/
private $getter_names = [], $setter_names = [];

/**
* @param string $field_name
* @return string
*/
private function getGetterName($field_name)
{
if (empty($this->getter_names[$field_name])) {
$camelized_field_name = Inflector::classify($field_name);

$this->getter_names[$field_name] = "get{$camelized_field_name}";
$this->setter_names[$field_name] = "set{$camelized_field_name}";
}

return $this->getter_names[$field_name];
}

/**
* @param string $field_name
* @return string
*/
private function getSetterName($field_name)
{
if (empty($this->setter_names[$field_name])) {
$camelized_field_name = Inflector::classify($field_name);

$this->getter_names[$field_name] = "get{$camelized_field_name}";
$this->setter_names[$field_name] = "set{$camelized_field_name}";
}

return $this->setter_names[$field_name];
}
}
28 changes: 28 additions & 0 deletions src/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,4 +577,32 @@ public function &orderBy($order_by)

return $this;
}

/**
* @var array
*/
private $serialize = [];

/**
* Return a list of additional fields that will be included during object serialization
*
* @return array
*/
public function getSerialize()
{
return $this->serialize;
}

/**
* Set a list of fields that will be included during object serialization
*
* @param string ...$fields
* @return $this
*/
public function &serialize(...$fields)
{
$this->serialize = $fields;

return $this;
}
}
15 changes: 15 additions & 0 deletions src/TypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,19 @@ public function getOrderBy();
* @return $this
*/
public function &orderBy($order_by);

/**
* Return a list of additional fields that will be included during object serialization
*
* @return array
*/
public function getSerialize();

/**
* Set a list of fields that will be included during object serialization
*
* @param string ...$fields
* @return $this
*/
public function &serialize(...$fields);
}
16 changes: 16 additions & 0 deletions test/src/CodeBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ActiveCollab\DatabaseStructure\Test;

use ActiveCollab\DatabaseConnection\Test\Fixture\Writer;
use ActiveCollab\DatabaseStructure\Structure;
use ActiveCollab\DatabaseStructure\Test\Fixtures\Writers\WritersStructure;
use ReflectionClass;
Expand Down Expand Up @@ -271,4 +272,19 @@ public function testChaptersOrderBy()
$this->assertCount(1, $order_by);
$this->assertEquals('position', $order_by[0]);
}

/**
* Test if JSON serialize is properly defined in writer class
*/
public function testJsonSerializeIsDefinedInBookClass()
{
$json_serialize = $this->base_writer_reflection->getMethod('jsonSerialize');
$this->assertEquals($this->base_writer_reflection->getName(), $json_serialize->getDeclaringClass()->getName());

$json_serialize = $this->base_book_reflection->getMethod('jsonSerialize');
$this->assertEquals('ActiveCollab\DatabaseObject\Object', $json_serialize->getDeclaringClass()->getName());

$json_serialize = $this->base_chapter_reflection->getMethod('jsonSerialize');
$this->assertEquals('ActiveCollab\DatabaseObject\Object', $json_serialize->getDeclaringClass()->getName());
}
}
2 changes: 1 addition & 1 deletion test/src/Fixtures/Writers/WritersStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function configure()
new Index('birthday'),
])->addAssociations([
new HasManyAssociation('books'),
])->orderBy('name');
])->orderBy('name')->serialize('name', 'birthday');

$this->addType('books')->addFields([
(new NameField('title', '', true))->required()->unique('writer_id'),
Expand Down

0 comments on commit 77242cc

Please sign in to comment.