Skip to content

Commit

Permalink
Also added support for params
Browse files Browse the repository at this point in the history
  • Loading branch information
Niklan committed Feb 28, 2018
1 parent 6a89360 commit 618588f
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 22 deletions.
32 changes: 15 additions & 17 deletions src/YandexYml/Offer/YandexYmlOfferBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\Component\Utility\Unicode;
use Drupal\yandex_yml\Annotation\YandexYmlAttribute;
use Drupal\yandex_yml\Annotation\YandexYmlElement;
use Drupal\yandex_yml\Annotation\YandexYmlElementWrapper;
use Drupal\yandex_yml\Annotation\YandexYmlElementWrapperAttribute;
use Drupal\yandex_yml\YandexYml\YandexYmlToArrayTrait;
Expand Down Expand Up @@ -217,17 +218,7 @@ abstract class YandexYmlOfferBase {
protected $cpa;

/**
* @TODO
* YandexYml(
* elementName = "param",
* type = "array_map",
* parentElement = "offer",
* array_map = {
* "name": "property",
* "value": "property",
* "unit": "property"
* }
* )
* @YandexYmlElement()
*
* @var array
*/
Expand Down Expand Up @@ -825,7 +816,14 @@ public function getCpa() {
* @return YandexYmlOfferBase
*/
public function setParam($name, $value, $unit = NULL) {
$this->param[] = ['name' => $name, 'value' => $value, 'unit' => $unit];
$param = \Drupal::service('yandex_yml.param')
->setName($name)
->setValue($value);

if ($unit) {
$param->setUnit($unit);
}
$this->param[] = $param;
return $this;
}

Expand Down Expand Up @@ -969,22 +967,22 @@ public function getGroupId() {
}

/**
* Set id's of recommended products separated by comma.
* Set id's of recommended products.
*
* @param string $rec
* @param array $rec
*
* @return YandexYmlOfferBase
*/
public function setRec($rec) {
$this->rec = $rec;
public function setRec(array $rec) {
$this->rec = implode(',', $rec);
return $this;
}

/**
* @return array
*/
public function getRec() {
return $this->rec;
return explode(',', $this->rec);
}

}
97 changes: 97 additions & 0 deletions src/YandexYml/Param/YandexYmlParam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Drupal\yandex_yml\YandexYml\Param;

use Drupal\yandex_yml\Annotation\YandexYmlElement;
use Drupal\yandex_yml\Annotation\YandexYmlAttribute;
use Drupal\yandex_yml\Annotation\YandexYmlValue;
use Drupal\yandex_yml\YandexYml\YandexYmlToArrayTrait;

/**
* Class YandexYmlParam
*
* Used as abstraction for YML element.
*
* @YandexYmlElement(
* name = "param"
* )
*
* @package Drupal\yandex_yml\YandexYml\Param
*/
class YandexYmlParam {

use YandexYmlToArrayTrait;

/**
* @YandexYmlAttribute()
*
* @var string
*/
private $name;

/**
* @YandexYmlAttribute()
*
* @var string
*/
private $unit;

/**
* @YandexYmlValue()
*
* @var string
*/
private $value;

/**
* @param string $name
*
* @return YandexYmlParam
*/
public function setName($name) {
$this->name = $name;
return $this;
}

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

/**
* @param string $unit
*
* @return YandexYmlParam
*/
public function setUnit($unit) {
$this->unit = $unit;
return $this;
}

/**
* @return string
*/
public function getUnit() {
return $this->unit;
}

/**
* @param string $value
*
* @return YandexYmlParam
*/
public function setValue($value) {
$this->value = $value;
return $this;
}

/**
* @return string
*/
public function getValue() {
return $this->value;
}

}
21 changes: 16 additions & 5 deletions src/YandexYml/YandexYmlToArrayTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ private function parseAnnotations() {
$child = $this->getElementWrappers();
$element['child'] = $child;

$elements = $this->getElementsFromProperties();
$element['child'] = array_merge($element['child'], $elements);

$result[] = $element;
}
else {
Expand Down Expand Up @@ -139,11 +142,19 @@ private function getElementsFromProperties() {
$reflection = new \ReflectionProperty($this, $name);
$property_annotation = $this->reader->getPropertyAnnotation($reflection, 'Drupal\yandex_yml\Annotation\YandexYmlElement');
if ($property_annotation) {
$element_name = !empty($property_annotation->get()['name']) ? $property_annotation->get()['name'] : $name;
$elements[] = [
'name' => $element_name,
'value' => $value,
];
if (is_array($value)) {
ksm($value);
foreach ($value as $value_element) {
$elements = array_merge($elements, $value_element->toArray());
}
}
else {
$element_name = !empty($property_annotation->get()['name']) ? $property_annotation->get()['name'] : $name;
$elements[] = [
'name' => $element_name,
'value' => $value,
];
}
}
}
return $elements;
Expand Down
4 changes: 4 additions & 0 deletions yandex_yml.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ services:
yandex_yml.shop:
class: Drupal\yandex_yml\YandexYml\Shop\YandexYmlShop
arguments: []
shared: false
yandex_yml.param:
class: Drupal\yandex_yml\YandexYml\Param\YandexYmlParam
arguments: []
shared: false

0 comments on commit 618588f

Please sign in to comment.