• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

timber / timber / 5690057835

pending completion
5690057835

push

github

nlemoine
Merge branch '2.x' of github.com:timber/timber into 2.x-refactor-file-models

# Conflicts:
#	src/Attachment.php
#	src/ExternalImage.php
#	src/FileSize.php
#	src/URLHelper.php

1134 of 1134 new or added lines in 55 files covered. (100.0%)

3923 of 4430 relevant lines covered (88.56%)

59.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.75
/src/Core.php
1
<?php
2

3
namespace Timber;
4

5
use AllowDynamicProperties;
6

7
/**
8
 * Class Core
9
 */
10
#[AllowDynamicProperties]
11
abstract class Core
12
{
13
    public $id;
14

15
    public $ID;
16

17
    public $object_type;
18

19
    /**
20
     * This method is needed to complement the magic __get() method, because Twig uses `isset()`
21
     * internally.
22
     *
23
     * @internal
24
     * @link https://github.com/twigphp/Twig/issues/601
25
     * @link https://twig.symfony.com/doc/2.x/recipes.html#using-dynamic-object-properties
26
     * @return boolean
27
     */
28
    public function __isset($field)
29
    {
30
        if (isset($this->$field)) {
147✔
31
            return $this->$field;
5✔
32
        }
33
        return false;
146✔
34
    }
35

36
    /**
37
     * Magic method dispatcher for meta fields, for convience in Twig views.
38
     *
39
     * Called when explicitly invoking non-existent methods on a Core object. This method is not
40
     * meant to be called directly.
41
     *
42
     * @example
43
     * ```php
44
     * $post = Timber\Post::get( get_the_ID() );
45
     *
46
     * update_post_meta( $post->id, 'favorite_zep4_track', 'Black Dog' );
47
     *
48
     * Timber::render( 'rock-n-roll.twig', array( 'post' => $post ) );
49
     * ```
50
     * ```twig
51
     * {# Since this method does not exist explicitly on the Post class,
52
     *    it will dynamically dispatch the magic __call() method with an argument
53
     *    of "favorite_zep4_track" #}
54
     * <span>Favorite <i>Zeppelin IV</i> Track: {{ post.favorite_zep4_track() }}</span>
55
     * ```
56
     * @link https://secure.php.net/manual/en/language.oop5.overloading.php#object.call
57
     * @link https://github.com/twigphp/Twig/issues/2
58
     * @api
59
     *
60
     * @param string $field     The name of the method being called.
61
     * @param array  $arguments Enumerated array containing the parameters passed to the function.
62
     *                          Not used.
63
     *
64
     * @return mixed The value of the meta field named `$field` if truthy, `false` otherwise.
65
     */
66
    public function __call($field, $arguments)
67
    {
68
        if (\method_exists($this, 'meta') && $meta_value = $this->meta($field)) {
17✔
69
            return $meta_value;
11✔
70
        }
71

72
        return false;
6✔
73
    }
74

75
    /**
76
     * Magic getter for dynamic meta fields, for convenience in Twig views.
77
     *
78
     * This method is not meant to be called directly.
79
     *
80
     * @example
81
     * ```php
82
     * $post = Timber\Post::get( get_the_ID() );
83
     *
84
     * update_post_meta( $post->id, 'favorite_darkside_track', 'Any Colour You Like' );
85
     *
86
     * Timber::render('rock-n-roll.twig', array( 'post' => $post ));
87
     * ```
88
     * ```twig
89
     * {# Since this property does not exist explicitly on the Post class,
90
     *    it will dynamically dispatch the magic __get() method with an argument
91
     *    of "favorite_darkside_track" #}
92
     * <span>Favorite <i>Dark Side of the Moon</i> Track: {{ post.favorite_darkside_track }}</span>
93
     * ```
94
     * @link https://secure.php.net/manual/en/language.oop5.overloading.php#object.get
95
     * @link https://twig.symfony.com/doc/2.x/recipes.html#using-dynamic-object-properties
96
     *
97
     * @param string $field The name of the property being accessed.
98
     *
99
     * @return mixed The value of the meta field, or the result of invoking `$field()` as a method
100
     * with no arguments, or `false` if neither returns a truthy value.
101
     */
102
    public function __get($field)
103
    {
104
        if (\method_exists($this, 'meta') && $meta_value = $this->meta($field)) {
88✔
105
            return $this->$field = $meta_value;
13✔
106
        }
107
        if (\method_exists($this, $field)) {
77✔
108
            return $this->$field = $this->$field();
71✔
109
        }
110

111
        if ('custom' === $field) {
6✔
112
            Helper::deprecated(
4✔
113
                "Accessing a meta value through {{ {$this->object_type}.custom }}",
4✔
114
                "{{ {$this->object_type}.meta() }} or {{ {$this->object_type}.raw_meta() }}",
4✔
115
                '2.0.0'
4✔
116
            );
4✔
117
        }
118

119
        return $this->$field = false;
6✔
120
    }
121

122
    /**
123
     * Takes an array or object and adds the properties to the parent object.
124
     *
125
     * @example
126
     * ```php
127
     * $data = array( 'airplane' => '757-200', 'flight' => '5316' );
128
     * $post = Timber::get_post();
129
     * $post->import(data);
130
     *
131
     * echo $post->airplane; // 757-200
132
     * ```
133
     * @param array|object $info an object or array you want to grab data from to attach to the Timber object
134
     */
135
    public function import($info, $force = false, $only_declared_properties = false)
136
    {
137
        if (\is_object($info)) {
621✔
138
            $info = \get_object_vars($info);
269✔
139
        }
140
        if (\is_array($info)) {
621✔
141
            foreach ($info as $key => $value) {
621✔
142
                if ($key === '' || \ord($key[0]) === 0) {
621✔
143
                    continue;
1✔
144
                }
145
                if (!empty($key) && $force) {
621✔
146
                    $this->$key = $value;
1✔
147
                } elseif (!empty($key) && !\method_exists($this, $key)) {
621✔
148
                    if ($only_declared_properties) {
621✔
149
                        if (\property_exists($this, $key)) {
×
150
                            $this->$key = $value;
×
151
                        }
152
                    } else {
153
                        $this->$key = $value;
621✔
154
                    }
155
                }
156
            }
157
        }
158
    }
159

160
    /**
161
     * Updates metadata for the object.
162
     *
163
     * @deprecated 2.0.0 Use `update_metadata()` instead.
164
     *
165
     * @param string $key   The key of the meta field to update.
166
     * @param mixed  $value The new value.
167
     */
168
    public function update($key, $value)
169
    {
170
        Helper::deprecated('Timber\Core::update()', 'update_metadata()', '2.0.0');
1✔
171
        \update_metadata($this->object_type, $this->ID, $key, $value);
1✔
172
    }
173
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc