Advertisement
ShadowEmbrace

Articles 2

Nov 19th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 KB | None | 0 0
  1. <?php
  2.  
  3. Class Article
  4. {
  5.     /**
  6.      * @var string
  7.      */
  8.     private $title;
  9.     /**
  10.      * @var string
  11.      */
  12.     private $content;
  13.     /**
  14.      * @var string
  15.      */
  16.     private $author;
  17.  
  18.     /**
  19.      * Article constructor.
  20.      * @param string $title
  21.      * @param string $content
  22.      * @param string $author
  23.      */
  24.     public function __construct(string $title, string $content, string $author)
  25.     {
  26.         $this->title = $title;
  27.         $this->content = $content;
  28.         $this->author = $author;
  29.     }
  30.  
  31.     /**
  32.      * @return string
  33.      */
  34.     public function getTitle(): string
  35.     {
  36.         return $this->title;
  37.     }
  38.  
  39.     /**
  40.      * @param string $title
  41.      */
  42.     public function setTitle(string $title): void
  43.     {
  44.         $this->title = $title;
  45.     }
  46.  
  47.     /**
  48.      * @return string
  49.      */
  50.     public function getContent(): string
  51.     {
  52.         return $this->content;
  53.     }
  54.  
  55.     /**
  56.      * @param string $content
  57.      */
  58.     public function setContent(string $content): void
  59.     {
  60.         $this->content = $content;
  61.     }
  62.  
  63.     /**
  64.      * @return string
  65.      */
  66.     public function getAuthor(): string
  67.     {
  68.         return $this->author;
  69.     }
  70.  
  71.     /**
  72.      * @param string $author
  73.      */
  74.     public function setAuthor(string $author): void
  75.     {
  76.         $this->author = $author;
  77.     }
  78.  
  79.     public function __toString()
  80.     {
  81.         return $this->getTitle() . ' - ' . $this->getContent() . ': ' . $this->getAuthor() . PHP_EOL;
  82.     }
  83.  
  84.  
  85. }
  86.  
  87. $articles = [];
  88.  
  89. $n = intval(readline());
  90.  
  91. for ($i = 0; $i < $n; $i++) {
  92.     $cmd = explode(', ', readline());
  93.     list($title, $content, $author) = $cmd;
  94.     $article = new Article($title, $content, $author);
  95.     $articles[] = $article;
  96. }
  97.  
  98. $orderBy = readline();
  99.  
  100. uasort($articles, function (Article $article1, Article $article2) use ($articles, $orderBy) {
  101.    if ($orderBy === 'title'){
  102.        return $article1->getTitle() <=> $article2->getTitle();
  103.    } elseif ($orderBy === 'content') {
  104.        return $article1->getContent() <=> $article2->getContent();
  105.    }elseif ($orderBy === 'author') {
  106.        return $article1->getAuthor() <=> $article2->getAuthor();
  107.    }
  108. });
  109.  
  110. foreach ($articles as $articleInOrder) {
  111.     echo $articleInOrder;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement