ViewModel::get()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 15
cp 0
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 9
nc 8
nop 2
crap 20
1
<?php
2
3
/**
4
 *
5
 * This file is part of the Apix Project.
6
 *
7
 * (c) Franck Cassedanne <franck at ouarz.net>
8
 *
9
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
10
 *
11
 */
12
13
namespace Apix\View;
14
15
/**
16
 * Model View ViewModel (MVVM)
17
 * @see  http://en.wikipedia.org/wiki/Model_View_ViewModel
18
 */
19
abstract class ViewModel
20
{
21
22
    /**
23
     * Option variable exposed to the templates.
24
     * @var array
25
     */
26
    public $options = array();
27
28
    /**
29
     * Config variable exposed to the templates.
30
     * @var array|null
31
     */
32
    public $config = null;
33
34
    /**
35
     * Deals with the request params/filters definitions.
36
     *
37
     * @return array
38
     */
39
    public function getOptions()
40
    {
41
        return $this->options;
42
    }
43
44
    /**
45
     * Assigns a property.
46
     *
47
     *     // This value can be accessed as {{foo}} within the template
48
     *     $view->set('foo', 'my value');
49
     *
50
     * You can also use an array to set several values at once:
51
     *
52
     *     // Create the values {{food}} and {{beverage}} in the template
53
     *     $view->set(array('food' => 'bread', 'beverage' => 'water'));
54
     *
55
     * @param  string|array $blob  A string key or an associative array to set.
56
     * @param  mixed        $value The value to set if the blob is a string.
57
     * @return $this
58
     */
59 View Code Duplication
    public function set($blob, $value = null)
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...
60
    {
61
        if (is_array($blob)) {
62
            foreach ($blob as $key => $value) {
63
                $this->{$key} = $value;
64
            }
65
        } else {
66
            $this->{$blob} = $value;
67
        }
68
69
        return $this;
70
    }
71
72
    public function get($key, $htmlize=true)
73
    {
74
        $v = isset($this->{$key}) ? (array) $this->{$key} : array();
75
76
        if (isset($this->options[$key])) {
77
            array_push($v,
78
                // is_array($this->options[$key])
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
                // ? $this->options[$key][0]
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
80
                // :
81
                 $this->options[$key]
82
            );
83
    }
84
85
        if ($htmlize) {
86
            array_walk_recursive($v, function (&$v) {
87
                $v = ViewModel::htmlizer($v);
88
            });
89
        }
90
91
        return $v;
92
    }
93
94 View Code Duplication
    public static function htmlizer($string)
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...
95
    {
96
        $pattern = array(
97
          '/((?:[\w\d]+\:\/\/)?(?:[\w\-\d]+\.)+[\w\-\d]+(?:\/[\w\-\d]+)*(?:\/|\.[\w\-\d]+)?(?:\?[\w\-\d]+\=[\w\-\d]+\&?)?(?:\#[\w\-\d\.]*)?)/', # URL
98
          '/([\w\-\d]+\@[\w\-\d]+\.[\w\-\d]+)/', # email
99
          // '/\S{2}/', # line break
100
        );
101
        $replace = array(
102
            '<a href="$1">$1</a>',
103
            '<a href="mailto:$1">$1</a>',
104
            // '- $1'
105
        );
106
107
        return preg_replace($pattern, $replace, $string);
108
    }
109
110
    /**
111
     * Assigns a value by reference. The benefit of binding is that values can
112
     * be altered without re-setting them. It is also possible to bind variables
113
     * before they have values. Assigned values will be available as a
114
     * variable within the template file:
115
     *
116
     *     // This reference can be accessed as {{ref}} within the template
117
     *     $view->bind('ref', $bar);
118
     *
119
     * @param   string   variable name
120
     * @param   mixed    referenced variable
121
     * @return $this
122
     */
123
    public function bind($key, & $value)
124
    {
125
        $this->{$key} =& $value;
126
127
        return $this;
128
    }
129
130
    /* ---- generic helpers --- */
131
132 View Code Duplication
    public function hasMany($mix)
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...
133
    {
134
        if (is_string($mix) && isset($this->{$mix})) {
135
            return count($this->{$mix})>1;
136
        } elseif (is_array($mix)) {
137
            return count($mix)>1;
138
        }
139
140
        return false;
141
    }
142
143
    /**
144
     * Returns this view model layout.
145
     *
146
     * @return string
147
     */
148
    public function getLayout()
149
    {
150
        return $this->_layout;
0 ignored issues
show
Bug introduced by
The property _layout does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
151
    }
152
153
}
154