Skip to content

Instantly share code, notes, and snippets.

@kmkmjhyiiiu
Last active February 14, 2024 14:40
Show Gist options
  • Save kmkmjhyiiiu/1313b83527307908af8fd31deaadd73b to your computer and use it in GitHub Desktop.
Save kmkmjhyiiiu/1313b83527307908af8fd31deaadd73b to your computer and use it in GitHub Desktop.
PHP Challenge
<?php
/**
* Author class
*/
class Author {
protected $name;
protected $email;
/**
* This will allow us to set name and email of author.
* @param [string] $name [name of author]
* @param [string] $email [email of author]
*/
public function __construct($name, $email)
{
$this->name = $name;
$this->email = $email;
}
/**
* get name of author.
* @return [string]
*/
public function getName()
{
return $this->name;
}
/**
* get email of author.
* @return [string]
*/
public function getEmail()
{
return $this->email;
}
}
/**
* Article
*/
class Article implements JsonSerializable {
protected $title;
protected $body;
protected $status;
/**
* Author property
* @var [object]
*/
protected $author;
/**
* Gets Author Object
* @param Author $author
* @return object
*/
public function getAuthor()
{
$this->author = $author;
}
/**
* Gets the title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Gets the body
*
* @return string
*/
public function getBody()
{
return $this->body;
}
/**
* Gets the status
*
* @return string
*/
public function getStatus()
{
return $this->status;
}
/**
* Sets the title parameter
*
* @param string $title The article title
*
* @return void
*/
public function setTitle(String $title)
{
$this->title = $title;
}
/**
* Sets the body parameter
*
* @param string $body The article body
*
* @return void
*/
public function setBody(String $body)
{
$this->body = $body;
}
/**
* Set Author Object
* @param Author $author
*
* @return void
*/
public function setAuthor(Author $author)
{
$this->author = $author;
}
/**
* Sets Status.
* @param String $status
*/
public function setStatus(String $status)
{
$this->status = $status;
}
/**
* checks if property is set.
* @param [string] $property [description]
*
* @throws LogicException | RuntimeException
*
* @return void
*/
public function checkIfSet(String $property)
{
if(!property_exists($this, $property)) {
throw new LogicException("Property <b>$$property<b> is not found in class: <b>Article</b>", 1);
}
/**
* check if method set
*/
if(!isset($this->$property)) {
throw new LogicException("Property <b>$$property</b> is not set!", 1);
}
}
/**
* Validate the object.
*
* Ensure that the title, body, status, and author have been set.
* Ensure that the title is not longer than 80 characters.
* Ensure that the body has no HTML tags.
* Ensure that the status is either 'Active' or 'Inactive'.
*
* If any assertions fail, a LogicException will be thrown with
* appropriate messages.
*
* @throws LogicException
*
* @return void
*/
public function validate()
{
/**
* title validation.
*/
$this->checkIfSet('title');
if (strlen($this->title) >= 80) {
throw new LogicException('Property <b>$title</b> Should be less than 80 characters.', 1);
}
/**
* Body Validation
*/
$this->checkIfSet('body');
// strip body
$body = strlen(strip_tags($this->body));
if(strlen($this->body) != $body) {
throw new LogicException('Property <b>$Body</b> Should not contains HTML.', 1);
}
/**
* Status Validation.
*/
$this->checkIfSet('status');
if ($this->status !== 'Active' && $this->status !== 'Inactive'):
throw new LogicException('Property <b>$status</b> Must Active or Inactive.', 1);
endif;
/**
* check if author object is set.
*/
$this->checkIfSet('author');
}
/**
* Json interface method
*/
public function jsonSerialize() {
/**
* valiate it
*/
$this->validate();
$data = [
'author_name' => $this->author->getName(),
'title' => $this->getTitle(),
'body' => $this->getBody()
];
return json_encode($data);
}
/**
* get data according to written in challenge.
* @return String Json
*/
public function get_data() {
/**
* set Json Header
*/
header('Content-type: application/json');
return $this->jsonSerialize();
}
/**
* Not Good Pratice but seems OK.
* @return string json
*/
public function __toString()
{
return $this->get_data();
}
}
$article = new Article();
$article->setTitle('This is title');
$article->setBody('This is Body');
$article->setStatus('Inactive');
$author = new Author('AAAAAAAAAAAAAA', 'asaasaaaa@gmail.com');
$article->setAuthor($author);
echo $article; // or $article->get_data();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment