Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add toArray support to AbstractEntity

  • Loading branch information...
commit 6abf7acc034f1a866ad6d10da3fd544690b97650 1 parent 49de7be
@jeremiahsmall authored
Showing with 44 additions and 0 deletions.
  1. +44 −0 library/Soliant/SimpleFM/ZF2/Entity/AbstractEntity.php
View
44 library/Soliant/SimpleFM/ZF2/Entity/AbstractEntity.php
@@ -34,6 +34,11 @@
protected $fieldMap;
/**
+ * @var array
+ */
+ protected $entityAsArray;
+
+ /**
* This property is marked TRUE by the constructor and may be updated by unserializeField()
* to allow serialization logic to avoid unintentional nullification of existing field values.
* @var boolean
@@ -187,6 +192,24 @@ public function serialize()
}
+ /**
+ * @return the $entityAsArray
+ */
+ public function toArray() {
+
+ $this->addPropertyToEntityAsArray('recid');
+ $this->addPropertyToEntityAsArray('modid');
+
+ foreach ($this->fieldMap[get_class($this)]['writeable'] as $property=>$field) {
+ $this->addPropertyToEntityAsArray($property);
+ }
+
+ foreach ($this->fieldMap[get_class($this)]['readonly'] as $property=>$field) {
+ $this->addPropertyToEntityAsArray($property);
+ }
+
+ return $this->entityAsArray;
+ }
/**
* For unserialize, optimized layouts are permitted to omit fields defined by the entity.
@@ -243,5 +266,26 @@ protected function serializeField($fileMakerFieldName, $propertyName)
}
}
+ /**
+ * For toArray, all fields should be mapped
+ * @param string $propertyName
+ * @throws InvalidArgumentException
+ * @throws Exception
+ */
+ protected function addPropertyToEntityAsArray($propertyName)
+ {
+ $getterName = 'get' . ucfirst($propertyName);
+
+ try {
+ $this->entityAsArray[$propertyName] = $this->$getterName();
+ } catch (\Exception $e) {
+ if (!is_callable($this, $getterName)){
+ throw new InvalidArgumentException($getterName . ' is not a valid getter.', '', $e);
+ } else {
+ throw $e;
+ }
+ }
+
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.