예제 #1
0
 /**
  * Merges each of the elements from `$data` into each of the entities in `$entities`.
  *
  * Records in `$data` are matched against the entities using the primary key
  * column. Entries in `$entities` that cannot be matched to any record in
  * `$data` will be discarded. Records in `$data` that could not be matched will
  * be marshalled as a new entity.
  *
  * ### Options:
  *
  * - fieldList: A whitelist of fields to be assigned to the entity. If not present,
  *   the accessible fields list in the entity will be used.
  * - accessibleFields: A list of fields to allow or deny in entity accessible fields.
  *
  * @param array|\Traversable $entities the entities that will get the
  *   data merged in
  * @param array $data list of arrays to be merged into the entities
  * @param array $options List of options.
  * @return array
  */
 public function mergeMany($entities, array $data, array $options = [])
 {
     $primary = (array) $this->_endpoint->primaryKey();
     $indexed = (new Collection($data))->groupBy(function ($el) use($primary) {
         $keys = [];
         foreach ($primary as $key) {
             $keys[] = isset($el[$key]) ? $el[$key] : '';
         }
         return implode(';', $keys);
     })->map(function ($element, $key) {
         return $key === '' ? $element : $element[0];
     })->toArray();
     $new = isset($indexed[null]) ? $indexed[null] : [];
     unset($indexed[null]);
     $output = [];
     foreach ($entities as $entity) {
         if (!$entity instanceof EntityInterface) {
             continue;
         }
         $key = implode(';', $entity->extract($primary));
         if ($key === null || !isset($indexed[$key])) {
             continue;
         }
         $output[] = $this->merge($entity, $indexed[$key], $options);
         unset($indexed[$key]);
     }
     foreach ((new Collection($indexed))->append($new) as $value) {
         if (!is_array($value)) {
             continue;
         }
         $output[] = $this->one($value, $options);
     }
     return $output;
 }
 /**
  * {@inheritDoc}
  */
 public function buildRules(RulesChecker $rules)
 {
     $rules->addCreate(function () {
         return $this->find()->count() < 25;
     }, 'maximumAmount');
     return parent::buildRules($rules);
 }
예제 #3
0
 public function schema($schema = null)
 {
     if ($schema === null) {
         if ($this->_schema === null) {
             $customFieldsEndpoint = new CustomFieldsEndpoint(['connection' => $this->connection()]);
             $this->_schema = $customFieldsEndpoint->alterSchema($this->baseSchema());
         }
         return $this->_schema;
     }
     return parent::schema($schema);
 }
 /**
  * {@inheritDoc}
  */
 public function initialize(array $config)
 {
     parent::initialize($config);
     $this->primaryKey('id');
     $this->displayField('text');
 }
 /**
  * {@inheritDoc}
  */
 public function initialize(array $config)
 {
     parent::initialize($config);
     $this->primaryKey('user_id');
     $this->displayField('screen_name');
 }
 protected function _transformResource(array $result, Endpoint $endpoint)
 {
     $properties = [];
     foreach ($result as $field => $value) {
         if ($field === 'custom_fields') {
             // Loop over custom fields
             foreach ($value as $customField) {
                 // Get the alias for the custom field
                 $customFieldField = Schema::nameToField($customField['name']);
                 // Lookup the field in the schema
                 $column = $endpoint->schema()->column($customFieldField);
                 // If no value has been given set it to null
                 if (!isset($customField['value'])) {
                     $properties[$customFieldField] = null;
                     continue;
                 }
                 // Cast value to correct type and set it as property
                 $properties[$customFieldField] = $this->castValue($customField['value'], $column['type']);
             }
             continue;
         }
         $column = $endpoint->schema()->column($field);
         if (!$column) {
             $properties[$field] = $value;
             continue;
         }
         $properties[$field] = $this->castValue($value, $column['type']);
     }
     return $this->_createResource($endpoint->resourceClass(), $properties);
 }
 /**
  * Turns a single result into a resource
  *
  * @param \Muffin\Webservice\Model\Endpoint $endpoint The endpoint class to use
  * @param array $result The API result
  *
  * @return \Muffin\Webservice\Model\Resource
  */
 protected function _transformResource(Endpoint $endpoint, array $result)
 {
     $properties = [];
     foreach ($result as $property => $value) {
         $properties[$property] = $value;
     }
     return $this->_createResource($endpoint->resourceClass(), $properties);
 }