OOP - PHP Online
Form of PHP Sandbox
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of OOP.php
- <?php
- class Document
- {
- private string $filename;
- private string $content;
- private string $type;
- const storageTypeTemp = 'temp';
- const storageTypeSecret = 'secret';
- const storageTypeSimple = 'simple';
- public function __construct(string $filename, string $content, string $type)
- {
- $this->filename = $filename;
- $this->content = $content;
- $this->type = $type;
- }
- /**
- * @return string
- */
- public function getFilename(): string
- {
- return $this->filename;
- }
- /**
- * @return string
- */
- public function getContent(): string
- {
- return $this->content;
- }
- /**
- * @return string
- */
- public function getType(): string
- {
- return $this->type;
- }
- }
- //1. Parašykite interface StorageInterface.
- //- store(Document $document): void
- //- getContent(string $filename): string
- abstract class StorageInterface
- {
- public abstract function store(Document $document): void;
- public abstract function getContent(string $filename): string;
- public function printContent(string $filename): string
- {
- echo ('cia yra turinys ECHO') . $this->getContent($filename);
- }
- }
- //2. Parašykite klasę FileSystemStorage kuri implementuoja StorageInterface
- //- store funkicja saugo turiny file sistemoje galite naudoti
- //- getContent gauna turinį iš file sistemos.
- class FileSystemStorage extends StorageInterface
- {
- public function store(Document $document): void
- {
- file_put_contents($document->getFilename(), $document->getContent());
- }
- public function getContent(string $filename): string
- {
- return file_get_contents($filename);
- }
- public function printContent(string $filename): string
- {
- echo ('cia yra turinys ECHO') . $this->getContent($filename);
- }
- }
- //3. Parašykite klasę HashedFileSystemStorage kuri extendina FileSystemStorage
- //- store funkcija naudoja comandą base64_encode kad užhešuotų turinį saugomą failų sistemoje.
- //- getContent gauna turinį iš file sistemos. base64_decode naudokite iššifravimui
- class HashedFileSystemStorage extends FileSystemStorage
- {
- public function store(Document $document): void
- {
- file_put_contents($document->getFilename(), base64_encode($document->getContent()));
- }
- public function getContent(string $filename): string
- {
- return base64_decode(parent::getContent($filename));
- }
- }
- //4. Parašykite klasę TempStorage kuri implementuoja StorageInterface
- //- store funkcija pasaugo informaciją tiesiog array kintamajame.
- // - getContent gauna turinį iš kintamajo.
- class TempStorage extends StorageInterface
- {
- private array $temp = [];
- public function store(Document $document): void
- {
- $this->temp[$document->getFilename()] = $document->getContent();
- }
- public function getContent(string $filename): string
- {
- if (isset($this->temp[$filename])){
- return $this->temp[$filename];
- }
- else {
- return "";
- }
- }
- }
- // ----------------------------------------------------------------------
- //$simpleStorage = new FileSystemStorage();
- //$secretStorage = new HashedFileSystemStorage();
- //$tempStorage = new TempStorage();
- //
- //$secretStorage->store($documentSecret);
- //$simpleStorage->store($documentSimple);
- //$simpleStorage->store($documentSimple2);
- //$tempStorage->store($documentTemp);
- //var_dump($tempStorage->getContent('temp.txt'));
- //
- // ----------------------------------------------------------------------
- //NEBŪTINAS 5 žingnis jeigu yra noro ir laiko:
- //5. Parašykite klasę CookieStorage, kuri implementuoja StorageInterface
- //- store funkcija pasaugo informaciją teisiog cookie
- //- getContent gauna informacija iš cookie
- //6. Sukurkite skirtingus storage tipus
- //- sukurkite Documentus su skirtingais tipais ir pabandykite patalpinti juos naudodami atitinkamus storage tipus.
- //
- //
- //7. Tam kad nereiktų spėlioti, kur kokiam storage viskas gyvena, Parašykite StorageContext clasę, kuri atliks funkcijas:
- // - store(Document $document) //pagal documento tipa i atitinkama storage deda informaciją
- // - getContentFromTheStorage(string $filename, string $documentType) //pagal documento tipa is atitinkamos storage traukia informacija
- //!Pastaba pabandykite pagalvoti, kaip būtų galima eliminuoti if'us arba switch.
- class StorageContext {
- public function store(Document $document)
- {
- $simpleStorage = new FileSystemStorage();
- $secretStorage = new HashedFileSystemStorage();
- $tempStorage = new TempStorage();
- if ($document->getType() == 'simple') {
- $simpleStorage->store($document);
- }
- if ($document->getType() == 'secret') {
- $secretStorage->store($document);
- } else {
- $tempStorage->store($document);
- }
- }
- public function getContentFromTheStorage(string $filename, string $documentType)
- {
- $simpleStorage = new FileSystemStorage();
- $secretStorage = new HashedFileSystemStorage();
- $tempStorage = new TempStorage();
- switch ($documentType) {
- case Document::storageTypeSimple:
- return $simpleStorage->getContent($filename);
- case Document::storageTypeSecret:
- return $secretStorage->getContent($filename);
- case Document::storageTypeTemp:
- return $tempStorage->getContent($filename);
- }
- }
- }
- $documentSecret = new Document("secret_Bandymas.txt", "Labai slaptas turinys", Document::storageTypeSecret);
- $documentSimple = new Document("simple.txt", "Labai paprastas turinys", Document::storageTypeSimple);
- $documentSimple2 = new Document("simple2.txt", "Dar vienas labai paprastas turinys", Document::storageTypeSimple);
- $documentTemp = new Document('tem.txt', 'Laikinas turinys', Document::storageTypeTemp);
- $storage = new StorageContext();
- $storage->store($documentSecret);
- $storage->store($documentSimple);
- //$storage->store(new Document("test.txt", "test", Document::storageTypeSimple));
- var_dump('Cia yra failo turinys: ' . $storage->getContentFromTheStorage( "test.txt", Document::storageTypeSimple));
- var_dump('Cia yra failo turinys: ' . $storage->getContentFromTheStorage("secret_Bandymas.txt", Document::storageTypeSecret));
File Description
- OOP
- PHP Code
- 05 Jan-2023
- 6.28 Kb
You can Share it:
Latest PHP Pastes