1 | <?php |
||
2 | /** |
||
3 | * Twigfield for Craft CMS |
||
4 | * |
||
5 | * Provides a twig editor field with Twig & Craft API autocomplete |
||
6 | * |
||
7 | * @link https://nystudio107.com |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
8 | * @copyright Copyright (c) 2022 nystudio107 |
||
0 ignored issues
–
show
|
|||
9 | */ |
||
0 ignored issues
–
show
|
|||
10 | |||
11 | namespace nystudio107\twigfield\base; |
||
12 | |||
13 | use Craft; |
||
14 | use craft\base\Model; |
||
15 | use yii\base\InvalidArgumentException; |
||
16 | |||
17 | /** |
||
0 ignored issues
–
show
|
|||
18 | * @author nystudio107 |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||
19 | * @package Twigfield |
||
0 ignored issues
–
show
|
|||
20 | * @since 1.0.0 |
||
0 ignored issues
–
show
|
|||
21 | */ |
||
0 ignored issues
–
show
|
|||
22 | abstract class FluentModel extends Model |
||
23 | { |
||
24 | |||
25 | // Public Methods |
||
26 | // ========================================================================= |
||
27 | |||
28 | /** |
||
29 | * Add fluent getters/setters for this class |
||
30 | * |
||
31 | * @param string $method The method name (property name) |
||
32 | * @param array $args The arguments list |
||
0 ignored issues
–
show
|
|||
33 | * |
||
34 | * @return mixed The value of the property |
||
35 | */ |
||
36 | public function __call($method, $args) |
||
37 | { |
||
38 | try { |
||
39 | $reflector = new \ReflectionClass(static::class); |
||
40 | } catch (\ReflectionException $e) { |
||
41 | Craft::error( |
||
42 | $e->getMessage(), |
||
43 | __METHOD__ |
||
44 | ); |
||
45 | |||
46 | return null; |
||
47 | } |
||
48 | if (!$reflector->hasProperty($method)) { |
||
49 | throw new InvalidArgumentException("Property {$method} doesn't exist"); |
||
50 | } |
||
51 | $property = $reflector->getProperty($method); |
||
52 | if (empty($args)) { |
||
53 | // Return the property |
||
54 | return $property->getValue(); |
||
55 | } |
||
56 | // Set the property |
||
57 | $value = $args[0]; |
||
58 | $property->setValue($this, $value); |
||
59 | |||
60 | // Make it chainable |
||
61 | return $this; |
||
62 | } |
||
63 | } |
||
64 |