/** * 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; }
/** * 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; }
/** * Test Collection::getPublicVars() with a class name. */ public function testGetPublicVarsFromClass() { $this->assertEquals(['_id', 'name', 'text', 'date', 'integer', 'float', 'boolean', 'password', 'arrayOfStrings'], array_keys(Collection::getPublicVars('Tacit\\Test\\Model\\MockPersistent'))); }