Skip to content
Browse files

Add method required fields and objects properties for easier checking…

… of required data in modRestController requests
  • Loading branch information...
1 parent c654f89 commit 11ca312172a028d66cb7c58659c73f922e87af9b splittingred committed Apr 23, 2012
Showing with 118 additions and 9 deletions.
  1. +118 −9 core/model/modx/rest/modrestcontroller.class.php
View
127 core/model/modx/rest/modrestcontroller.class.php
@@ -75,6 +75,23 @@
/** @var array $searchFields Optional. An array of fields to use when the search parameter is passed */
public $searchFields = array();
+ /** @var array $postRequiredFields An array of required field keys that must be passed for POST requests */
+ public $postRequiredFields = array();
+ /** @var array $postRequiredRelatedObjects An array of classKey/field pairings for checking related objects on POST */
+ public $postRequiredRelatedObjects = array();
+ /** @var string $postMethod The method on the object to call for POST requests */
+ public $postMethod = 'save';
+ /** @var array $putRequiredFields An array of required field keys that must be passed for PUT requests */
+ public $putRequiredFields = array();
+ /** @var array $postRequiredRelatedObjects An array of classKey/field pairings for checking related objects on PUT */
+ public $putRequiredRelatedObjects = array();
+ /** @var string $putMethod The method on the object to call for PUT requests */
+ public $putMethod = 'save';
+ /** @var array $deleteRequiredFields An array of required field keys that must be passed for DELETE requests */
+ public $deleteRequiredFields = array();
+ /** @var string $deleteMethod The method on the object to call for DELETE requests */
+ public $deleteMethod = 'remove';
+
/**
* @param modX $modx The modX instance
* @param modRestServiceRequest $request The rest service request class instance
@@ -147,6 +164,27 @@ public function checkRequiredFields(array $fields = array(),$setFieldError = tru
}
/**
+ * Check to ensure the existence of required related objects on the passed request
+ *
+ * @param array $pairs An array of arrays in the format: 'field' => 'classKey'
+ * @return boolean
+ */
+ public function checkRequiredRelatedObjects(array $pairs = array()) {
+ $passed = true;
+ foreach ($pairs as $field => $classKey) {
+ if (!empty($classKey) && !empty($field)) {
+ $relatedObject = $this->modx->getObject($classKey,$this->getProperty($field));
+ if (empty($relatedObject)) {
+ $objectName = substr($classKey,2);
+ $this->addFieldError($field,$this->modx->lexicon('err.obj_nf',array('name' => $objectName)));
+ $passed = false;
+ }
+ }
+ }
+ return $passed;
+ }
+
+ /**
* Get a REQUEST property for the controller
*
* @param string $key
@@ -337,6 +375,37 @@ public function clearErrorMessage() {
}
/**
+ * Set a default value for a property on this controller request.
+ * @param string $k The key of the field
+ * @param mixed $v The default value to set
+ * @param bool $useNotEmpty Whether or not to use empty() for checking set status
+ * @return boolean True if the default was used
+ */
+ public function setDefault($k,$v,$useNotEmpty = false) {
+ $isSet = false;
+ if ($useNotEmpty) {
+ if (!empty($this->properties[$k])) $isSet = true;
+ } else if (array_key_exists($k,$this->properties)) {
+ $isSet = true;
+ }
+ if (!$isSet) {
+ $this->properties[$k] = $v;
+ }
+ return !$isSet;
+ }
+
+ /**
+ * Set an array of default values for properties on this controller request
+ * @param array $array
+ * @param bool $useNotEmpty
+ */
+ public function setDefaults(array $array,$useNotEmpty = false) {
+ foreach ($array as $k => $v) {
+ $this->setDefault($k,$v,$useNotEmpty);
+ }
+ }
+
+ /**
* Output a collection of objects as a list.
*
* @param array $list
@@ -510,18 +579,35 @@ public function afterRead(array &$objectArray) {
public function post() {
$properties = $this->getProperties();
+ if (!empty($this->postRequiredFields)) {
+ if (!$this->checkRequiredFields($this->postRequiredFields)) {
+ return $this->failure($this->modx->lexicon('error'));
+ }
+ }
+
+ if (!empty($this->postRequiredRelatedObjects)) {
+ if (!$this->checkRequiredRelatedObjects($this->postRequiredRelatedObjects)) {
+ return $this->failure();
+ }
+ }
+
/** @var xPDOObject $object */
$this->object = $this->modx->newObject($this->classKey);
$this->object->fromArray($properties);
$beforePost = $this->beforePost();
if ($beforePost !== true && $beforePost !== null) {
return $this->failure($beforePost === false ? $this->errorMessage : $beforePost);
}
- if (!$this->object->save()) {
+ if (!$this->object->{$this->postMethod}()) {
$this->setObjectErrors();
- return $this->failure($this->modx->lexicon('rest.err_class_save',array(
- 'class_key' => $this->classKey,
- )));
+ if ($this->hasErrors()) {
+ return $this->failure();
+ } else {
+ return $this->failure($this->modx->lexicon('rest.err_class_save',array(
+ 'class_key' => $this->classKey,
+ )));
+ }
+
}
$objectArray = $this->object->toArray();
$this->afterPost($objectArray);
@@ -560,17 +646,33 @@ public function put() {
)));
}
+ if (!empty($this->putRequiredFields)) {
+ if (!$this->checkRequiredFields($this->putRequiredFields)) {
+ return $this->failure();
+ }
+ }
+
+ if (!empty($this->putRequiredRelatedObjects)) {
+ if (!$this->checkRequiredRelatedObjects($this->putRequiredRelatedObjects)) {
+ return $this->failure();
+ }
+ }
+
$this->object->fromArray($this->getProperties());
$beforePut = $this->beforePut();
if ($beforePut !== true && $beforePut !== null) {
return $this->failure($beforePut === false ? $this->errorMessage : $beforePut);
}
- if (!$this->object->save()) {
+ if (!$this->object->{$this->putMethod}()) {
$this->setObjectErrors();
- return $this->failure($this->modx->lexicon('rest.err_class_save',array(
- 'class_key' => $this->classKey,
- )));
+ if ($this->hasErrors()) {
+ return $this->failure();
+ } else {
+ return $this->failure($this->modx->lexicon('rest.err_class_save',array(
+ 'class_key' => $this->classKey,
+ )));
+ }
}
$objectArray = $this->object->toArray();
@@ -610,13 +712,19 @@ public function delete() {
)));
}
+ if (!empty($this->deleteRequiredFields)) {
+ if (!$this->checkRequiredFields($this->deleteRequiredFields)) {
+ return $this->failure();
+ }
+ }
+
$this->object->fromArray($this->getProperties());
$beforeDelete = $this->beforeDelete();
if ($beforeDelete !== true) {
return $this->failure($beforeDelete === false ? $this->errorMessage : $beforeDelete);
}
- if (!$this->object->remove()) {
+ if (!$this->object->{$this->deleteMethod}()) {
$this->setObjectErrors();
return $this->failure($this->modx->lexicon('rest.err_class_remove',array(
'class_key' => $this->classKey,
@@ -674,4 +782,5 @@ public function setObjectFields(&$object,array $values = array()) {
}
return $object;
}
+
}

0 comments on commit 11ca312

Please sign in to comment.
Something went wrong with that request. Please try again.