/** * Build component * * @param array|string $component * @return array */ protected function buildComponent($component) { $componentManager = $this->componentManager; $transformObj = function ($item) use($componentManager) { if (ArrayUtils::isHashTable($item) && isset($item['cmp'])) { $itemObj = $componentManager->get($item['cmp']); $itemObj->setProperties($item); unset($itemObj['cmp'], $itemObj['extend']); $item = $itemObj; } return $item; }; // Recursive mapping, convert to class later $map = function ($func, $arr) use(&$map, $transformObj) { $result = array(); if (ArrayUtils::isHashTable($arr)) { foreach ($arr as $k => $v) { $result[$k] = $map($func, $v); } $result = $transformObj($result); } elseif (ArrayUtils::isList($arr)) { foreach ($arr as $b) { $result[] = $transformObj($b); } } elseif (is_string($arr)) { $result = $arr; } return $result; }; return $map(function ($item) { return $item; }, $component); }
/** * Option setter with validation * If option can have the specified value then it is set, otherwise this method throws exception * * Tip: call it into your setter methods. * * @param $key * @param $value * @return $this * @throws Exception\DomainException * @throws Exception\InvalidArgumentException */ protected function setOption($key, $value) { if (!isset($this->config[$key])) { throw new Exception\DomainException(sprintf('Option "%s" does not exist; available options are (%s)', $key, implode(', ', array_map(function ($opt) { return '"' . $opt . '"'; }, array_keys($this->config))))); } if (!ArrayUtils::isList($this->config[$key], false)) { throw new Exception\DomainException(sprintf('Option "%s" does not have a list of allowed values', $key)); } if (!ArrayUtils::inArray($value, $this->config[$key], true)) { throw new Exception\InvalidArgumentException(sprintf('Option "%s" can not be set to value "%s"; allowed values are (%s)', $key, $value, implode(', ', array_map(function ($val) { return '"' . $val . '"'; }, $this->config[$key])))); } $this->options[$key] = $value; return $this; }
/** * Write a value * * @param mixed $value * @throws Exception\RuntimeException on invalid or unrecognized value type */ protected function write($value) { if ($value === null) { $this->writeNull(); } elseif (is_bool($value)) { $this->writeBool($value); } elseif (is_int($value)) { $this->writeInt($value); } elseif (is_float($value)) { $this->writeFloat($value); } elseif (is_string($value)) { // TODO: write unicode / binary $this->writeString($value); } elseif (is_array($value)) { if (ArrayUtils::isList($value)) { $this->writeArrayList($value); } else { $this->writeArrayDict($value); } } elseif (is_object($value)) { $this->writeObject($value); } else { throw new Exception\RuntimeException(sprintf('PHP-Type "%s" can not be serialized by %s', gettype($value), get_called_class())); } }
public function testEmptyArrayReturnsFalse() { $test = array(); $this->assertFalse(ArrayUtils::hasStringKeys($test, false)); $this->assertFalse(ArrayUtils::hasIntegerKeys($test, false)); $this->assertFalse(ArrayUtils::hasNumericKeys($test, false)); $this->assertFalse(ArrayUtils::isList($test, false)); $this->assertFalse(ArrayUtils::isHashTable($test, false)); }
/** * @return bool */ public function isList() { return ArrayUtils::isList($this->data()); }
/** * @param array $data * @return array */ protected function extractResourceFromHal(array $data, $promoteTopCollection = true) { if (array_key_exists('_links', $data)) { unset($data['_links']); } if (array_key_exists('_embedded', $data)) { $embedded = $data['_embedded']; foreach ($embedded as $key => $resourceNode) { if (ArrayUtils::isList($resourceNode, true)) { //assume is a collection of resources $temp = []; foreach ($resourceNode as $resource) { $temp[] = $this->extractResourceFromHal($resource, false); } if ($promoteTopCollection) { if (count($embedded) > 1) { throw new Exception\RuntimeException('Cannot promote multiple top collections'); } $data = $temp; break; } else { $data[$key] = $temp; } } else { //assume is a single resource $data[$key] = $this->extractResourceFromHal($resourceNode, false); } } unset($data['_embedded']); } return $data; }
public function setSearchFields(array $fields) { if (ArrayUtils::isList($fields)) { $this->searchFields = array_flip($fields); } else { $this->searchFields = $fields; } return $this; }
/** * Bulk Process Data, handles POST, PUT & DELETE * * @param array $data * @param string $entityName * @param string $entityIdentifier * @return JsonModel * @throws Exception\InvalidArgumentException * @throws Exception\RuntimeException */ public function processBulkData(array $data, $entityName, $entityIdentifier = 'id') { $data = reset($data); if (!ArrayUtils::isList($data)) { throw new Exception\InvalidArgumentException('Invalid bulk data provided'); } $errors = false; $responses = array(); foreach ($data as $entityArray) { try { switch ($this->getRequest()->getMethod()) { case 'POST': $result = $this->create(array($entityName => $entityArray)); break; case 'PUT': $id = isset($entityArray[$entityIdentifier]) ? $entityArray[$entityIdentifier] : null; $result = $this->update($id, array($entityName => $entityArray)); break; case 'PATCH': $id = isset($entityArray[$entityIdentifier]) ? $entityArray[$entityIdentifier] : null; $result = $this->patch($id, array($entityName => $entityArray)); break; case 'DELETE': $id = isset($entityArray[$entityIdentifier]) ? $entityArray[$entityIdentifier] : null; $result = $this->delete($id); break; default: throw new Exception\RuntimeException('Invalid HTTP method specificed for bulk action'); break; } } catch (ServiceException\ExceptionInterface $ex) { $result = new JsonModel(array('api-problem' => new ApiProblem($ex->getCode(), $ex))); } if (!$result instanceof JsonModel && !$result instanceof Response) { throw new Exception\RuntimeException(sprintf('Expected to receive a JsonModel or Response object, received %s', is_object($result) ? get_class($result) : gettype($result))); } if ($result instanceof Response) { $responses[] = array('code' => $result->getStatusCode(), 'headers' => $this->getResponse()->getHeaders()->toArray(), 'body' => null); if (!$result->isSuccess()) { $errors = true; } continue; } $problem = $result->getVariable('api-problem'); if ($problem instanceof ApiProblem) { $responses[] = array('code' => $problem->status, 'headers' => array('content-type' => 'application/api-problem+json'), 'body' => $problem->toArray()); $errors = true; } else { $responses[] = array('code' => $this->getResponse()->getStatusCode(), 'headers' => $this->getResponse()->getHeaders()->toArray(), 'body' => $result->getVariables()); } } if ($errors) { $this->getResponse()->setStatusCode(Response::STATUS_CODE_207); } else { switch ($this->getRequest()->getMethod()) { case 'POST': $this->getResponse()->setStatusCode(Response::STATUS_CODE_201); break; case 'PUT': case 'PATCH': $this->getResponse()->setStatusCode(Response::STATUS_CODE_200); break; case 'DELETE': $this->getResponse()->setStatusCode(Response::STATUS_CODE_204); break; } } return new JsonModel(array('responses' => $responses)); }