Ejemplo n.º 1
0
 /**
  * Create instance from reques
  *
  * @param  string $method
  * @param  null|int|string $id
  * @param  array|\Illuminate\Contracts\Support\Arrayable $params
  * @return $this|array
  */
 protected static function instanceFromRequest($method, $id = null, $params = [])
 {
     $response = static::requestToArray($method, $id, $params);
     if (Arr::isAssoc($response)) {
         return new static($response);
     }
     return Collection::makeOf(static::class, $response);
 }
Ejemplo n.º 2
0
 public function getPostgisFields()
 {
     if (property_exists($this, 'postgisFields')) {
         return Arr::isAssoc($this->postgisFields) ? array_keys($this->postgisFields) : $this->postgisFields;
         //Returns the non-associative array that doesn't define the geometry type.
     } else {
         throw new PostgisFieldsNotDefinedException(__CLASS__ . ' has to define $postgisFields');
     }
 }
Ejemplo n.º 3
0
 /**
  * Builds a table-keyed array from model class names.
  *
  * @param  string[]|null $models
  * @return array|null
  */
 protected static function buildMorphMapFromModels(array $models = null)
 {
     if (is_null($models) || Arr::isAssoc($models)) {
         return $models;
     }
     $tables = array_map(function ($model) {
         return (new $model())->getEndpoint();
     }, $models);
     return array_combine($tables, $models);
 }
Ejemplo n.º 4
0
 public function setRows($rows = [])
 {
     if (Arr::isAssoc($rows)) {
         foreach ($rows as $key => $item) {
             $this->rows[] = [$key, $item];
         }
         return $this;
     }
     $this->rows = $rows;
     return $this;
 }
Ejemplo n.º 5
0
 /**
  * Get the fillable attributes for the model.
  *
  * @return array
  */
 public function getFillable()
 {
     if ($fillable = $this->fillable) {
         if (Arr::isAssoc($fillable)) {
             return $fillable;
         }
         $keys = array_values($fillable);
         $types = array_fill(0, count($keys), 'string');
         return array_combine($keys, $types);
     }
 }
Ejemplo n.º 6
0
 /**
  * Send email the handler method
  *
  * @param  int|static $resource
  * @param  array $options
  * @return bool
  */
 protected static function macroSendHandler($resource, ...$options)
 {
     if (static::isResource($resource)) {
         $resource = $resource->id;
     }
     if ($isAssoc = count($options) == 1 && Arr::isAssoc($options[0])) {
         $options = $options[0];
     }
     if (!$isAssoc) {
         $options = ['emails' => $options];
     }
     $response = static::request('POST', $resource . '/email', $options);
     return $response->getStatusCode() === 200;
 }
Ejemplo n.º 7
0
 public function add($label, $data = [], $fillColor = '')
 {
     if (is_array($label)) {
         if (Arr::isAssoc($label)) {
             $this->data[] = $label;
         } else {
             foreach ($label as $item) {
                 call_user_func_array([$this, 'add'], $item);
             }
         }
         return $this;
     }
     $this->data['datasets'][] = ['label' => $label, 'data' => $data, 'fillColor' => $fillColor];
     return $this;
 }
Ejemplo n.º 8
0
 /**
  * Prepare array elements for formatting.
  *
  * @param array $array
  * @return array
  */
 protected static function prepareArrayLines(array $array)
 {
     $isAssoc = Arr::isAssoc($array);
     array_walk($array, function (&$value, $index, $withIndexes = false) {
         if (is_array($value)) {
             $value = self::formatArray($value);
         } else {
             $value = self::formatScalar($value);
         }
         if ($withIndexes) {
             $value = self::formatScalar($index) . ' => ' . $value;
         }
     }, $isAssoc);
     return $array;
 }
Ejemplo n.º 9
0
 public function add($label, $value = '', $color = '', $highlight = '')
 {
     if (is_array($label)) {
         if (Arr::isAssoc($label)) {
             $this->data[] = $label;
         } else {
             foreach ($label as $item) {
                 call_user_func_array([$this, 'add'], $item);
             }
         }
         return $this;
     }
     $this->data[] = ['label' => $label, 'value' => $value, 'color' => $color, 'highlight' => $highlight];
     return $this;
 }
Ejemplo n.º 10
0
 /**
  * Prepare input data for insert.
  *
  * @param $inserts
  * @return array
  */
 public function prepareInsert($inserts)
 {
     $first = current($inserts);
     if (is_array($first) && Arr::isAssoc($first)) {
         $inserts = Arr::dot($inserts);
     }
     foreach ($inserts as $column => $value) {
         if (is_null($field = $this->getFieldByColumn($column))) {
             unset($inserts[$column]);
             continue;
         }
         if (method_exists($field, 'prepare')) {
             $inserts[$column] = $field->prepare($value);
         }
     }
     $prepared = [];
     foreach ($inserts as $key => $value) {
         Arr::set($prepared, $key, $value);
     }
     return $prepared;
 }
Ejemplo n.º 11
0
 /**
  * Validate the value as safe for this object
  *
  * @param  mixed  $value the value to test
  * @return boolean
  */
 private function isValidValue($value)
 {
     return is_scalar($value) || is_array($value) && !$this->arrHelper->isAssoc($value) && count($value) === count(array_filter($value, 'is_scalar'));
 }
Ejemplo n.º 12
0
 /**
  * Process model relationship
  *
  * @param array $data
  * @param array $relationships
  * @param string $relationshipMethod
  * @return void
  */
 protected function processRelationship(&$data, $relationships, $relationshipMethod)
 {
     if (!isset($data['relationship'])) {
         $data['relationship'] = [];
     }
     $relationships = Arr::isAssoc($relationships) ? [$relationships] : $relationships;
     foreach ($relationships as $relationship) {
         $method = $relationshipMethod . 'Relationship';
         $data['relationship'][] = $this->grammar->{$method}($relationship);
     }
 }
Ejemplo n.º 13
0
 /**
  * Structure the matches array.
  *
  * @param  \Phroggyy\Discover\Contracts\Searchable $model
  * @param  array $query
  * @return array|string
  */
 private function structureMatches(Searchable $model, $query)
 {
     // If the query is an array of arrays, we assume the
     // developer knows exactly what they're doing and
     // are providing a complete match query for us.
     if (is_array($query) && !Arr::isAssoc($query)) {
         return $query;
     }
     $key = '';
     // If the search index turns out to actually be nested, we
     // want to make sure we use the name of the subdocument
     // when such a query is performed. This is necessary.
     if ($this->indexIsNested($model->getSearchIndex())) {
         $key = $this->retrieveNestedIndex($model->getSearchIndex())[1] . '.';
     }
     // If the query is just a string, we assume the user
     // intends to just do a simple match query on the
     // default search field defined in their model.
     if (is_string($query)) {
         return [['match' => [$key . $model->getDefaultSearchField() => $query]]];
     }
     $query = Collection::make($query);
     return $query->map(function ($constraint, $property) use($key) {
         if (strpos($property, '.') === false) {
             $property = $key . $property;
         }
         return ['match' => [$property => $constraint]];
     })->values()->all();
 }
Ejemplo n.º 14
0
 /**
  * Normalizes ACL presence data to an array of ACLPresence instances.
  *
  * @param $data
  * @return AclPresenceInterface[]
  */
 protected function normalizeAclPresence($data)
 {
     if ($data instanceof AclPresenceInterface) {
         $data = [$data];
     } elseif (is_array($data) && !Arr::isAssoc($data)) {
         $presences = [];
         foreach ($data as $nestedData) {
             $presences[] = new AclPresence($nestedData);
         }
         $data = $presences;
     } else {
         $data = [new AclPresence($data)];
     }
     /** @var AclPresenceInterface[] $data */
     // Now normalized to an array with one or more ACL presences.
     // Make sure the permission children are listed as simple strings.
     foreach ($data as $index => $presence) {
         $children = $presence->permissions() ?: [];
         if ($children instanceof Arrayable) {
             $children = $children->toArray();
         }
         if (!is_array($children)) {
             $children = [$children];
         }
         $data[$index]->setPermissions($children);
     }
     return $data;
 }
Ejemplo n.º 15
0
 /**
  * Get casts from fillable attribute
  * When fillable is assoc the value is the type
  *
  * @return array
  */
 protected function getCastFromFillable()
 {
     $fillable = $this->fillable;
     if (Arr::isAssoc($fillable)) {
         return $fillable;
     }
     return [];
 }
Ejemplo n.º 16
0
 /**
  * ParseACL can be set by passing "acl" as key. This is useful specially in
  * mass assigments, e.g. ACL can be set alongside attributes with create().
  *
  * @param string $key
  * @param mixed $value
  */
 public function set($key, $value)
 {
     if (is_array($value)) {
         if (Arr::isAssoc($value)) {
             $this->parseObject->setAssociativeArray($key, $value);
         } else {
             $this->parseObject->setArray($key, $value);
         }
     } elseif ($value instanceof ObjectModel) {
         $this->parseObject->set($key, $value->parseObject);
     } elseif ($key == 'acl') {
         $this->parseObject->setACL($value);
     } else {
         $this->parseObject->set($key, $value);
     }
     return $this;
 }
Ejemplo n.º 17
0
 /**
  * Response to get method
  *
  * @param  string $path
  * @param  array $options
  * @return \GuzzleHttp\Psr7\Response
  */
 protected function get($path, $options)
 {
     list($path, $id) = static::parsePath($path);
     $resources = $this->collections->get($path);
     if (!count($resources) && ($fileStorage = $this->files->find('get', $path))) {
         if (!Arr::isAssoc($fileStorage)) {
             $fileStorage = Collection::make($fileStorage);
         }
         $this->collections->put($path, $resources = $fileStorage);
     }
     if ($id !== null) {
         $resources = $this->first($resources, $id);
     } elseif ($options['query']) {
         $resources = $this->filterApply($resources, $options['query']);
     }
     $status = count($resources) > 0 ? 200 : 404;
     return $this->createResponse($status, $resources);
 }
Ejemplo n.º 18
0
 /**
  * Normalizes menu presence data to an array of MenuPresence instances.
  *
  * @param $data
  * @return MenuPresenceInterface[]
  */
 protected function normalizeMenuPresence($data)
 {
     if ($data instanceof MenuPresenceInterface) {
         $data = [$data];
     } elseif (is_array($data) && !Arr::isAssoc($data)) {
         $presences = [];
         foreach ($data as $nestedData) {
             $presences[] = new MenuPresence($nestedData);
         }
         $data = $presences;
     } else {
         $data = [new MenuPresence($data)];
     }
     /** @var MenuPresenceInterface[] $data */
     // Now normalized to an array with one or more menu presences.
     // Make sure the children of each menu presence are normalized aswell.
     foreach ($data as $index => $presence) {
         $children = $presence->children();
         if (!empty($children)) {
             $data[$index]->setChildren($this->normalizeMenuPresence($children));
         }
     }
     return $data;
 }