Completed
Pull Request — master (#106)
by Phecho
05:57
created

CommentPresenter::created_at_diff()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Presenters;
13
14
use Gitamin\Facades\Setting;
15
use Gitamin\Presenters\Traits\TimestampsTrait;
16
use GrahamCampbell\Markdown\Facades\Markdown;
17
use Jenssegers\Date\Date;
18
19
class CommentPresenter extends AbstractPresenter
20
{
21
    use TimestampsTrait;
22
23
    /**
24
     * Renders the message from Markdown into HTML.
25
     *
26
     * @return string
27
     */
28
    public function formattedMessage()
29
    {
30
        return Markdown::convertToHtml($this->wrappedObject->message);
31
    }
32
33
    /**
34
     * Formats the created_at time ready to be used by bootstrap-datetimepicker.
35
     *
36
     * @return string
37
     */
38
    public function created_at_datetimepicker()
39
    {
40
        return $this->wrappedObject->created_at->setTimezone($this->setting->get('app_timezone'))->format('d/m/Y H:i');
41
    }
42
43
    /**
44
     * Returns a formatted timestamp for use within the timeline.
45
     *
46
     * @return string
47
     */
48
    public function timestamp_formatted()
49
    {
50
        return $this->created_at_formatted;
0 ignored issues
show
Documentation introduced by
The property created_at_formatted does not exist on object<Gitamin\Presenters\CommentPresenter>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
51
    }
52
53
    /**
54
     * Return the iso timestamp for use within the timeline.
55
     *
56
     * @return string
57
     */
58
    public function timestamp_iso()
59
    {
60
        return $this->created_at_iso;
0 ignored issues
show
Documentation introduced by
The property created_at_iso does not exist on object<Gitamin\Presenters\CommentPresenter>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
61
    }
62
63
    /**
64
     * Convert the presenter instance to an array.
65
     *
66
     * @return string[]
67
     */
68
    public function toArray()
69
    {
70
        return array_merge($this->wrappedObject->toArray(), [
71
            'created_at' => $this->created_at(),
72
            'updated_at' => $this->updated_at(),
73
        ]);
74
    }
75
}
76