OOP - PHP Online

Form of PHP Sandbox

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.



Your result can be seen below.

Result of php executing





Full code of OOP.php

  1. <?php
  2.  
  3. class Document
  4. {
  5.     private string $filename;
  6.     private string $content;
  7.     private string $type;
  8.     const storageTypeTemp = 'temp';
  9.     const storageTypeSecret = 'secret';
  10.     const storageTypeSimple = 'simple';
  11.  
  12.     public function __construct(string $filename, string $content, string $type)
  13.     {
  14.         $this->filename = $filename;
  15.         $this->content = $content;
  16.         $this->type = $type;
  17.     }
  18.  
  19.     /**
  20.      * @return string
  21.      */
  22.     public function getFilename(): string
  23.     {
  24.         return $this->filename;
  25.     }
  26.  
  27.     /**
  28.      * @return string
  29.      */
  30.     public function getContent(): string
  31.     {
  32.         return $this->content;
  33.     }
  34.  
  35.     /**
  36.      * @return string
  37.      */
  38.     public function getType(): string
  39.     {
  40.         return $this->type;
  41.     }
  42. }
  43.  
  44. //1. Parašykite interface StorageInterface.
  45. //- store(Document $document): void
  46. //- getContent(string $filename): string
  47.  
  48. abstract class StorageInterface
  49. {
  50.     public abstract function store(Document $document): void;
  51.  
  52.     public abstract function getContent(string $filename): string;
  53.  
  54.     public function printContent(string $filename): string
  55.     {
  56.         echo ('cia yra turinys ECHO') . $this->getContent($filename);
  57.     }
  58. }
  59.  
  60. //2. Parašykite klasę FileSystemStorage kuri implementuoja StorageInterface
  61. //- store funkicja saugo turiny file sistemoje galite naudoti
  62. //- getContent gauna turinį iš file sistemos.
  63.  
  64.  
  65. class FileSystemStorage extends StorageInterface
  66. {
  67.  
  68.     public function store(Document $document): void
  69.     {
  70.         file_put_contents($document->getFilename(), $document->getContent());
  71.     }
  72.  
  73.     public function getContent(string $filename): string
  74.     {
  75.         return file_get_contents($filename);
  76.     }
  77.     public function printContent(string $filename): string
  78.     {
  79.      echo ('cia yra turinys ECHO') . $this->getContent($filename);
  80.     }
  81.  
  82. }
  83.  
  84. //3. Parašykite klasę HashedFileSystemStorage kuri extendina FileSystemStorage
  85. //- store funkcija naudoja comandą base64_encode kad užhešuotų turinį saugomą failų sistemoje.
  86. //- getContent gauna turinį iš file sistemos. base64_decode naudokite iššifravimui
  87.  
  88. class HashedFileSystemStorage extends FileSystemStorage
  89. {
  90.     public function store(Document $document): void
  91.     {
  92.         file_put_contents($document->getFilename(), base64_encode($document->getContent()));
  93.     }
  94.  
  95.     public function getContent(string $filename): string
  96.     {
  97.         return base64_decode(parent::getContent($filename));
  98.     }
  99. }
  100.  
  101. //4. Parašykite klasę TempStorage kuri implementuoja StorageInterface
  102. //- store funkcija pasaugo informaciją tiesiog array kintamajame.
  103. //    - getContent gauna turinį iš kintamajo.
  104.  
  105.  
  106.  
  107.  
  108.  
  109. class TempStorage extends StorageInterface
  110. {
  111.     private array $temp = [];
  112.     public function store(Document $document): void
  113.     {
  114.         $this->temp[$document->getFilename()] = $document->getContent();
  115.  
  116.     }
  117.     public function getContent(string $filename): string
  118.     {
  119.         if (isset($this->temp[$filename])){
  120.             return $this->temp[$filename];
  121.         }
  122.         else {
  123.             return "";
  124.         }
  125.     }
  126. }
  127.  
  128.  
  129.  
  130. // ----------------------------------------------------------------------
  131. //$simpleStorage = new FileSystemStorage();
  132. //$secretStorage = new HashedFileSystemStorage();
  133. //$tempStorage = new TempStorage();
  134. //
  135. //$secretStorage->store($documentSecret);
  136. //$simpleStorage->store($documentSimple);
  137. //$simpleStorage->store($documentSimple2);
  138. //$tempStorage->store($documentTemp);
  139.  
  140. //var_dump($tempStorage->getContent('temp.txt'));
  141. //
  142. // ----------------------------------------------------------------------
  143.  
  144. //NEBŪTINAS 5 žingnis jeigu yra noro ir laiko:
  145. //5. Parašykite klasę CookieStorage, kuri implementuoja StorageInterface
  146. //- store funkcija pasaugo informaciją teisiog cookie
  147. //- getContent gauna informacija iš cookie
  148.  
  149. //6. Sukurkite skirtingus storage tipus
  150. //- sukurkite Documentus su skirtingais tipais ir pabandykite patalpinti juos naudodami atitinkamus storage tipus.
  151. //
  152. //
  153. //7. Tam kad nereiktų spėlioti, kur kokiam storage viskas gyvena, Parašykite StorageContext clasę, kuri atliks funkcijas:
  154. //      - store(Document $document) //pagal documento tipa i atitinkama storage deda informaciją
  155. //      - getContentFromTheStorage(string $filename, string $documentType) //pagal documento tipa is atitinkamos storage traukia informacija
  156. //!Pastaba pabandykite pagalvoti, kaip būtų galima eliminuoti if'us arba switch.
  157.  
  158. class StorageContext {
  159.     public function store(Document $document)
  160.     {
  161.         $simpleStorage = new FileSystemStorage();
  162.         $secretStorage = new HashedFileSystemStorage();
  163.         $tempStorage = new TempStorage();
  164.  
  165.         if ($document->getType() == 'simple') {
  166.             $simpleStorage->store($document);
  167.         }
  168.         if ($document->getType() == 'secret') {
  169.             $secretStorage->store($document);
  170.         } else {
  171.             $tempStorage->store($document);
  172.         }
  173.     }
  174.     public function getContentFromTheStorage(string $filename, string $documentType)
  175.     {
  176.         $simpleStorage = new FileSystemStorage();
  177.         $secretStorage = new HashedFileSystemStorage();
  178.         $tempStorage = new TempStorage();
  179.  
  180.         switch ($documentType) {
  181.             case Document::storageTypeSimple:
  182.                 return $simpleStorage->getContent($filename);
  183.             case Document::storageTypeSecret:
  184.                 return $secretStorage->getContent($filename);
  185.             case Document::storageTypeTemp:
  186.                 return $tempStorage->getContent($filename);
  187.  
  188.         }
  189.     }
  190. }
  191.  
  192.  
  193.  
  194. $documentSecret = new Document("secret_Bandymas.txt", "Labai slaptas turinys", Document::storageTypeSecret);
  195. $documentSimple = new Document("simple.txt", "Labai paprastas turinys", Document::storageTypeSimple);
  196. $documentSimple2 = new Document("simple2.txt", "Dar vienas labai paprastas turinys", Document::storageTypeSimple);
  197. $documentTemp = new Document('tem.txt', 'Laikinas turinys', Document::storageTypeTemp);
  198.  
  199. $storage = new StorageContext();
  200. $storage->store($documentSecret);
  201. $storage->store($documentSimple);
  202. //$storage->store(new Document("test.txt", "test", Document::storageTypeSimple));
  203.  
  204. var_dump('Cia yra failo turinys: ' . $storage->getContentFromTheStorage( "test.txt", Document::storageTypeSimple));
  205. 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: