Skip to content

Commit

Permalink
Add classes for data types
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasGraml11 committed Nov 2, 2023
1 parent a31543b commit 9ba7202
Show file tree
Hide file tree
Showing 21 changed files with 1,895 additions and 2 deletions.
63 changes: 63 additions & 0 deletions src/Result/Graphql/Search/SearchResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Nosto\Result\Graphql\Search;

use Nosto\Result\Graphql\Search\SearchResult\Explain;
use Nosto\Result\Graphql\Search\SearchResult\Products;
use Nosto\Util\GraphQL;
use stdClass;

class SearchResult
{
/** @var ?string */
private $redirect;

/** @var ?string */
private $query;

/** @var ?Explain */
private $explain;

/** @var ?Products */
private $products;

public function __construct(stdClass $data)
{
$this->redirect = GraphQL::getProperty($data, 'redirect');
$this->query = GraphQL::getProperty($data, 'query');
$this->explain = GraphQL::getClassProperty($data, 'explain', Explain::class);
$this->products = GraphQL::getClassProperty($data, 'products', Products::class);
}

/**
* @return ?string
*/
public function getRedirect()
{
return $this->redirect;
}

/**
* @return ?string
*/
public function getQuery()
{
return $this->query;
}

/**
* @return ?Explain
*/
public function getExplain()
{
return $this->explain;
}

/**
* @return ?Products
*/
public function getProducts()
{
return $this->products;
}
}
26 changes: 26 additions & 0 deletions src/Result/Graphql/Search/SearchResult/Explain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Nosto\Result\Graphql\Search\SearchResult;

use Nosto\Result\Graphql\Search\SearchResult\Explain\MatchedRule;
use Nosto\Util\GraphQL;
use stdClass;

class Explain
{
/** @var ?MatchedRule[] */
private $matchedRules;

public function __construct(stdClass $data)
{
$this->matchedRules = GraphQL::getArrayProperty($data, 'matchedRules', MatchedRule::class);
}

/**
* @return ?MatchedRule[]
*/
public function getMatchedRules()
{
return $this->matchedRules;
}
}
49 changes: 49 additions & 0 deletions src/Result/Graphql/Search/SearchResult/Explain/MatchedRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Nosto\Result\Graphql\Search\SearchResult\Explain;

use Nosto\Util\GraphQL;
use stdClass;

class MatchedRule
{
/** @var ?string */
private $id;

/** @var ?string */
private $name;

/** @var ?stdClass */
private $set;

public function __construct(stdClass $data)
{
$this->id = GraphQL::getProperty($data, 'id');
$this->name = GraphQL::getProperty($data, 'name');
$this->set = GraphQL::getProperty($data, 'set');
}

/**
* @return ?string
*/
public function getId()
{
return $this->id;
}

/**
* @return ?string
*/
public function getName()
{
return $this->name;
}

/**
* @return ?stdClass
*/
public function getSet()
{
return $this->set;
}
}
130 changes: 130 additions & 0 deletions src/Result/Graphql/Search/SearchResult/Products.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Nosto\Result\Graphql\Search\SearchResult;

use Nosto\Result\Graphql\Search\SearchResult\Products\Facet;
use Nosto\Result\Graphql\Search\SearchResult\Products\Hit;
use Nosto\Util\GraphQL;
use stdClass;

class Products
{
/** @var ?int */
private $total;

/** @var ?int */
private $size;

/** @var ?int */
private $from;

/** @var ?string */
private $collapse;

/** @var ?boolean */
private $fuzzy;

/** @var ?string */
private $categoryId;

/** @var ?string */
private $categoryPath;

/** @var ?Hit[] */
private $hits;

/** @var ?Facet[] */
private $facets;

public function __construct(stdClass $data)
{
$this->total = GraphQL::getProperty($data, 'total');
$this->size = GraphQL::getProperty($data, 'size');
$this->from = GraphQL::getProperty($data, 'from');
$this->collapse = GraphQL::getProperty($data, 'collapse');
$this->fuzzy = GraphQL::getProperty($data, 'fuzzy');
$this->categoryId = GraphQL::getProperty($data, 'categoryId');
$this->categoryPath = GraphQL::getProperty($data, 'categoryPath');
$this->hits = GraphQL::getArrayProperty($data, 'hits', Hit::class);
$this->facets = property_exists($data, 'facets') && $data->facets
? array_map(
function (stdClass $facet) {
return Facet::getInstance($facet);
},
$data->facets
)
: null;
}

/**
* @return ?int
*/
public function getTotal()
{
return $this->total;
}

/**
* @return ?int
*/
public function getSize()
{
return $this->size;
}

/**
* @return ?int
*/
public function getFrom()
{
return $this->from;
}

/**
* @return ?string
*/
public function getCollapse()
{
return $this->collapse;
}

/**
* @return ?bool
*/
public function getFuzzy()
{
return $this->fuzzy;
}

/**
* @return ?string
*/
public function getCategoryId()
{
return $this->categoryId;
}

/**
* @return ?string
*/
public function getCategoryPath()
{
return $this->categoryPath;
}

/**
* @return ?Hit[]
*/
public function getHits()
{
return $this->hits;
}

/**
* @return ?Facet[]
*/
public function getFacets()
{
return $this->facets;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Nosto\Result\Graphql\Search\SearchResult\Products;

class BasicFacet extends Facet
{
}
82 changes: 82 additions & 0 deletions src/Result/Graphql/Search/SearchResult/Products/Facet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Nosto\Result\Graphql\Search\SearchResult\Products;

use Exception;
use Nosto\Util\GraphQL;
use stdClass;

abstract class Facet
{
const STATS_TYPE = 'stats';

const TERMS_TYPE = 'terms';

/** @var ?string */
private $id;

/** @var ?string */
private $name;

/** @var ?string */
private $field;

/** @var ?string */
private $type;

public function __construct(stdClass $data)
{
$this->id = GraphQL::getProperty($data, 'id');
$this->name = GraphQL::getProperty($data, 'name');
$this->field = GraphQL::getProperty($data, 'field');
$this->type = GraphQL::getProperty($data, 'type');
}

/**
* @return Facet
* @throws Exception
*/
public static function getInstance(stdClass $facet)
{
switch ($facet->type) {
case self::STATS_TYPE:
return new StatsFacet($facet);
case self::TERMS_TYPE:
return new TermsFacet($facet);
default:
return new BasicFacet($facet);
}
}

/**
* @return ?string
*/
public function getId()
{
return $this->id;
}

/**
* @return ?string
*/
public function getName()
{
return $this->name;
}

/**
* @return ?string
*/
public function getField()
{
return $this->field;
}

/**
* @return ?string
*/
public function getType()
{
return $this->type;
}
}
Loading

0 comments on commit 9ba7202

Please sign in to comment.