/**
  * {inheritdoc}
  */
 public function serialize($value, array $formatters)
 {
     if (!is_object($value)) {
         return [];
     }
     $evaluator = new ObjectFieldEvaluator($value);
     if ($this->rule->isFieldAllIncludes()) {
         $this->rule->includes($evaluator->listFields());
     }
     return $evaluator->evaluate($this->rule->listIncludeFields(), $formatters);
 }
 private static function createFieldStrategy(FilterRule $rule)
 {
     if ($rule->isObjectRule()) {
         $strategy = new Strategy\ObjectToArrayStrategy($rule);
         if (count($rule->nestedFilters) > 0) {
             $strategy = new Strategy\ObjectSubsetStrategy($strategy);
         }
     } else {
         $strategy = new Strategy\AssocArrayEncodeStrategy($rule);
     }
     foreach ($rule->nestedFilters as $field => $r) {
         $strategy->append($field, static::createFieldStrategy($r));
     }
     return $strategy;
 }
 /**
  * {inheritdoc}
  */
 public function serialize($value, array $formatters)
 {
     if (!is_array($value)) {
         return [];
     }
     $value1 = $this->rule->intersectByKey($value);
     $value1 = array_reduce(array_keys($value1), function (array &$tmp, $k) use($value1, $formatters) {
         $v = $value1[$k];
         if (is_object($v)) {
             $class = get_class($v);
             if (!isset($formatters[$class])) {
                 throw new \Exception("formatter is not found (type: {$class})");
             }
             $v = $formatters[$class]->format($v);
         }
         return $tmp + [$k => $v];
     }, []);
     $value2 = array_reduce(array_keys($this->strategies), function (&$tmp, $f) use($value, $formatters) {
         return isset($value[$f]) ? $tmp + [$f => $this->strategies[$f]->serialize($value[$f], $formatters)] : $tmp;
     }, []);
     return $value1 + $value2;
 }
 /**
  * @test
  */
 public function test_build_as_datetime_assoc_array()
 {
     $rule = FilterRule::newRule()->withArrayRule()->includes(['b']);
     $builder = EncoderBuilder::ofAssocArray($rule);
     $values = ['a' => new \DateTime('2015/12/13 14:15:36', new \DateTimeZone('Asia/Tokyo')), 'b' => new \DateTime('2008/2/29 11:59:59', new \DateTimeZone('Asia/Tokyo')), 'c' => new \DateTime('2000/11/10 9:8:7', new \DateTimeZone('Asia/Tokyo'))];
     $result = $builder->strategy()->serialize($values, $builder->formatters());
     $this->assertEquals(['b' => '2008-02-29T11:59:59+0900'], $result);
     $serializer = $builder->build($values);
     $result2 = $serializer->jsonSerialize();
     $this->assertEquals(['b' => '2008-02-29T11:59:59+0900'], $result2);
 }
 private function newRule(array $fieldNames)
 {
     return \JsonEncoder\FilterRule::newRule()->includes($fieldNames);
 }