Skip to content

Commit

Permalink
add offers option (#2015)
Browse files Browse the repository at this point in the history
* add offers option

* add changelog

* add offer test

* add exception test
  • Loading branch information
nadar committed Apr 28, 2020
1 parent 3dc3c71 commit 4a0fdfe
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 4 deletions.
1 change: 1 addition & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ In order to read more about upgrading and BC breaks have a look at the [UPGRADE
+ [#2011](https://github.com/luyadev/luya/pull/2011) Update theme create command to work on Windows
+ [#2009](https://github.com/luyadev/luya/pull/2009) Improve the performance of mb_str_split, this will strongly improve the speed when working with LUYA crawler result previews.
+ [#2012](https://github.com/luyadev/luya/pull/2012) Extend the create theme cli command with assets.
+ [#2015](https://github.com/luyadev/luya/pull/2015) JsonLd Offers information added.

## 1.2.1 (7. April 2020)

Expand Down
8 changes: 7 additions & 1 deletion core/web/jsonld/CurrencyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace luya\web\jsonld;

use yii\base\InvalidConfigException;

/**
* The currency accepted.
*
Expand All @@ -17,11 +19,15 @@ class CurrencyValue extends BaseValue

public function __construct($currency)
{
if (strlen($currency) !== 3) {
throw new InvalidConfigException("The currency value must have 3 letter code like USD, CHF. Value \"{$currency}\" given.");
}

$this->_currency = $currency;
}

public function getValue()
{
return $this->_currency;
return strtoupper($this->_currency);
}
}
33 changes: 33 additions & 0 deletions core/web/jsonld/OfferInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,37 @@
*/
interface OfferInterface extends ThingInterface
{
/**
* Price Setter
*
* @param PriceValue $price
* @return static
* @since 1.2.2
*/
public function setPrice(PriceValue $price);

/**
* Price Getter
*
* @return mixed
* @since 1.2.2
*/
public function getPrice();

/**
* Price Currency Setter.
*
* @param CurrencyValue $currencyValue
* @return static
* @since 1.2.2
*/
public function setPriceCurrency(CurrencyValue $currencyValue);

/**
* Price Currency Getter
*
* @return string
* @since 1.2.2
*/
public function getPriceCurrency();
}
54 changes: 54 additions & 0 deletions core/web/jsonld/OfferTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,58 @@
trait OfferTrait
{
use ThingTrait;

private $_availability;

private $_price;

/**
* Price Setter
*
* @param PriceValue $price
* @return static
* @since 1.2.2
*/
public function setPrice(PriceValue $price)
{
$this->_price = $price->getValue();
return $this;
}

/**
* Get Price
*
* @return mixed
* @since 1.2.2
*/
public function getPrice()
{
return $this->_price;
}

private $_priceCurrency;

/**
* Price Currency Setter.
*
* @param CurrencyValue $currencyValue
* @return static
* @since 1.2.2
*/
public function setPriceCurrency(CurrencyValue $currencyValue)
{
$this->_priceCurrency = $currencyValue->getValue();
return $this;
}

/**
* Get Price Currency
*
* @return string
* @since 1.2.2
*/
public function getPriceCurrency()
{
return $this->_priceCurrency;
}
}
26 changes: 26 additions & 0 deletions core/web/jsonld/PriceValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace luya\web\jsonld;

/**
* The price accepted.
*
* Use '.' (Unicode 'FULL STOP' (U+002E)) rather than ',' to indicate a decimal point. Avoid using these symbols as a readability separator.
*
* @author Basil Suter <basil@nadar.io>
* @since 1.2.2
*/
class PriceValue extends BaseValue
{
private $_price;

public function __construct($price)
{
$this->_price = $price;
}

public function getValue()
{
return str_replace(",", ".", $this->_price);
}
}
16 changes: 16 additions & 0 deletions core/web/jsonld/ThingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,20 @@ public function getUrl();
* @return static
*/
public function setUrl(UrlValue $url);

/**
* Set Offer
*
* @param Offer $offer
* @since 1.2.2
*/
public function setOffers(Offer $offers);

/**
* Get Offer
*
* @return Offer
* @since 1.2.2
*/
public function getOffers();
}
26 changes: 26 additions & 0 deletions core/web/jsonld/ThingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,30 @@ public function setUrl(UrlValue $url)
$this->_url = $url->getValue();
return $this;
}

private $_offers;

/**
* Set Offer
*
* @param Offer $offer
* @since @since 1.2.2
* @return static
*/
public function setOffers(Offer $offers)
{
$this->_offers = $offers;
return $this;
}

/**
* Get Offer
*
* @return Offer
* @since 1.2.2
*/
public function getOffers()
{
return $this->_offers;
}
}
44 changes: 41 additions & 3 deletions tests/core/web/JsonLdTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace luyatests\core\web;

use luya\testsuite\cases\ConsoleApplicationTestCase;
use luya\web\jsonld\Article;
use luya\web\jsonld\BlogPosting;
use luya\web\jsonld\CreativeWork;
Expand Down Expand Up @@ -30,11 +31,21 @@
use luya\web\jsonld\FoodEstablishment;
use luya\web\jsonld\Restaurant;
use luya\web\jsonld\GeoCoordinates;
use luya\web\jsonld\PriceValue;
use luya\web\jsonld\Review;
use luya\web\jsonld\RangeValue;
use yii\base\InvalidConfigException;

class JsonLdTest extends \luyatests\LuyaConsoleTestCase
class JsonLdTest extends ConsoleApplicationTestCase
{
public function getConfigArray()
{
return [
'id' => 'mytestapp',
'basePath' => dirname(__DIR__),
];
}

public function testAssignView()
{
Jsonld::addGraph(['foo' => 'bar']);
Expand All @@ -51,9 +62,11 @@ public function testAssignView()
public function testBaseThingGetters()
{
$thing = (new Thing());
$same = ['name', 'additionalType', 'alternateName', 'description', 'disambiguatingDescription', 'identifier', 'image', 'mainEntityOfPage', 'sameAs', 'subjectOf', 'url'];
$same = ['name', 'additionalType', 'alternateName', 'description', 'offers', 'disambiguatingDescription', 'identifier', 'image', 'mainEntityOfPage', 'sameAs', 'subjectOf', 'url'];
sort($same);
$this->assertSame($same, $thing->resolveGetterMethods());
$r = $thing->resolveGetterMethods();
sort($r);
$this->assertSame($same, $r);
}

/**
Expand Down Expand Up @@ -402,4 +415,29 @@ public function testRestaurantWithPlaceGeoCordinates()

], $r->toArray());
}

public function testInvalidCurrencyValue()
{
$this->expectException(InvalidConfigException::class);
new CurrencyValue('CH');
}

public function testOffers()
{
$o = (new Offer())
->setOffers(
(new Offer())
->setPrice(new PriceValue('12,34'))
->setPriceCurrency(new CurrencyValue('CHF'))
);

$this->assertSame([
'offers' => [
'price' => '12.34',
'priceCurrency' => 'CHF',
'@type' => 'Offer',
],
'@type' => 'Offer'
], $o->toArray());
}
}

0 comments on commit 4a0fdfe

Please sign in to comment.