Completed
Branch FET/rest-relation-endpoints (393bb8)
by
unknown
30:20 queued 20:04
created

Base::validateModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\libraries\rest_api\controllers\model;
3
4
use EEM_Base;
5
use EventEspresso\core\libraries\rest_api\controllers\Base as Controller_Base;
6
use EventEspresso\core\libraries\rest_api\ModelVersionInfo;
7
use EE_Error;
8
use EventEspresso\core\libraries\rest_api\RestException;
9
10
/**
11
 * Base
12
 * Base controller which also has something to do with models
13
 *
14
 * @package               Event Espresso
15
 * @subpackage
16
 * @author                Mike Nelson
17
 */
18
class Base extends Controller_Base
19
{
20
21
    /**
22
     * Holds reference to the model version info, which knows the requested version
23
     *
24
     * @var ModelVersionInfo
25
     */
26
    protected $model_version_info;
27
28
29
30
    /**
31
     * Sets the version the user requested
32
     *
33
     * @param string $version eg '4.8'
34
     */
35
    public function setRequestedVersion($version)
36
    {
37
        parent::setRequestedVersion($version);
38
        $this->model_version_info = new ModelVersionInfo($version);
39
    }
40
41
42
43
    /**
44
     * Gets the object that should be used for getting any info from the models,
45
     * because it's takes the requested and current core version into account
46
     *
47
     * @return \EventEspresso\core\libraries\rest_api\ModelVersionInfo
48
     * @throws EE_Error
49
     */
50
    public function getModelVersionInfo()
51
    {
52
        if (! $this->model_version_info) {
53
            throw new EE_Error(
54
                sprintf(
55
                    __(
56
                        'Cannot use model version info before setting the requested version in the controller',
57
                        'event_espresso'
58
                    )
59
                )
60
            );
61
        }
62
        return $this->model_version_info;
63
    }
64
65
66
67
    /**
68
     * Determines if $object is of one of the classes of $classes. Similar to
69
     * in_array(), except this checks if $object is a subclass of the classnames provided
70
     * in $classnames
71
     *
72
     * @param object $object
73
     * @param array  $classnames
74
     * @return boolean
75
     */
76
    public function isSubclassOfOne($object, $classnames)
77
    {
78
        foreach ($classnames as $classname) {
79
            if (is_a($object, $classname)) {
80
                return true;
81
            }
82
        }
83
        return false;
84
    }
85
86
    /**
87
     * Verifies the model name provided was valid. If so, returns the model (as an object). Otherwise, throws an
88
     * exception. Must be called after `setRequestedVersion()`.
89
     * @since $VID:$
90
     * @param $model_name
91
     * @return EEM_Base
92
     * @throws EE_Error
93
     * @throws RestException
94
     */
95
    protected function validateModel($model_name)
96
    {
97
        if (! $this->getModelVersionInfo()->isModelNameInThisVersion($model_name)) {
98
            throw new RestException(
99
                'endpoint_parsing_error',
100
                sprintf(
101
                    __(
102
                        'There is no model for endpoint %s. Please contact event espresso support',
103
                        'event_espresso'
104
                    ),
105
                    $model_name
106
                )
107
            );
108
        }
109
        return $this->getModelVersionInfo()->loadModel($model_name);
110
    }
111
}
112
// End of file Base.php
113