setModel(new Error()) ->setModel(new ErrorDev()) ->setModel(new User()) ->setModel(new Session()) ->setModel(new Locale()) ->setModel(new Team()) ->setModel(new TeamList()) ->setModel(new Membership()) ->setModel(new MembershipList()) ; parent::__construct($time); } /** * HTTP content types */ const CONTENT_TYPE_YAML = 'application/x-yaml'; /** * List of defined output objects */ protected $models = []; /** * Set Model Object * * @return self */ public function setModel(Model $instance): self { $this->models[$instance->getType()] = $instance; return $this; } /** * Get Model Object * * @return Model */ public function getModel(string $key): Model { if(!isset($this->models[$key])) { throw new Exception('Undefined model: '.$key); } return $this->models[$key]; } /** * Validate response objects and outputs * the response according to given format type */ public function dynamic(Document $document, string $model) { return $this->json($this->output($document, $model)); } /** * Generate valid response object from document data */ protected function output(Document $document, string $model): array { $data = $document; $model = $this->getModel($model); $output = []; foreach($model->getRules() as $key => $rule) { if(!$document->isSet($key)) { if(!is_null($rule['default'])) { $document->setAttribute($key, $rule['default']); } else { throw new Exception('Missing response key: '.$key); } } if($rule['array']) { if(!is_array($data[$key])) { throw new Exception($key.' must be an array of '.$rule['type'].' types'); } foreach ($data[$key] as &$item) { if(array_key_exists($rule['type'], $this->models) && $item instanceof Document) { $item = $this->output($item, $rule['type']); } } } $output[$key] = $data[$key]; } return $output; } /** * YAML * * This helper is for sending YAML HTTP response. * It sets relevant content type header ('application/x-yaml') and convert a PHP array ($data) to valid YAML using native yaml_parse * * @see https://en.wikipedia.org/wiki/YAML * * @param array $data * * @return void */ public function yaml(array $data): void { if(!extension_loaded('yaml')) { throw new Exception('Missing yaml extension. Learn more at: https://www.php.net/manual/en/book.yaml.php'); } $this ->setContentType(Response::CONTENT_TYPE_YAML) ->send(yaml_emit($data, YAML_UTF8_ENCODING)) ; } }