Completed
Push — master ( b4ab83...0f95d0 )
by Mikołaj
05:24
created

Article::isHomepageHidden()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Rudolf\Modules\Articles\One;
4
5
use Rudolf\Component\Forms\Validator;
6
use Rudolf\Component\Hooks;
7
use Rudolf\Component\Html\Text;
8
use Rudolf\Component\Images\Image;
9
10
class Article
11
{
12
    /**
13
     * @var array Article data
14
     */
15
    protected $article;
16
17
    /**
18
     * Constructor.
19
     * 
20
     * @param array $article
21
     */
22
    public function __construct($article = [])
23
    {
24
        $this->setData($article);
25
    }
26
27
    /**
28
     * Set article data.
29
     * 
30
     * @param array $article
31
     */
32
    public function setData($article)
33
    {
34
        $this->article = array_merge(
35
            [
36
                'id' => 0,
37
                'category_ID' => 0,
38
                'title' => '',
39
                'keywords' => '',
40
                'description' => '',
41
                'content' => '',
42
                'author' => '',
43
                'date' => '',
44
                'added' => '',
45
                'modified' => '',
46
                'adder_ID' => 0,
47
                'adder_first_name' => '',
48
                'adder_surname' => '',
49
                'modifier_ID' => 0,
50
                'modifier_first_name' => '',
51
                'modifier_surname' => '',
52
                'views' => 0,
53
                'slug' => '',
54
                'url' => '',
55
                'album' => '',
56
                'thumb' => '',
57
                'thumbnail' => '',
58
                'photos' => '',
59
                'homepage_hidden' => false,
60
                'published' => false,
61
                'category' => '',
62
                'category_title' => '',
63
                'category_url' => '',
64
            ],
65
            (array) $article
66
        );
67
    }
68
69
    /**
70
     * Returns article ID.
71
     * 
72
     * @return int
73
     */
74
    public function id()
75
    {
76
        return (int) $this->article['id'];
77
    }
78
79
    /**
80
     * Returns category ID.
81
     * 
82
     * @return int
83
     */
84
    public function categoryID()
85
    {
86
        return (int) $this->article['category_ID'];
87
    }
88
89
    /**
90
     * Returns article title.
91
     * 
92
     * @param string $type null|raw
93
     * 
94
     * @return string
95
     */
96
    public function title($type = '')
97
    {
98
        $title = $this->article['title'];
99
        if ('raw' === $type) {
100
            return $title;
101
        }
102
103
        return Text::escape($title);
104
    }
105
106
    /**
107
     * Returns the keywords.
108
     * 
109
     * @param string $type null|raw
110
     * 
111
     * @return string
112
     */
113
    public function keywords($type = '')
114
    {
115
        $keywords = $this->article['keywords'];
116
        if ('raw' === $type) {
117
            return $keywords;
118
        }
119
120
        return Text::escape($keywords);
121
    }
122
123
    /**
124
     * Returns the description.
125
     * 
126
     * @param string $type
127
     * 
128
     * @return string
129
     */
130 View Code Duplication
    public function description($type = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132
        $description = $this->article['description'];
133
        if ('raw' === $type) {
134
            return $description;
135
        }
136
137
        return Text::escape($description);
138
    }
139
140
    /**
141
     * Returns article content.
142
     * 
143
     * @param bool|int $truncate
144
     * @param bool     $stripTags
145
     * @param bool     $escape
146
     * @param bool     $raw
147
     * 
148
     * @return string
149
     */
150
    public function content($truncate = false, $stripTags = false, $escape = false, $raw = false)
151
    {
152
        $content = $this->article['content'];
153
154
        if (true === $stripTags) {
155
            $content = strip_tags($content);
156
        }
157
158
        if (false !== $truncate) {
159
            $content = Text::truncate($content, $truncate, '…', '<b><i><u><em><strong><a><span>');
0 ignored issues
show
Bug introduced by
It seems like $truncate defined by parameter $truncate on line 150 can also be of type boolean; however, Rudolf\Component\Html\Text::truncate() does only seem to accept integer, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
160
        }
161
162
        if (true === $escape) {
163
            $content = Text::escape($content);
164
        }
165
166
        if (false === $raw) {
167
            $content = Hooks\Filter::apply('content_filter', $content);
168
169
            return $content;
170
        }
171
172
        return $content;
173
    }
174
    /**
175
     * Returns the author.
176
     * 
177
     * @param string $type null|raw
178
     * 
179
     * @return string
180
     */
181 View Code Duplication
    public function author($type = '', $adder = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183
        $author = $this->article['author'];
184
185
        // if fields is empty and $adder is true
186
        if (empty($author) and true === $adder) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
187
            $author = $this->adderFullName(false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
188
        }
189
190
        if ('raw' === $type) {
191
            return $author;
192
        }
193
194
        return Text::escape($author);
195
    }
196
197
    /**
198
     * Returns article date.
199
     * 
200
     * @param bool|string $format
201
     * @param string      $style  normal|locale
202
     * 
203
     * @return string If date field empty, return current date
204
     */
205 View Code Duplication
    public function date($format = false, $style = 'normal', $inflected = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206
    {
207
        $date = $this->article['date'];
208
209
        $validator = new Validator();
210
        $validator->checkDatetime('date', $date, 'Y-m-d H:i:s');
211
        if ($validator->isErrors() or empty($date)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
212
            return $date;
213
        }
214
215
        switch ($style) {
216
            case 'locale': // http://php.net/manual/en/function.strftime.php
217
                $format = ($format) ? $format : '%D';
218
                $date = strftime($format, strtotime($date));
219
                break;
220
221
            default: // http://php.net/manual/en/datetime.formats.date.php
222
                $format = ($format) ? $format : 'Y-m-d H:i:s';
223
                $date = date_format(date_create($date), $format);
224
                break;
225
        }
226
227
        $date = Hooks\Filter::apply('date_format_filter', $date);
228
229
        if (true === $inflected) {
230
            $month = [
231
                'styczeń' => 'stycznia', // 01
232
                'luty' => 'lutego', // 02
233
                'marzec' => 'marca', // 03
234
                'kwiecień' => 'kwietnia', // 04
235
                'maj' => 'maja', // 05
236
                'czerwiec' => 'czerwca', // 06
237
                'lipiec' => 'lipca', // 07
238
                'sierpień' => 'sierpnia', // 08
239
                'wrzesień' => 'września', // 09
240
                'październik' => 'października', // 10
241
                'listopad' => 'listopada', // 11
242
                'grudzień' => 'grudnia', // 12
243
            ];
244
245
            foreach ($month as $key => $value) {
246
                $date = str_replace($key, $value, $date);
247
            }
248
        }
249
250
        return $date;
251
    }
252
253
    /**
254
     * Returns date of article added.
255
     * 
256
     * @return string
257
     */
258
    public function added()
259
    {
260
        return $this->article['added'];
261
    }
262
263
    /**
264
     * Returns date of last article modified.
265
     * 
266
     * @return string
267
     */
268
    public function modified()
269
    {
270
        return $this->article['modified'];
271
    }
272
273
    /**
274
     * Returns adder ID.
275
     * 
276
     * @return int
277
     */
278
    public function adderID()
279
    {
280
        return (int) $this->article['adder_ID'];
281
    }
282
283
    /**
284
     * Returns first name and surname of adder.
285
     * 
286
     * @param string $type
287
     * 
288
     * @return string
289
     */
290 View Code Duplication
    public function adderFullName($type = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
291
    {
292
        $name = trim($this->article['adder_first_name'].' '.$this->article['adder_surname']);
293
        if ('raw' === $type) {
294
            return $name;
295
        }
296
297
        return Text::escape($name);
298
    }
299
300
    /**
301
     * Returns modifier ID.
302
     * 
303
     * @return int
304
     */
305
    public function modifierID()
306
    {
307
        return (int) $this->article['modifier_ID'];
308
    }
309
310
    /**
311
     * Returns modifier full name.
312
     * 
313
     * @return int
314
     */
315 View Code Duplication
    public function modifierFullName($type = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
316
    {
317
        $name = $this->article['modifier_first_name'].' '.$this->article['modifier_surname'];
318
        if ('raw' === $type) {
319
            return $name;
320
        }
321
322
        return Text::escape($name);
323
    }
324
325
    /**
326
     * Checks whether the article has modified.
327
     * 
328
     * @return bool
329
     */
330
    public function isModified()
331
    {
332
        return (bool) $this->article['modified'];
333
    }
334
335
    /**
336
     * Returns the number of views.
337
     * 
338
     * @return int
339
     */
340
    public function views()
341
    {
342
        return (int) $this->article['views'];
343
    }
344
345
    /**
346
     * Returns article slug.
347
     *
348
     * @param string $type
349
     *
350
     * @return string
351
     */
352 View Code Duplication
    public function slug($type = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
353
    {
354
        $slug = $this->article['slug'];
355
        if ('raw' === $type) {
356
            return $slug;
357
        }
358
359
        return Text::escape($slug);
360
    }
361
362
    /**
363
     * Returns article url.
364
     * 
365
     * @return string
366
     */
367
    public function url()
368
    {
369
        return sprintf('%1$s/%2$s/%3$s/%4$s/%5$s',
370
            DIR,
371
            'artykuly',
372
            $this->date('Y'),
373
            $this->date('m'),
374
            $this->slug()
375
        );
376
    }
377
378
    /**
379
     * Returns album path.
380
     *
381
     * @param string $type
382
     * 
383
     * @return string
384
     */
385
    public function album($type = '')
386
    {
387
        $album = $this->article['album'];
388
        if ('raw' === $type) {
389
            return $album;
390
        }
391
392
        return Text::escape($album);
393
    }
394
395
    /**
396
     * Returns thumb path.
397
     *
398
     * @param string $type
399
     * 
400
     * @return string
401
     */
402
    public function thumb($type = '')
403
    {
404
        $thumb = $this->article['thumb'];
405
        if ('raw' === $type) {
406
            return $thumb;
407
        }
408
409
        return Text::escape($thumb);
410
    }
411
412
    /**
413
     * Checks whether the article has a thumbnail.
414
     * 
415
     * @return bool
416
     */
417
    public function hasThumbnail()
418
    {
419
        return (bool) $this->article['thumb'];
420
    }
421
422
    /**
423
     * Returns thumbnail code or only address.
424
     * 
425
     * @param int    $width   Image width
426
     * @param int    $height  Image height
427
     * @param bool   $album   Add album address if exists
428
     * @param string $alt     Set alternative text
429
     * @param string $default Default thumb path. It use when thumb path is empty
430
     * 
431
     * @return string
432
     */
433 View Code Duplication
    public function thumbnail($width = 100, $height = 100, $album = false, $alt = '', $default = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
434
    {
435
        $thumbUrl = $this->thumb();
436
        $albumUrl = $this->album();
437
        $alt = ($alt) ? $alt : $this->title();
438
439
        if (!$this->hasThumbnail()) {
440
            if (!empty($default)) {
441
                $thumbUrl = $default;
442
            } else {
443
                return false;
444
            }
445
        }
446
447
        $thumbUrl = Image::resize($thumbUrl, $width, $height);
448
449
        $html = sprintf('<img src="%1$s" alt="%4$s" width="%2$s" height="%3$s">',
450
            $thumbUrl, $width, $height, $alt
451
        );
452
453
        if (true === $album and !empty($albumUrl)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
454
            $html = sprintf('<a href="%1$s">%2$s</a>', $albumUrl, $html);
455
        }
456
457
        return $html;
458
    }
459
460
    /**
461
     * Returns the number of photos.
462
     * 
463
     * @return int
464
     */
465
    public function photos()
466
    {
467
        return (int) $this->article['photos'];
468
    }
469
470
    /**
471
     * Checks whether the article has a photos.
472
     * 
473
     * @return bool
474
     */
475
    public function hasPhotos()
476
    {
477
        return (bool) $this->article['photos'];
478
    }
479
480
    /**
481
     * Check whether the article is hidden on homepage.
482
     *
483
     * @return bool
484
     */
485
    public function isHomepageHidden()
486
    {
487
        return (bool) $this->article['homepage_hidden'];
488
    }
489
490
    /**
491
     * Chcecks whether the article is published.
492
     * 
493
     * @return bool
494
     */
495
    public function isPublished()
496
    {
497
        return (bool) $this->article['published'];
498
    }
499
500
    /**
501
     * Returns article category anchor.
502
     * 
503
     * @return string
504
     */
505
    public function category()
506
    {
507
        return sprintf('<a href="%1$s">%2$s</a>',
508
            $this->categoryUrl(),
509
            $this->categoryTitle()
510
        );
511
    }
512
513
    /**
514
     * Returns category title.
515
     * 
516
     * @param string $type
517
     * 
518
     * @return string
519
     */
520
    public function categoryTitle($type = '')
521
    {
522
        $title = $this->article['category_title'];
523
524
        if ('raw' === $type) {
525
            return $title;
526
        }
527
528
        return Text::escape($title);
529
    }
530
531
    /**
532
     * Returns category url.
533
     * 
534
     * @return string
535
     */
536
    public function categoryUrl()
537
    {
538
        return sprintf('%1$s/%2$s/%3$s',
539
            DIR,
540
            'artykuly/kategorie',
541
            Text::escape($this->article['category_url'])
542
        );
543
    }
544
545
    /**
546
     * Checks whether the article has a category.
547
     * 
548
     * @return bool
549
     */
550
    public function hasCategory()
551
    {
552
        return (bool) $this->article['category_url'];
553
    }
554
}
555