1: <?php
2:
3: /**
4: * Copyright 2015, Eduardo Trujillo
5: *
6: * For the full copyright and license information, please view the LICENSE
7: * file that was distributed with this source code.
8: *
9: * This file is part of the Nucleus package
10: */
11:
12: namespace Chromabits\Nucleus\Meditation\Constraints;
13:
14: /**
15: * Class TraversableOfConstraint.
16: *
17: * @author Eduardo Trujillo <ed@chromabits.com>
18: * @package Chromabits\Nucleus\Meditation\Constraints
19: */
20: class LeftFoldableOfConstraint extends AbstractConstraint
21: {
22: /**
23: * @var AbstractConstraint
24: */
25: protected $valueConstraint;
26:
27: /**
28: * Construct an instance of a TraversableOfConstraint.
29: *
30: * @param AbstractConstraint $valueConstraint
31: */
32: public function __construct(AbstractConstraint $valueConstraint)
33: {
34: parent::__construct();
35:
36: $this->valueConstraint = $valueConstraint;
37: }
38:
39: /**
40: * Check the type of the traversable container.
41: *
42: * @param mixed $value
43: * @param array $context
44: *
45: * @return bool
46: */
47: protected function checkContainerType($value, $context = [])
48: {
49: return (new LeftFoldableConstraint())->check($value, $context);
50: }
51:
52: /**
53: * Check if the constraint is met.
54: *
55: * @param mixed $value
56: * @param array $context
57: *
58: * @return mixed
59: */
60: public function check($value, array $context = [])
61: {
62: if ($this->checkContainerType($value, $context) === false) {
63: return false;
64: }
65:
66: foreach ($value as $item) {
67: if ($this->valueConstraint->check($item, $context) === false) {
68: return false;
69: }
70: }
71:
72: return true;
73: }
74:
75: /**
76: * Get string representation of this constraint.
77: *
78: * @return string
79: */
80: public function toString()
81: {
82: return vsprintf(
83: 'Traversable<%s>',
84: [$this->valueConstraint->toString()]
85: );
86: }
87: }
88: