Blame view

app/library/App/Mvc/ExtendedApiResource.php 5.18 KB
5166024b   Alex Savenko   extended classes
1
2
3
4
  <?php
  
  namespace App\Mvc;
  
5166024b   Alex Savenko   extended classes
5
6
7
8
9
10
11
  use Phalcon\Di;
  use Phalcon\Mvc\Micro\CollectionInterface;
  use PhalconApi\Acl\MountableInterface;
  use PhalconRest\Constants\Services;
  use PhalconRest\Mvc\Controllers\CrudResourceController;
  use PhalconRest\Transformers\ModelTransformer;
  
963a3b62   Alex Savenko   extended classes
12
  class ExtendedApiResource extends ExtendedApiCollection implements MountableInterface, CollectionInterface
5166024b   Alex Savenko   extended classes
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  {
      protected $model;
      protected $transformer;
  
      protected $itemKey;
      protected $collectionKey;
  
      protected $_modelPrimaryKey;
  
      /**
       * Returns resource with default values & all, find, create, update and delete endpoints pre-configured
       *
       * @param string $prefix Prefix for the resource (e.g. /user)
       * @param string $name Name for the resource (e.g. users) (optional)
       *
       * @return static
       */
      public static function crud($prefix, $name = null)
      {
          return self::factory($prefix, $name)
              ->endpoint(ExtendedApiEndpoint::all())
              ->endpoint(ExtendedApiEndpoint::find())
              ->endpoint(ExtendedApiEndpoint::create())
              ->endpoint(ExtendedApiEndpoint::update())
              ->endpoint(ExtendedApiEndpoint::remove());
      }
  
      /**
       * Returns resource with default values
       *
       * @param string $prefix Prefix for the resource (e.g. /user)
       * @param string $name Name for the resource (e.g. users) (optional)
       *
       * @return static
       */
      public static function factory($prefix, $name = null)
      {
          $calledClass = get_called_class();
  
          /** @var ExtendedApiResource $resource */
          $resource = new $calledClass($prefix);
  
          if (!$resource->getItemKey()) {
              $resource->itemKey('items');
          }
  
          if (!$resource->getCollectionKey()) {
              $resource->collectionKey('items');
          }
  
          if (!$resource->getTransformer()) {
              $resource->transformer(ModelTransformer::class);
          }
  
          if (!$resource->getHandler()) {
              $resource->setHandler(CrudResourceController::class);
          }
  
          if (!$resource->getName() && $name) {
              $resource->name($name);
          }
  
          if ($name) {
              $resource->name($name);
          }
  
          return $resource;
      }
  
      /**
       * @param string $model Classname of the model
       *
       * @return static
       */
      public function model($model)
      {
          $this->model = $model;
          return $this;
      }
  
      /**
       * @return string|null Classname of the model
       */
      public function getModel()
      {
          return $this->model;
      }
  
      /**
       * @return string|null Primary key of the model
       */
      public function getModelPrimaryKey()
      {
          if (!$this->model) {
              return null;
          }
  
          if (!$this->_modelPrimaryKey) {
  
              /** @var \Phalcon\Mvc\Model\MetaData $modelsMetaData */
              $modelsMetaData = Di::getDefault()->get(Services::MODELS_METADATA);
  
              $modelClass = $this->model;
  
              $this->_modelPrimaryKey = $modelsMetaData->getIdentityField(new $modelClass);
          }
  
          return $this->_modelPrimaryKey;
      }
  
      /**
       * @param string $transformer Classname of the transformer
       *
       * @return static
       */
      public function transformer($transformer)
      {
          $this->transformer = $transformer;
          return $this;
      }
  
      /**
       * @return string|null Classname of the transformer
       */
      public function getTransformer()
      {
          return $this->transformer;
      }
  
      /**
       * @param string $singleKey Response key for single item
       *
       * @return static
       *
       * @deprecated Use itemKey() instead
       */
      public function singleKey($singleKey)
      {
          return $this->itemKey($singleKey);
      }
  
      /**
       * @param string $itemKey Response key for single item
       *
       * @return static
       */
      public function itemKey($itemKey)
      {
          $this->itemKey = $itemKey;
          return $this;
      }
  
      /**
       * @return string Response key for single item
       *
       * @deprecated Use getItemKey() instead
       */
      public function getSingleKey()
      {
          return $this->getItemKey();
      }
  
      /**
       * @return string Response key for single item
       */
      public function getItemKey()
      {
          return ($this->itemKey ?: $this->name) ?: 'item';
      }
  
      /**
       * @param string $multipleKey Response key for multiple items
       *
       * @return static
       *
       * @deprecated Use collectionKey() instead
       */
      public function multipleKey($multipleKey)
      {
          return $this->collectionKey($multipleKey);
      }
  
      /**
       * @param string $collectionKey Response key for multiple items
       *
       * @return static
       */
      public function collectionKey($collectionKey)
      {
          $this->collectionKey = $collectionKey;
          return $this;
      }
  
      /**
       * @return string Response key for multiple items
       *
       * @deprecated Use getCollectionKey() instead
       */
      public function getMultipleKey()
      {
          return $this->getCollectionKey();
      }
  
      /**
       * @return string Response key for multiple items
       */
      public function getCollectionKey()
      {
          return ($this->collectionKey ?: $this->name) ?: 'items';
      }
  }