PHP class for publishing posts open

PHP class for publishing posts

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 7

Let’s start by creating a new interface:

interface IStorage{
    //in the interface we announce the methods and data that our methods should return
	public function create() : int;
	public function get(int $id) : ?array;// ?array - emptiness may return
	public function remove(int $id) : bool;
	public function update(int $id, array $fields) : bool;
}

Write the base class of our post:

class Article{
	protected int $id; 
	public string $title; 
	public string $content;
    // use the interface as a data type
	protected IStorage $storage;


public function __construct(){
		$this->storage = new FileStorage('articles.txt');
	}

//call the create method in our Article class object
	public function create(){
		$this->id = $this->storage->create();
	}


// after creating an object with the create method and getting it id create a load class that will load our article

public function load (int $id) {
$data = $this->storage->get($id);

 // if empty data came, throw out the error
if ($ data === null) {
     throw new Exception ("article with id = $id not found");
}

  // put the data in a variable
$this->id = $id;
$this->title = $data['title'];
$this->content = $data['content'];
}

public function save(){
		$this->storage->update($this->id, [
			'title' => $this->title,
			'content' => $this->content
		]);
	}
}

Create class of our storage:

//implement our IStorage interface
class MemoryStorage implements IStorage{
	protected array $records = [];
	protected int $ai = 0;

	public function create() : int{
		$id = ++$this->ai;
		$this->records[$id] = [];
		return $id;
	}

	public function get(int $id) : ?array {
		return $this->records[$id] ?? null;
	}

	public function remove(int $id) : bool{
		if(array_key_exists($id, $this->records)){
			unset($this->records[$id]);
			return true;
		}

		return false;
	}

	public function update(int $id, array $fields) : bool{
		if(array_key_exists($id, $this->records)){
			$this->records[$id] = $fields;
			return true;
		}

		return false;
	}
}
class FileStorage implements IStorage{
	protected array $records = [];
	protected int $ai = 0;
	protected string $dbPath;

	public function __construct(string $dbPath){
		$this->dbPath = $dbPath;

		if(file_exists($this->dbPath)){
			$data = json_decode(file_get_contents($this->dbPath), true);
			$this->records = $data['records'];
			$this->ai = $data['ai'];
		}
	}

	public function create() : int{
		$id = ++$this->ai;
		$this->records[$id] = [];
		$this->save();
		return $id;
	}

	public function get(int $id) : ?array{
		return $this->records[$id] ?? null;
	}

	public function remove(int $id) : bool{
		if(array_key_exists($id, $this->records)){
			unset($this->records[$id]);
			$this->save();
			return true;
		}

		return false;
	}

	public function update(int $id, array $fields) : bool{
		if(array_key_exists($id, $this->records)){
			$this->records[$id] = $fields;
			$this->save();
			return true;
		}

		return false;
	}

	protected function save(){
		file_put_contents($this->dbPath, json_encode([
			'records' => $this->records,
			'ai' => $this->ai
		]));
	}
}

You can now perform operations on our article:

//create a new instance of the class
$article = new Article();

//call the create method
$article->create();
//add the title property 
$article->title = 'First article';
//add the property $ content
$art1->content = 'Secobd article';
//save the article by calling the save method
$art1->save();
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation