title: validation in entity classes
body: <p>Let's say I have an entity class like this:</p>
  
  <pre><code>class car
  {
    private $price;
    private $color;
  
    public __get($name){ ... }
    public __set($name, $value){ ... }
    ...
  }
  </code></pre>
  
  <p>and a manager class like this:</p>
  
  <pre><code>class CarDealer
  {
    private $db;
  
    public __construct(IDatabase $db) {...}
    public sell(Car $car) {...}
    public insert(Car $car) {...}
    ...
  }
  </code></pre>
  
  <p>The question is how can I validate a car's properties? I can do this in the setter method of the car class by checking with <code>if</code> statements. </p>
  
  <p>But I think, if there is already a validation service, Symfony\Component\Validator, I should use this. Here, I cannot decide where the validator service comes from. </p>
  
  <p>a) I can depend on this validator and use, by taking it as a parameter of the constructor of the car class. But this injection, injection into an entity is said to be a bad idea. is this true?</p>
  
  <p>b) I can directly instantiate a validator object in the constructor of the car class. But this time, the dependency will be hidden.  this is also a bad approach, right?</p>
  
  <p>c) in view of injecting something to manager classes is fine, I can validate entities in the manager class. But again, this is a bad design, this responsibility is not the manager's, but the entity's itself. right?</p>
  
  <p>What is the best, or conventional way to validate an entity?</p>
  
  <h2>More explanation</h2>
  
  <p>The following approach hides the dependency to the <code>Validator</code> class, as far as I know. So this is a bad one, right?</p>
  
  <pre><code>class car
  {
    private $price;
    private $color;
  
    public __get($name){ ... }
    public setPrice($price} {
      $validator = new Validator();
      $validator-&gt;assert($price, '...some constraint...');
    }
    ...
  }
  </code></pre>
  
  <p>And this is also bad because the car class depends on something which is not directly related its entity, right?</p>
  
  <pre><code>class car
  {
    private $price;
    private $color;
  
    private $validator;
  
    public __construct(Validator $validator){
      $this-&gt;validator = $validator;
    }
  
    public __get($name){ ... }
    ....
    public setPrice($price} {
      $this-&gt;validator-&gt;assert($price, '...some constraint...');
    }
    ...
  }
  </code></pre>
  
  <p>So, how can I validate prices with an external service?</p>