Passed
Push — master ( 1282f8...92a01c )
by Schlaefer
03:31
created

Meta::getFormattedDate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

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