Skip to content

Commit

Permalink
Merge pull request #9 from adamgoose/develop
Browse files Browse the repository at this point in the history
Fixing compatability issue with Prismic's PHP Kit
  • Loading branch information
adamgoose committed Jul 29, 2014
2 parents fe2e770 + d8d6404 commit b70c07b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor
composer.phar
composer.lock
.DS_Store
.DS_Store
.idea
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": ">=5.3.0",
"php": ">=5.4.0",
"illuminate/support": "~4.0",
"prismic/php-sdk": "dev-master"
},
Expand All @@ -19,5 +19,5 @@
"Adamgoose\\PrismicIo": "src/"
}
},
"minimum-stability": "dev"
"minimum-stability": "stable"
}
14 changes: 8 additions & 6 deletions src/Adamgoose/PrismicIo/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ abstract class Model {

protected $endpoint;
protected $token;
protected $ref;
public $ref;

public $collection;
public $mask;
public $tags;

public $conditions = array();
public $conditions = [];

public $pageSize = 20;
public $page = 1;
Expand All @@ -23,7 +23,9 @@ abstract class Model {
/**
* Grab variables from config
*
* @return void
* @param Document $document
*
* @return \Adamgoose\PrismicIo\Model
*/
public function __construct(Document $document = null)
{
Expand Down Expand Up @@ -57,10 +59,10 @@ public function newQuery()
public function __call($method, $parameters)
{
if($this->document instanceof Document) {
return call_user_func_array(array($this->document, $method), $parameters);
return call_user_func_array([$this->document, $method], $parameters);
} else {
$query = $this->newQuery();
return call_user_func_array(array($query, $method), $parameters);
return call_user_func_array([$query, $method], $parameters);
}
}

Expand All @@ -75,7 +77,7 @@ public static function __callStatic($method, $parameters)
{
$instance = new static;

return call_user_func_array(array($instance, $method), $parameters);
return call_user_func_array([$instance, $method], $parameters);
}

/**
Expand Down
63 changes: 38 additions & 25 deletions src/Adamgoose/PrismicIo/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class Query {
* Creates a new Query instance.
*
* @param \Adamgoose\PrismicIo\Model $model
* @return void
*
* @return \Adamgoose\PrismicIo\Query
*/
public function __construct(Model $model)
{
Expand All @@ -21,7 +22,8 @@ public function __construct(Model $model)
/**
* Sets the ref of the query
*
* @param string $ref
* @param string $ref
*
* @return \Adamgoose\PrismicIo\Query
*/
public function ref($ref)
Expand All @@ -34,7 +36,8 @@ public function ref($ref)
/**
* Sets the collection of the query
*
* @param string $collection
* @param string $collection
*
* @return \Adamgoose\PrismicIo\Query
*/
public function collection($collection)
Expand All @@ -47,7 +50,8 @@ public function collection($collection)
/**
* Sets the mask of the query
*
* @param string $mask
* @param string $mask
*
* @return \Adamgoose\PrismicIo\Query
*/
public function mask($mask)
Expand All @@ -60,7 +64,8 @@ public function mask($mask)
/**
* Sets the tags of the query
*
* @param array $tags
* @param array $tags
*
* @return \Adamgoose\PrismicIo\Query
*/
public function tags(array $tags)
Expand All @@ -75,7 +80,8 @@ public function tags(array $tags)
*
* @param string $key
* @param string $value
* @return \Adamgoose\Prismic\Query
*
* @return \Adamgoose\PrismicIo\Query
*/
public function at($key, $value)
{
Expand All @@ -89,7 +95,8 @@ public function at($key, $value)
*
* @param string $key
* @param array $values
* @return \Adamgoose\Prismic\Query
*
* @return \Adamgoose\PrismicIo\Query
*/
public function any($key, array $values)
{
Expand All @@ -103,7 +110,8 @@ public function any($key, array $values)
*
* @param string $key
* @param string $value
* @return \Adamgoose\Prismic\Query
*
* @return \Adamgoose\PrismicIo\Query
*/
public function fulltext($key, $value)
{
Expand All @@ -116,7 +124,8 @@ public function fulltext($key, $value)
* Define the pageSize for the query
*
* @param int $pageSize
* @return \Adamgoose\Prismic\Query
*
* @return \Adamgoose\PrismicIo\Query
*/
public function pageSize($pageSize)
{
Expand All @@ -129,7 +138,8 @@ public function pageSize($pageSize)
* Define which page to return
*
* @param int $page
* @return \Adamgoose\Prismic\Query
*
* @return \Adamgoose\PrismicIo\Query
*/
public function page($page)
{
Expand All @@ -142,13 +152,14 @@ public function page($page)
* Alias for at('document.id', $id)
*
* @param string $id
*
* @return \Prismic\Document
*/
public function find($id)
{
$collection = $this->get();

$collection = $collection->filter(function($document) use ($id)
$collection = $collection->filter(function ($document) use ($id)
{
if($document->id == $id) return true;
});
Expand All @@ -160,13 +171,14 @@ public function find($id)
* Return the document with matching slug
*
* @param string $slug
*
* @return \Prismic\Document
*/
public function findSlug($slug)
{
$collection = $this->get();

$collection = $collection->filter(function($document) use ($slug)
$collection = $collection->filter(function ($document) use ($slug)
{
if($document->containsSlug($slug)) return true;
});
Expand All @@ -176,7 +188,6 @@ public function findSlug($slug)

/**
* Alias for get()->first();
*
* @return \Prismic\Document
*/
public function first()
Expand All @@ -186,7 +197,6 @@ public function first()

/**
* Execute the query
*
* @return \Illuminate\Support\Collection
*/
public function get()
Expand All @@ -197,28 +207,31 @@ public function get()

// Set mask using predicated query
if($this->model->mask != null)
$query .= '[:d = at(document.type, "'.$this->model->mask.'")]';
$query .= '[:d = at(document.type, "' . $this->model->mask . '")]';

// Set tags using predicated query
if($this->model->tags != null)
$query .= '[:d = any(document.tags, ["'.implode('","', $this->model->tags).'"])]';
$query .= '[:d = any(document.tags, ["' . implode('","', $this->model->tags) . '"])]';

// Set "at" predicated queries
if(array_key_exists('at', $this->model->conditions))
foreach($this->model->conditions['at'] as $at) {
$query .= '[:d = at('.$at['key'].', "'.$at['value'].'")]';
foreach($this->model->conditions['at'] as $at)
{
$query .= '[:d = at(' . $at['key'] . ', "' . $at['value'] . '")]';
}

// Set "any" predicated queries
if(array_key_exists('any', $this->model->conditions))
foreach($this->model->conditions['any'] as $any) {
$query .= '[:d = any('.$any['key'].', ["'.implode('","', $any['values']).'"])]';
foreach($this->model->conditions['any'] as $any)
{
$query .= '[:d = any(' . $any['key'] . ', ["' . implode('","', $any['values']) . '"])]';
}

// Set "fulltext" predicated queries
if(array_key_exists('fulltext', $this->model->conditions))
foreach($this->model->conditions['fulltext'] as $fulltext) {
$query .= '[:d = fulltext('.$fulltext['key'].', "'.$fulltext['value'].'")]';
foreach($this->model->conditions['fulltext'] as $fulltext)
{
$query .= '[:d = fulltext(' . $fulltext['key'] . ', "' . $fulltext['value'] . '")]';
}

// Determine which API form to use
Expand All @@ -243,15 +256,14 @@ public function get()
$class = get_class($this->model);

$models = [];
foreach($results as $result)
foreach($results->getResults() as $result)
$models[] = new $class($result);

return new Collection($models);
}

/**
* Prepare API for calls
*
* @return \Prismic\Api
*/
private function prepareApi()
Expand All @@ -265,6 +277,8 @@ private function prepareApi()
/**
* Returns either the master ref, or the defined ref
*
* @param Api $api
*
* @return string
*/
private function getRef(Api $api)
Expand All @@ -274,5 +288,4 @@ private function getRef(Api $api)

return $api->master()->getRef();
}

}

0 comments on commit b70c07b

Please sign in to comment.