Meta::getFormattedDate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 11
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 10
1
<?php
2
/**
3
 * Model class
4
 */
5
namespace Phile\Model;
6
7
use Phile\Core\Container;
8
use Phile\Core\ServiceLocator;
9
10
/**
11
 * Meta model
12
 *
13
 * @author  Frank Nägler
14
 * @link    https://philecms.github.io
15
 * @license http://opensource.org/licenses/MIT
16
 * @package Phile\Model
17
 */
18
class Meta extends AbstractModel
19
{
20
21
    /**
22
     * the construtor
23
     *
24
     * @param string $rawData the raw data to parse
25
     */
26 27
    public function __construct($rawData = null)
27
    {
28 27
        if ($rawData !== null) {
29 26
            $this->setRawData($rawData);
30
        }
31
    }
32
33
    /**
34
     * set the raw data to parse
35
     *
36
     * @param string $rawData the raw data
37
     */
38 26
    public function setRawData($rawData)
39
    {
40
        /**
41
         * @triggerEvent before_read_file_meta this event is triggered before the meta data readed and parsed
42
         *
43
         * @param string rawData the unparsed data
44
         * @param \Phile\Model\Meta meta   the meta model
45
         */
46 26
        Container::getInstance()->get('Phile_EventBus')->trigger(
47
            'before_read_file_meta',
48 26
            ['rawData' => &$rawData, 'meta' => &$this]
49
        );
50 26
        $this->parseRawData($rawData);
51
        /**
52
         * @triggerEvent after_read_file_meta this event is triggered after the meta data readed and parsed
53
         *
54
         * @param string rawData the unparsed data
55
         * @param \Phile\Model\Meta meta   the meta model
56
         */
57 26
        Container::getInstance()->get('Phile_EventBus')->trigger(
58
            'after_read_file_meta',
59 26
            ['rawData' => &$rawData, 'meta' => &$this]
60
        );
61
    }
62
63
    /**
64
     * get formatted date
65
     *
66
     * @return bool|null|string
67
     */
68 1
    public function getFormattedDate()
69
    {
70 1
        $config = Container::getInstance()->get('Phile_Config');
71 1
        if (!isset($this->data['date'])) {
72
            return null;
73
        }
74 1
        $date = $this->data['date'];
75 1
        if (!is_numeric($date)) {
76 1
            $date = strtotime($date);
77
        }
78 1
        return date($config->get('date_format'), $date);
79
    }
80
81
    /**
82
     * parse the raw data
83
     *
84
     * @param string $rawData
85
     */
86 26
    protected function parseRawData($rawData)
87
    {
88
        /**
89
         * @var \Phile\ServiceLocator\MetaInterface $metaParser
90
         */
91 26
        $metaParser = ServiceLocator::getService('Phile_Parser_Meta');
92 26
        $data       = $metaParser->parse($rawData);
93
94 26
        foreach ($data as $key => $value) {
95 25
            $this->set($key, $value);
96
        }
97
    }
98
}
99