1 | <?php |
||
11 | class Content |
||
12 | { |
||
13 | const TITLE_MIN_LENGTH = 1; |
||
14 | const TITLE_MAX_LENGTH = 60; |
||
15 | |||
16 | const SUMMARY_MIN_LENGTH = 5; |
||
17 | const SUMMARY_MAX_LENGTH = 140; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $summaryText; |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $productDescription; |
||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $title; |
||
31 | |||
32 | /** |
||
33 | * Creates a new instance of a product Content model. |
||
34 | * |
||
35 | * @param string $title A product title (e.g. "Fancy T-Shirt No. 1") |
||
36 | * @param string $summaryText A short description text of a product (e.g. "The first edition of our fancy |
||
37 | * T-Shirt with a unicorn pooping ice cream on the front") |
||
38 | * @param string $productDescription A full product description text (optional) |
||
39 | * |
||
40 | * @throws ContentException If the summary text is too long |
||
41 | */ |
||
42 | 25 | public function __construct(string $title, string $summaryText, string $productDescription = "") |
|
43 | { |
||
44 | // validate the title |
||
45 | 25 | $trimmedTitle = trim($title); |
|
46 | 25 | if (strlen($trimmedTitle) < self::TITLE_MIN_LENGTH) { |
|
47 | 2 | throw new ContentException(sprintf("The title should not be shorter than %s characters", |
|
48 | 2 | self::TITLE_MIN_LENGTH)); |
|
49 | } |
||
50 | |||
51 | 23 | if (strlen($trimmedTitle) > self::TITLE_MAX_LENGTH) { |
|
52 | 1 | throw new ContentException(sprintf("The title should not be longer than %s characters", |
|
53 | 1 | self::TITLE_MAX_LENGTH)); |
|
54 | } |
||
55 | |||
56 | // validate the summary |
||
57 | 22 | $trimmedSummary = trim($summaryText); |
|
58 | |||
59 | 22 | if (strlen($trimmedSummary) < self::SUMMARY_MIN_LENGTH) { |
|
60 | 2 | throw new ContentException(sprintf("The summary text should not be shorter than %s characters", |
|
61 | 2 | self::SUMMARY_MIN_LENGTH)); |
|
62 | } |
||
63 | |||
64 | 20 | if (strlen($trimmedSummary) > self::SUMMARY_MAX_LENGTH) { |
|
65 | 1 | throw new ContentException(sprintf("The summary text should not be longer than %s characters", |
|
66 | 1 | self::SUMMARY_MAX_LENGTH)); |
|
67 | } |
||
68 | |||
69 | 19 | $this->title = $trimmedTitle; |
|
70 | 19 | $this->summaryText = $trimmedSummary; |
|
71 | 19 | $this->productDescription = $productDescription; |
|
72 | 19 | } |
|
73 | |||
74 | /** |
||
75 | * Get the title of a product (e.g. "Fancy T-Shirt No. 1") |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | 3 | public function getTitle() |
|
83 | |||
84 | /** |
||
85 | * Get a short description text of a product (e.g. "The first edition of our fancy T-Shirt with a unicorn pooping |
||
86 | * ice cream on the front") |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | 4 | public function getSummaryText() |
|
94 | |||
95 | /** |
||
96 | * Get a full product description text |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | 4 | public function getProductDescription() |
|
104 | } |