/** * Test a GET request for a RestfulItem with fields specified to return. */ public function testGetWithFields() { /** @var MockPersistent $itemObj */ $itemObj = MockPersistent::findOne(['name' => 'MockPersistent #1'], [], $this->fixture); $this->mockEnvironment(['PATH_INFO' => '/collection/' . $itemObj->_id, 'QUERY_STRING' => 'fields=name,text', 'REQUEST_METHOD' => 'GET']); try { $response = $this->tacit->invoke(); $item = json_decode($response->getBody(), true); $data = $itemObj->toArray(Collection::getMask($itemObj, ['integer', 'float', 'date', 'arrayOfStrings'])); $matches = array_uintersect_assoc($data, $item, array($this, 'compareMultidimensionalArray')); $this->assertEquals($data, $matches); } catch (RestfulException $e) { $this->fail($e->getMessage()); } }
/** * Insert this model into the repository. * * @throws ModelValidationException If the insert fails. * @return bool Returns true if successful; false otherwise. */ protected function insert() { return static::collection($this->getRepository())->insert(Collection::getPublicVars($this)) !== false; }
public function __construct($name, &$connection) { parent::__construct($name, $connection); }
/** * PUT the representation of this item specified in the request entity. * * NOTE: This will replace the item with the properties specified in request * entity. Properties not provided will be reset to default values for the item. */ public function put() { /** @var \Tacit\Model\Persistent $modelClass */ $modelClass = static::$modelClass; $criteria = $this->criteria(func_get_args()); /** @var \Tacit\Model\Persistent $item */ $item = $modelClass::findOne($criteria, [], $this->app->container->get('repository')); if (null === $item) { throw new NotFoundException($this); } try { /** @var \Tacit\Model\Persistent $newItem */ $newItem = new $modelClass(); $data = array_replace_recursive(array_filter($newItem->toArray(), [$this, 'filterNull']), $this->app->request->post(null, [])); $data = $this->putBeforeSet($data); $item->fromArray($data, Collection::getMask($item)); $item->save(); } catch (OperationalException $e) { $e->next($this); } catch (ModelValidationException $e) { throw new UnacceptableEntityException($this, 'Resource validation failed', $e->getMessage(), $e->getMessages(), $e); } catch (\Exception $e) { throw new ServerErrorException($this, 'Error updating resource', $e->getMessage(), null, $e); } $this->respondWithItem($item, new static::$transformer()); }
/** * Get an array representation of this model. * * @param bool|array $mask * @param bool $cast * @return array */ public function toArray($mask = false, $cast = true) { $vars = Collection::getPublicVars($this); if ($mask === true) { $mask = array_keys($vars); } $array = array(); if (is_array($mask) && !empty($mask)) { foreach ($mask as $key) { if (array_key_exists($key, $vars)) { $varValue = $vars[$key]; if ($cast === true) { $varValue = static::collection($this->getRepository())->cast($varValue); } $array[$key] = $varValue; } } } else { foreach ($vars as $varKey => $varValue) { if ($mask === false || in_array($varKey, $mask)) { if ($cast === true) { $varValue = static::collection($this->getRepository())->cast($varValue); } $array[$varKey] = $varValue; } } } return $array; }
/** * Test Collection::getMask() with an object instance. * * @param array $expected * @param array $exclude * * @dataProvider providerGetMaskFromClass */ public function testGetMaskFromClass(array $expected, array $exclude) { $this->assertEquals($expected, array_values(Collection::getMask('Tacit\\Test\\Model\\MockPersistent', $exclude))); }