bem/bh-php

Rework incoming data normalizing

qfox opened this issue · 1 comments

qfox commented

We should create a special classes for mods, bemjson nodes, etc.

Mods:

class Mods implements Iterator {
  protected $content = [];
  // etc.
}

JsonNode (or Json, or Node):

class JsonNode {
  protected $block = 'page';
  protected $elem = 'content';
  protected $mods = null; // Mods
  protected $blockMods = null; // Mods
  protected $elemMods = null; // Mods
  protected $attrs = 'page';
  protected $content = [];

  // etc.
  function __construct (array $bemjson) {
    $this->block = $bemjson['block'];
    $this->elem = $bemjson['elem'];
    $this->content = $bemjson['content'];
  }

  function iterate (callable $cb) {
    // call cb for each node
  }

  public static buildTree ($bemjson) {
    if (is_array($bemjson)) {
      $res = [];
      foreach ($bemjson as $node) {
        $res[] = static::buildTree($node);
      }
      return $res;
    } elseif ($bemjson instanceof self) {
      return $bemjson;
    } else {
      // etc.
      return new self($bemjson);
    }
  }
}
qfox commented

Done in 54cf162