Completed
Push — develop ( e9f2c4...db0f69 )
by Zack
17:54
created

Entry_List_Template::extract_zone_vars()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 14
ccs 8
cts 9
cp 0.8889
crap 3.0123
rs 9.7998
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * The Entry List Template class .
11
 *
12
 * Renders a \GV\Entry using a \GV\Entry_Renderer.
13
 */
14
class Entry_List_Template extends Entry_Template {
15
	/**
16
	 * @var string The template slug to be loaded (like "table", "list")
17
	 */
18
	public static $slug = 'list';
19
20
	/**
21
	 * Output the field in the list view.
22
	 *
23
	 * @param \GV\Field $field The field to output.
24
	 * @param array $extras Extra stuff, like wpautop, etc.
25
	 *
26
	 * @return string
27
	 */
28 2
	public function the_field( \GV\Field $field, $extras = null ) {
29 2
		$form = $this->view->form;
30
31 2
		$renderer = new Field_Renderer();
32 2
		$source = is_numeric( $field->ID ) ? $this->view->form : new Internal_Source();
33
		
34 2
		$value = $renderer->render( $field, $this->view, $source, $this->entry, $this->request );
35
		
36 2
		$context = Template_Context::from_template( $this, compact( 'field' ) );
37
38
		/**
39
		 * @deprecated Here for back-compatibility.
40
		 */
41 2
		$label = apply_filters( 'gravityview_render_after_label', $field->get_label( $this->view, $form, $this->entry ), $field->as_configuration() );
42 2
		$label = apply_filters( 'gravityview/template/field_label', $label, $field->as_configuration(), $form->form ? $form->form : null, $this->entry->as_entry() );
0 ignored issues
show
Documentation introduced by
The property $form is declared private in GV\Form. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
43
44
		/**
45
		 * @filter `gravityview/template/field/label` Override the field label.
46
		 * @since 2.0
47
		 * @param[in,out] string $label The label to override.
48
		 * @param \GV\Template_Context $context The context.
49
		 */
50 2
		$label = apply_filters( 'gravityview/template/field/label', $label, $context );
51
52
		/**
53
		 * @filter `gravityview/template/table/entry/hide_empty`
54
		 * @param boolean $hide_empty Should the row be hidden if the value is empty? Default: don't hide.
55
		 * @param \GV\Template_Context $context The context ;) Love it, cherish it. And don't you dare modify it!
56
		 */
57 2
		$hide_empty = apply_filters( 'gravityview/render/hide-empty-zone', Utils::get( $extras, 'hide_empty', $this->view->settings->get( 'hide_empty', false ) ), $context );
58
59 2
		if ( is_numeric( $field->ID ) ) {
60 2
			$extras['field'] = $field->as_configuration();
61
		}
62
63 2
		$extras['entry'] = $this->entry->as_entry();
64 2
		$extras['hide_empty'] = $hide_empty;
65 2
		$extras['label'] = $label;
66 2
		$extras['value'] = $value;
67
68 2
		return \gravityview_field_output( $extras, $context );
69
	}
70
71
	/**
72
	 * Return an array of variables ready to be extracted.
73
	 *
74
	 * @param string|array $zones The field zones to grab.
75
	 *
76
	 * @return array An array ready to be extract()ed in the form of
77
	 *  $zone => \GV\Field_Collection
78
	 *  has_$zone => int
79
	 */
80 2
	public function extract_zone_vars( $zones ) {
81 2
		if ( ! is_array( $zones ) ) {
82
			$zones = array( $zones );
83
		}
84
85 2
		$vars = array();
86 2
		foreach ( $zones as $zone ) {
87 2
			$zone_var = str_replace( '-', '_', $zone );
88 2
			$vars[ $zone_var ] = $this->view->fields->by_position( 'single_list-' . $zone )->by_visible();
89 2
			$vars[ "has_$zone_var" ] = $vars[ $zone_var ]->count();
90
		}
91
92 2
		return $vars;
93
	}
94
}
95