Book.php 3.25 KB
Newer Older
Sean Madsen's avatar
Sean Madsen committed
1 2 3 4 5
<?php

namespace AppBundle\Model;

use Symfony\Component\Yaml\Parser;
6
use AppBundle\Utils\StringTools;
Sean Madsen's avatar
Sean Madsen committed
7 8 9 10

class Book {

  /**
Sean Madsen's avatar
Sean Madsen committed
11 12 13
   * @var string
   *   The reference identifier for this book, taken from the name of the
   *   book's .yml file.
Sean Madsen's avatar
Sean Madsen committed
14 15
   */
  public $slug;
16

Sean Madsen's avatar
Sean Madsen committed
17
  /**
Sean Madsen's avatar
Sean Madsen committed
18 19
   * @var string
   *   The title of the book, taken from the "name"
Sean Madsen's avatar
Sean Madsen committed
20 21
   */
  public $name;
22

Sean Madsen's avatar
Sean Madsen committed
23
  /**
Sean Madsen's avatar
Sean Madsen committed
24 25
   * @var string
   *   Short phrase describing the book, taken from the
Sean Madsen's avatar
Sean Madsen committed
26 27
   */
  public $description;
28

Sean Madsen's avatar
Sean Madsen committed
29
  /**
Sean Madsen's avatar
Sean Madsen committed
30 31
   * @var Language[]
   *   An array (without keys of Language objects to
Sean Madsen's avatar
Sean Madsen committed
32 33
   */
  public $languages;
34

Sean Madsen's avatar
Sean Madsen committed
35
  /**
Sean Madsen's avatar
Sean Madsen committed
36 37
   * @var int
   *   Used to sort books
Sean Madsen's avatar
Sean Madsen committed
38 39
   */
  public $weight;
40

41
  /**
Sean Madsen's avatar
Sean Madsen committed
42 43
   * @var string
   *   (e.g. "Core", "Extensions") Should be in sentence case
44 45 46
   */
  public $category;

Sean Madsen's avatar
Sean Madsen committed
47 48
  /**
   * Creates a book based on a yaml conf file
49
   *
Sean Madsen's avatar
Sean Madsen committed
50 51 52
   * @param string $confFile
   *   The path to the yaml configuration file which defines the attributes
   *   of the book.
Sean Madsen's avatar
Sean Madsen committed
53 54 55 56
   */
  public function __construct($confFile) {
    $parser = new Parser();
    $yaml = $parser->parse(file_get_contents($confFile));
57 58 59
    $this->slug = StringTools::urlSafe(basename($confFile, '.yml'));
    $this->name = $yaml['name'];
    $this->weight = isset($yaml['weight']) ? $yaml['weight'] : 0;
Sean Madsen's avatar
Sean Madsen committed
60 61 62 63
    $this->description = isset($yaml['description']) ? $yaml['description'] : "";
    foreach ($yaml['langs'] as $code => $languageData) {
      $this->languages[] = new Language($code, $languageData);
    }
64 65
    $category = isset($yaml['category']) ? $yaml['category'] : "Extensions";
    $this->category = ucwords($category);
Sean Madsen's avatar
Sean Madsen committed
66 67 68
  }

  /**
Sean Madsen's avatar
Sean Madsen committed
69 70
   * @return bool
   *   True when the book contains multiple languages.
Sean Madsen's avatar
Sean Madsen committed
71
   */
72
  public function isMultiLanguage() {
Sean Madsen's avatar
Sean Madsen committed
73 74
    return count($this->languages) > 1;
  }
75

76
  /**
Sean Madsen's avatar
Sean Madsen committed
77 78
   * @return integer
   *   The total number of language/version combinations defined for this book
79 80 81 82 83 84 85 86 87 88
   */
  public function countEditions() {
    $sum = 0;
    foreach ($this->languages as $language) {
      $sum = $sum + $language->countVersions();
    }
    return $sum;
  }

  /**
Sean Madsen's avatar
Sean Madsen committed
89 90
   * @return bool
   *   TRUE if the book has more than one language/version combo
91 92 93 94 95
   */
  public function isMultiEdition() {
    return $this->countEditions() > 1;
  }

96 97 98
  /**
   * Selects one of the languages within the book
   *
Sean Madsen's avatar
Sean Madsen committed
99 100
   * @param string $code
   *   Two letter language code to describe the language
101 102 103 104 105
   *
   * @return Language
   */
  public function getLanguageByCode($code) {
    $chosen = NULL;
106 107
    foreach ($this->languages as $language) {
      if ($language->code == $code) {
108 109 110 111 112 113 114
        $chosen = $language;
        break;
      }
    }
    return $chosen;
  }

Sean Madsen's avatar
Sean Madsen committed
115 116 117 118
  /**
   * @return \AppBundle\Model\Language
   */
  public function getDefaultLanguage() {
119
    return $this->languages[0];
Sean Madsen's avatar
Sean Madsen committed
120 121
  }

122 123 124 125 126
  /**
   * Check this book for any problems in the way it's defined.
   *
   * If validation succeeds, this function returns nothing
   *
Sean Madsen's avatar
Sean Madsen committed
127 128
   * @throws \Exception
   *   If validation fails
129 130 131 132 133 134 135
   */
  public function validate() {
    $illegalBookSlugs = array(
      "bundles",
      "static",
    );
    if (in_array($this->slug, $illegalBookSlugs)) {
136
      throw new \Exception("Book slug is '{$this->slug}' but this word is "
137 138 139 140 141
          . "reserved in order to maintain functionality within this app. "
          . "Reserved words are: " . implode(", ", $illegalBookSlugs));
    }
  }

Sean Madsen's avatar
Sean Madsen committed
142
}